c# 导出Excel Xml格式 不用安装Excel也不用引入任何组件
2010-06-05 12:22
591 查看
c# 导出Excel Xml格式 不用安装Excel也不用引入任何组件
原文地址:http://hi.baidu.com/sbiweeq/blog/item/40fe0882effc21b36c8119b6.html
原文地址:http://hi.baidu.com/sbiweeq/blog/item/40fe0882effc21b36c8119b6.html
public static void CreateExcel(string filepath, int roms, int troms, int zroms, string strdate, Model.Form8 form8) { if (filepath != "") { StreamWriter rw = new StreamWriter(filepath, false, Encoding.GetEncoding("UTF-8"), 10240); rw.WriteLine("<?xml version=/"1.0/"?>"); rw.WriteLine("<?mso-application progid=/"Excel.Sheet/"?>"); rw.WriteLine("<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://www.w3.org/TR/REC-html40/">"); rw.WriteLine("<DocumentProperties xmlns=/"urn:schemas-microsoft-com:office:office/">"); rw.WriteLine("<Created>1996-12-17T01:32:42Z</Created>"); rw.WriteLine("<LastSaved>2000-11-18T06:53:49Z</LastSaved>"); rw.WriteLine("<Version>11.8107</Version>"); rw.WriteLine("</DocumentProperties>"); rw.WriteLine("<OfficeDocumentSettings xmlns=/"urn:schemas-microsoft-com:office:office/">"); rw.WriteLine("<RemovePersonalInformation/>"); rw.WriteLine("</OfficeDocumentSettings>"); rw.WriteLine("<ExcelWorkbook xmlns=/"urn:schemas-microsoft-com:office:excel/">"); rw.WriteLine("<WindowHeight>4530</WindowHeight>"); rw.WriteLine("<WindowWidth>8505</WindowWidth>"); rw.WriteLine("<WindowTopX>480</WindowTopX>"); rw.WriteLine("<WindowTopY>120</WindowTopY>"); rw.WriteLine("<AcceptLabelsInFormulas/>"); rw.WriteLine("<ProtectStructure>False</ProtectStructure>"); rw.WriteLine("<ProtectWindows>False</ProtectWindows>"); rw.WriteLine("</ExcelWorkbook>"); rw.WriteLine("<Styles>"); rw.WriteLine("<Style ss:ID=/"Default/" ss:Name=/"Normal/">"); rw.WriteLine("<Alignment ss:Vertical=/"Bottom/"/>"); rw.WriteLine("<Borders/>"); rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"12/"/>"); rw.WriteLine("<Interior/>"); rw.WriteLine("<NumberFormat/>"); rw.WriteLine("<Protection/>"); rw.WriteLine("</Style>"); rw.WriteLine("<Style ss:ID=/"s24/">"); rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"9/"/>"); rw.WriteLine("<Alignment ss:Horizontal=/"Center/" ss:Vertical=/"Bottom/"/>"); rw.WriteLine(" <Borders>"); rw.WriteLine(" <Border ss:Position=/"Bottom/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>"); rw.WriteLine("<Border ss:Position=/"Left/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>"); rw.WriteLine("<Border ss:Position=/"Right/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>"); rw.WriteLine("<Border ss:Position=/"Top/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>"); rw.WriteLine("</Borders>"); rw.WriteLine("<NumberFormat ss:Format=/"@/"/>"); rw.WriteLine("</Style>"); rw.WriteLine("<Style ss:ID=/"s25/">"); rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"9/"/>"); rw.WriteLine("<Alignment ss:Horizontal=/"Center/" ss:Vertical=/"Bottom/"/>"); rw.WriteLine("<Borders>"); rw.WriteLine(" <Border ss:Position=/"Bottom/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>"); rw.WriteLine(" <Border ss:Position=/"Left/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>"); rw.WriteLine(" <Border ss:Position=/"Right/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>"); rw.WriteLine(" <Border ss:Position=/"Top/" ss:LineStyle=/"Continuous/" ss:Weight=/"1/"/>"); rw.WriteLine("</Borders>"); rw.WriteLine("</Style>"); rw.WriteLine("<Style ss:ID=/"s26/">"); rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"9/"/>"); rw.WriteLine("<Alignment ss:Horizontal=/"Right/" ss:Vertical=/"Bottom/"/>"); rw.WriteLine("</Style>"); rw.WriteLine("<Style ss:ID=/"s27/">"); rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"9/"/>"); rw.WriteLine("<Alignment ss:Horizontal=/"Left/" ss:Vertical=/"Bottom/"/>"); rw.WriteLine("</Style>"); rw.WriteLine("<Style ss:ID=/"m24861836/">"); rw.WriteLine("<Alignment ss:Horizontal=/"Center/" ss:Vertical=/"Center/"/>"); rw.WriteLine("<Borders/>"); rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"20/" ss:Bold=/"1/"/>"); rw.WriteLine("<NumberFormat/>"); rw.WriteLine("<Protection ss:Protected=/"0/"/>"); rw.WriteLine("</Style>"); rw.WriteLine("<Style ss:ID=/"s28/">"); rw.WriteLine("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"9/"/>"); rw.WriteLine("<Alignment ss:Horizontal=/"Left/" ss:Vertical=/"Bottom/"/>"); rw.WriteLine("<NumberFormat ss:Format=/"@/"/>"); rw.WriteLine("</Style>"); rw.WriteLine("</Styles>"); //sheet rw.WriteLine("<Worksheet ss:Name=/"Sheet1/">"); rw.WriteLine("<Table x:FullColumns=/"1/" x:FullRows=/"1/" ss:DefaultColumnWidth=/"75/" ss:DefaultRowHeight=/"14.25/">"); if (strdate != null) {< 4000 br /> string[] str = strdate.Split(' ̄'); int cou = 1; //设置表头 rw.WriteLine("<Row ss:Height=/"31.5/">"); rw.WriteLine("<Cell ss:MergeAcross=/"7/" ss:StyleID=/"m24861836/"><Data ss:Type=/"String/">123123</Data></Cell>"); rw.WriteLine("</Row>"); rw.WriteLine("<Row>"); rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123123</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s27/"/>"); rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123</Data></Cell>"); rw.WriteLine("<Cell ss:MergeAcross=/"2/" ss:StyleID=/"s27/"><Data ss:Type=/"String/">asdasd</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123123</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s28/"><Data ss:Type=/"Number/">123</Data></Cell>"); rw.WriteLine("</Row>"); rw.WriteLine("<Row>"); rw.WriteLine("<Cell ss:MergeAcross=/"1/" ss:StyleID=/"s25/"><Data ss:Type=/"String/">q123</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>"); rw.WriteLine("</Row>"); for (int i = 0; i < roms; i++) { rw.WriteLine("<Row>"); for (int j = 0; j < 8; j++) { if (j == 0) { if (i < troms) { rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>"); } if (i >= troms && i < (troms + zroms)) { rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">123</Data></Cell>"); } } else { if (i < (troms + zroms)) { try { if (isNum(str[cou])) { rw.WriteLine("<Cell ss:StyleID=/"s24/"><Data ss:Type=/"Number/">" + str[cou] + "</Data></Cell>"); } else { rw.WriteLine("<Cell ss:StyleID=/"s25/"><Data ss:Type=/"String/">" + str[cou] + "</Data></Cell>"); } cou++; } catch (Exception ex) { } } } } rw.WriteLine("</Row>"); } } //设置表尾 rw.WriteLine("<Row>"); rw.WriteLine("<Cell/>"); rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123:</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s27/"><Data ss:Type=/"String/">" + form8.P302 + "</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123:</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s27/"><Data ss:Type=/"String/">" + form8.P303 + "</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s26/"><Data ss:Type=/"String/">123:</Data></Cell>"); rw.WriteLine("<Cell ss:StyleID=/"s27/"><Data ss:Type=/"String/">" + form8.P304 + "</Data></Cell>"); rw.WriteLine("</Row>"); rw.WriteLine("</Table>"); rw.WriteLine("<WorksheetOptions xmlns=/"urn:schemas-microsoft-com:office:excel/">"); rw.WriteLine("<Selected/>"); rw.WriteLine("<ProtectObjects>False</ProtectObjects>"); rw.WriteLine("<ProtectScenarios>False</ProtectScenarios>"); rw.WriteLine("</WorksheetOptions>"); rw.WriteLine("</Worksheet>"); rw.WriteLine("<Worksheet ss:Name=/"Sheet2/">"); rw.WriteLine("<Table ss:ExpandedColumnCount=/"0/" ss:ExpandedRowCount=/"0/" x:FullColumns=/"1/" x:FullRows=/"1/" ss:DefaultColumnWidth=/"54/" ss:DefaultRowHeight=/"14.25/"/>"); rw.WriteLine("<WorksheetOptions xmlns=/"urn:schemas-microsoft-com:office:excel/">"); rw.WriteLine("<Selected/>"); rw.WriteLine("<ProtectObjects>False</ProtectObjects>"); rw.WriteLine("<ProtectScenarios>False</ProtectScenarios>"); rw.WriteLine("</WorksheetOptions>"); rw.WriteLine("</Worksheet>"); rw.WriteLine("<Worksheet ss:Name=/"Sheet3/">"); rw.WriteLine("<Table ss:ExpandedColumnCount=/"0/" ss:ExpandedRowCount=/"0/" x:FullColumns=/"1/" x:FullRows=/"1/" ss:DefaultColumnWidth=/"54/" ss:DefaultRowHeight=/"14.25/"/>"); rw.WriteLine("<WorksheetOptions xmlns=/"urn:schemas-microsoft-com:office:excel/">"); rw.WriteLine("<Selected/>"); rw.WriteLine("<ProtectObjects>False</ProtectObjects>"); rw.WriteLine("<ProtectScenarios>False</ProtectScenarios>"); rw.WriteLine("</WorksheetOptions>"); rw.WriteLine("</Worksheet>"); //sheet rw.WriteLine("</Workbook>"); rw.Flush(); rw.Close(); } } |
相关文章推荐
- 导出Excel,可以转换科学计数法为文本,不用引用Excel组件
- (C#)利用Aspose.Cells组件导入导出excel文件
- 导出Excel方式汇总-- 第三种 利用 Npoi 插件,只需要在项目里引用DLL即可,无需在服务器安装任何插件。
- c# 使用Interop.Excel.dll官方组件导出Excel(根据模板)
- C#_Web应用程序_导出Excel出错(检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败。。。)
- 用C#.net编写导入导出EXCEL文件的代码。编绎运行后,错误提示: 找不到可安装的ISAM
- 无需安装微软组件的 excel导出方式
- C# 用Microsoft组件导出excel 报错解决方式00024500-0000-0000-C000-000000000046 80070005拒绝访问
- Myxls组件导入导出Excel(不需安装Excel)
- C#中Excel导入导出(通过NPOI组件)
- (C#)利用Aspose.Cells组件导入导出excel文件
- C# 根据模板 导出 Excel 图表 (NPOI组件)
- c#发邮件,我在win2003验证过,可以用.不用装任何组件
- 导出Excel--不用安装office
- Myxls组件导入导出Excel(不需安装Excel)
- C#中导出EXCEL服务器端不用安装OFFICE
- asp.net导出数据到Excel的方法(完全无组件,不用调用Excel.exe)
- c# 导出excel 组件比较(npoi vs openxml)
- asp.net导出数据到Excel的方法(完全无组件,不用调用Excel.exe)
- C#对Excel的操作(可以做报表导出的组件)