序:
最近在给客户做一个管理系统的时候,客户提出要将查询结果导出到Excel。对于还是实习生的我倍感压力,于是找了点资料。网上有个示例,其中方法十分简单。于是照猫画虎,把方法和心得与大家分享。OK,Let`s go
第一步:
创建一个Excel文件(就是 普通的Excel),在第一个单元格输入“city”,然后选择“另存为”,此时弹出保存窗口。注意:将保持格式选择为“XML 表格(*.xml)”,点击保存。完毕后用记事本打开这个Excel文件。你将看到如下的代码
<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"xmlns:html="http://piledTransform(); xct.Load(Application.StartupPath+"\\excel.xslt"); XmlTextWriter writer = new XmlTextWriter("output.xls", null); writer.WriteProcessingInstruction("xml", "version="1.0""); xct.Transform(xdoc, null,writer); writer.Close();}总结:
其实这个方法的核心就是利用.net的xslt转换功能将xml以一种特殊格式输出出去。而这种格式可以利用Excel软件自动生成