C# WinForm使用XML+XSLT导出Excel数据表格

最终导出EXCEL样式如下图:

1、将最终样式的Excel另存为XML电子表格2003,如下图:

2、使用记事本打开保存后的XML电子表格2003格式的文件,得到文件后台源码,如下:

  1 <?xml version="1.0"?>
  2 <?mso-application progid="Excel.Sheet"?>
  3 <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  4  xmlns:o="urn:schemas-microsoft-com:office:office"
  5  xmlns:x="urn:schemas-microsoft-com:office:excel"
  6  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  7  xmlns:html="http://www.w3.org/TR/REC-html40">
  8  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  9   <Created>2015-06-05T18:19:34Z</Created>
 10   <LastSaved>2017-08-29T14:13:09Z</LastSaved>
 11   <Version>16.00</Version>
 12  </DocumentProperties>
 13  <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
 14   <AllowPNG/>
 15   <RemovePersonalInformation/>
 16  </OfficeDocumentSettings>
 17  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
 18   <WindowHeight>12645</WindowHeight>
 19   <WindowWidth>22260</WindowWidth>
 20   <WindowTopX>0</WindowTopX>
 21   <WindowTopY>0</WindowTopY>
 22   <ProtectStructure>False</ProtectStructure>
 23   <ProtectWindows>False</ProtectWindows>
 24  </ExcelWorkbook>
 25  <Styles>
 26   <Style ss:ID="Default" ss:Name="Normal">
 27    <Alignment ss:Vertical="Bottom"/>
 28    <Borders/>
 29    <Font ss:FontName="等线" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
 30    <Interior/>
 31    <NumberFormat/>
 32    <Protection/>
 33   </Style>
 34   <Style ss:ID="m321133952">
 35    <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
 36    <Borders>
 37     <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
 38     <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
 39     <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
 40    </Borders>
 41    <Font ss:FontName="等线" x:CharSet="134" ss:Size="20" ss:Color="#000000"
 42     ss:Bold="1"/>
 43   </Style>
 44   <Style ss:ID="m321133972">
 45    <Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
 46    <Borders>
 47     <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
 48     <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
 49     <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
 50    </Borders>
 51   </Style>
 52   <Style ss:ID="s16">
 53    <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
 54    <Borders>
 55     <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
 56     <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
 57     <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
 58     <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
 59    </Borders>
 60   </Style>
 61   <Style ss:ID="s17">
 62    <Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
 63    <Borders>
 64     <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
 65     <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
 66     <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
 67     <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
 68    </Borders>
 69   </Style>
 70  </Styles>
 71  <Worksheet ss:Name="Sheet1">
 72   <Table ss:ExpandedColumnCount="5" ss:ExpandedRowCount="8" x:FullColumns="1"
 73    x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
 74    <Column ss:AutoFitWidth="0" ss:Width="35.25"/>
 75    <Column ss:AutoFitWidth="0" ss:Width="219"/>
 76    <Column ss:AutoFitWidth="0" ss:Width="63.75" ss:Span="1"/>
 77    <Column ss:Index="5" ss:AutoFitWidth="0" ss:Width="104.25"/>
 78    <Row ss:AutoFitHeight="0" ss:Height="42">
 79     <Cell ss:MergeAcross="4" ss:StyleID="m321133952"><Data ss:Type="String">房地产卷内目录</Data></Cell>
 80    </Row>
 81    <Row ss:AutoFitHeight="0" ss:Height="18">
 82     <Cell ss:MergeAcross="4" ss:StyleID="m321133972"><Data ss:Type="String">盒号 XXXX        卷号XXX</Data></Cell>
 83    </Row>
 84    <Row>
 85     <Cell ss:StyleID="s16"><Data ss:Type="String">编号</Data></Cell>
 86     <Cell ss:StyleID="s16"><Data ss:Type="String">文件名称</Data></Cell>
 87     <Cell ss:StyleID="s16"><Data ss:Type="String">件数</Data></Cell>
 88     <Cell ss:StyleID="s16"><Data ss:Type="String">页数</Data></Cell>
 89     <Cell ss:StyleID="s16"><Data ss:Type="String">附注</Data></Cell>
 90    </Row>
 91    <Row>
 92     <Cell ss:StyleID="s16"/>
 93     <Cell ss:StyleID="s17"/>
 94     <Cell ss:StyleID="s16"/>
 95     <Cell ss:StyleID="s16"/>
 96     <Cell ss:StyleID="s16"/>
 97    </Row>
 98    <Row>
 99     <Cell ss:StyleID="s16"/>
100     <Cell ss:StyleID="s17"/>
101     <Cell ss:StyleID="s16"/>
102     <Cell ss:StyleID="s16"/>
103     <Cell ss:StyleID="s16"/>
104    </Row>
105    <Row>
106     <Cell ss:StyleID="s16"/>
107     <Cell ss:StyleID="s17"/>
108     <Cell ss:StyleID="s16"/>
109     <Cell ss:StyleID="s16"/>
110     <Cell ss:StyleID="s16"/>
111    </Row>
112    <Row>
113     <Cell ss:StyleID="s16"/>
114     <Cell ss:StyleID="s17"/>
115     <Cell ss:StyleID="s16"/>
116     <Cell ss:StyleID="s16"/>
117     <Cell ss:StyleID="s16"/>
118    </Row>
119    <Row>
120     <Cell ss:StyleID="s16"/>
121     <Cell ss:StyleID="s17"/>
122     <Cell ss:StyleID="s16"/>
123     <Cell ss:StyleID="s16"/>
124     <Cell ss:StyleID="s16"/>
125    </Row>
126   </Table>
127   <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
128    <PageSetup>
129     <Header x:Margin="0.3"/>
130     <Footer x:Margin="0.3"/>
131     <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
132    </PageSetup>
133    <Print>
134     <ValidPrinterInfo/>
135     <PaperSizeIndex>9</PaperSizeIndex>
136     <HorizontalResolution>600</HorizontalResolution>
137     <VerticalResolution>600</VerticalResolution>
138    </Print>
139    <Selected/>
140    <Panes>
141     <Pane>
142      <Number>3</Number>
143      <ActiveRow>8</ActiveRow>
144      <ActiveCol>4</ActiveCol>
145     </Pane>
146    </Panes>
147    <ProtectObjects>False</ProtectObjects>
148    <ProtectScenarios>False</ProtectScenarios>
149   </WorksheetOptions>
150  </Worksheet>
151 </Workbook>

3、将上面的内容结合xslt样式修改,最后得到样式文件ExcelOut.xslt,具体为:

  1 <?xml version="1.0" ?>
  2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3   <xsl:template match="SuperMap">
  4     <xsl:call-template name="Sheet"></xsl:call-template>
  5   </xsl:template>
  6   <xsl:template name="Sheet">
  7     <?mso-application progid="Excel.Sheet"?>
  8     <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  9      xmlns:o="urn:schemas-microsoft-com:office:office"
 10      xmlns:x="urn:schemas-microsoft-com:office:excel"
 11      xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 12      xmlns:html="http://www.w3.org/TR/REC-html40">
 13       <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
 14         <Created>2015-06-05T18:19:34Z</Created>
 15         <LastSaved>2017-08-29T14:13:09Z</LastSaved>
 16         <Version>16.00</Version>
 17       </DocumentProperties>
 18       <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
 19         <AllowPNG/>
 20         <RemovePersonalInformation/>
 21       </OfficeDocumentSettings>
 22       <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
 23         <WindowHeight>12645</WindowHeight>
 24         <WindowWidth>22260</WindowWidth>
 25         <WindowTopX>0</WindowTopX>
 26         <WindowTopY>0</WindowTopY>
 27         <ProtectStructure>False</ProtectStructure>
 28         <ProtectWindows>False</ProtectWindows>
 29       </ExcelWorkbook>
 30       <Styles>
 31         <Style ss:ID="Default" ss:Name="Normal">
 32           <Alignment ss:Vertical="Bottom"/>
 33           <Borders/>
 34           <Font ss:FontName="等线" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
 35           <Interior/>
 36           <NumberFormat/>
 37           <Protection/>
 38         </Style>
 39         <Style ss:ID="m321133952">
 40           <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
 41           <Borders>
 42             <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
 43             <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
 44             <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
 45           </Borders>
 46           <Font ss:FontName="等线" x:CharSet="134" ss:Size="20" ss:Color="#000000"
 47            ss:Bold="1"/>
 48         </Style>
 49         <Style ss:ID="m321133972">
 50           <Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
 51           <Borders>
 52             <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
 53             <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
 54             <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
 55           </Borders>
 56         </Style>
 57         <Style ss:ID="s16">
 58           <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
 59           <Borders>
 60             <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
 61             <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
 62             <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
 63             <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
 64           </Borders>
 65         </Style>
 66         <Style ss:ID="s17">
 67           <Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
 68           <Borders>
 69             <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
 70             <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
 71             <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
 72             <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
 73           </Borders>
 74         </Style>
 75       </Styles>
 76       <Worksheet ss:Name="Sheet1">
 77         <Table ss:ExpandedColumnCount="5" ss:ExpandedRowCount="8" x:FullColumns="1"
 78          x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
 79           <Column ss:AutoFitWidth="0" ss:Width="35.25"/>
 80           <Column ss:AutoFitWidth="0" ss:Width="219"/>
 81           <Column ss:AutoFitWidth="0" ss:Width="63.75" ss:Span="1"/>
 82           <Column ss:Index="5" ss:AutoFitWidth="0" ss:Width="104.25"/>
 83           <Row ss:AutoFitHeight="0" ss:Height="42">
 84             <Cell ss:MergeAcross="4" ss:StyleID="m321133952">
 85               <Data ss:Type="String">房地产卷内目录</Data>
 86             </Cell>
 87           </Row>
 88           <Row ss:AutoFitHeight="0" ss:Height="18">
 89             <Cell ss:MergeAcross="4" ss:StyleID="m321133972">
 90               <Data ss:Type="String">
 91                 盒号 <xsl:value-of select="BOX_NUM" />        卷号<xsl:value-of select="UNIT_NUM" />
 92               </Data>
 93             </Cell>
 94           </Row>
 95           <Row>
 96             <Cell ss:StyleID="s16">
 97               <Data ss:Type="String">编号</Data>
 98             </Cell>
 99             <Cell ss:StyleID="s16">
100               <Data ss:Type="String">文件名称</Data>
101             </Cell>
102             <Cell ss:StyleID="s16">
103               <Data ss:Type="String">件数</Data>
104             </Cell>
105             <Cell ss:StyleID="s16">
106               <Data ss:Type="String">页数</Data>
107             </Cell>
108             <Cell ss:StyleID="s16">
109               <Data ss:Type="String">附注</Data>
110             </Cell>
111           </Row>
112           <xsl:for-each select="FILE">
113             <Row>
114               <Cell ss:StyleID="s16">
115                 <Data ss:Type="String"><xsl:value-of select="position()" /></Data>
116               </Cell>
117               <Cell ss:StyleID="s17">
118                 <Data ss:Type="String">
119                   <xsl:value-of select="FILE_NAME" />
120                 </Data>
121               </Cell>
122               <Cell ss:StyleID="s16">
123                 <Data ss:Type="String">
124                   <xsl:value-of select="FILE_NUM" />
125                 </Data>
126               </Cell>
127               <Cell ss:StyleID="s16">
128                 <Data ss:Type="String">
129                   <xsl:value-of select="FILE_PAGE" />
130                 </Data>
131               </Cell>
132               <Cell ss:StyleID="s16"/>
133             </Row>
134           </xsl:for-each>
135         </Table>
136         <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
137           <PageSetup>
138             <Header x:Margin="0.3"/>
139             <Footer x:Margin="0.3"/>
140             <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
141           </PageSetup>
142           <Print>
143             <ValidPrinterInfo/>
144             <PaperSizeIndex>9</PaperSizeIndex>
145             <HorizontalResolution>600</HorizontalResolution>
146             <VerticalResolution>600</VerticalResolution>
147           </Print>
148           <Selected/>
149           <Panes>
150             <Pane>
151               <Number>3</Number>
152               <ActiveRow>8</ActiveRow>
153               <ActiveCol>4</ActiveCol>
154             </Pane>
155           </Panes>
156           <ProtectObjects>False</ProtectObjects>
157           <ProtectScenarios>False</ProtectScenarios>
158         </WorksheetOptions>
159       </Worksheet>
160     </Workbook>
161   </xsl:template>
162 </xsl:stylesheet>

4、C#后台代码实现

 1 private XmlDocument GetXMlDocument(List<string[]> _List)
 2         {
 3             XmlDocument _XmlDocument = new XmlDocument();
 4             XmlElement _XmlElement;
 5             XmlElement _XmlElementChild;
 6
 7             _XmlDocument.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?>" +
 8                 "<?xml-stylesheet type=‘text/xsl‘ href=‘‘?><SuperMap/>");
 9
10             for (int i = 0; i < _List.Count; i++)
11             {
12                 string[] a = _List[i];
13                 _XmlElement = _XmlDocument.CreateElement("FILE");
14
15                 _XmlElementChild = _XmlDocument.CreateElement("FILE_NAME");
16                 _XmlElementChild.InnerText = a[1];
17                 _XmlElement.AppendChild(_XmlElementChild);
18
19                 _XmlElementChild = _XmlDocument.CreateElement("FILE_NUM");
20                 _XmlElementChild.InnerText = a[2];
21                 _XmlElement.AppendChild(_XmlElementChild);
22
23                 _XmlElementChild = _XmlDocument.CreateElement("FILE_PAGE");
24                 _XmlElementChild.InnerText = a[3];
25                 _XmlElement.AppendChild(_XmlElementChild);
26
27                 _XmlDocument.DocumentElement.AppendChild(_XmlElement);
28             }
29
30             _XmlElement = _XmlDocument.CreateElement("BOX_NUM");
31             _XmlElement.InnerText = textBox1.Text;
32             _XmlDocument.DocumentElement.AppendChild(_XmlElement);
33
34             _XmlElement = _XmlDocument.CreateElement("UNIT_NUM");
35             _XmlElement.InnerText = textBox2.Text;
36             _XmlDocument.DocumentElement.AppendChild(_XmlElement);
37
38             return _XmlDocument;
39         }
40
41         private void SaveFile(List<string[]> _List, string FileFullPath)
42         {
43             XmlDocument _XmlDocument = new XmlDocument();
44             System.Xml.Xsl.XslCompiledTransform xslt = new System.Xml.Xsl.XslCompiledTransform();
45             StreamWriter sw = new StreamWriter(FileFullPath, false, Encoding.GetEncoding("utf-8"));
46             StringWriter stringWrite = new System.IO.StringWriter();
47             XmlTextReader r = new XmlTextReader(new StringReader(GetXMlDocument(_List).OuterXml));
48             xslt.Load(System.Environment.CurrentDirectory + "\\" + "ExcelOut.xslt");
49             xslt.Transform(r, null, stringWrite);
50             sw.Write(stringWrite.ToString());
51             sw.Flush();
52             sw.Close();
53             sw.Dispose();
54         }
时间: 2024-11-03 01:33:06

C# WinForm使用XML+XSLT导出Excel数据表格的相关文章

vue导出excel数据表格功能

前端工作量最多的就是需求,需求就是一直在变,比如当前端数据写完之后,需要用Excel把数据下载出来. 第一步安装依赖包,需要把代码下载你的项目当中 cnpm install  file-saver cnpm install xlsx cnpm install script-loader 第二步在项目中创建一个新的文件夹用于存放Blob和Export2Excel两个js文件 第三步在你那个组件中使用 tHeader:标题 filterVal:字段名 tableData:前端请求的数据 export

【基于WinForm+Access局域网共享数据库的项目总结】之篇二:WinForm开发扇形图统计和Excel数据导出

篇一:WinForm开发总体概述与技术实现 篇二:WinForm开发扇形图统计和Excel数据导出 篇三:Access远程连接数据库和窗体打包部署 [小记]:最近基于WinForm+Access数据库完成一个法律咨询管理系统.本系统要求类似网页后台管理效果,并且基于局域网内,完成多客户端操作同一数据库,根据权限不同分别执行不同功能模块.核心模块为级联统计类型管理.数据库咨询数据扇形统计.树的操作.咨询数据的管理.手写分页.Excel数据的导出.多用户操作服务器数据等.并支持多用户同时操作,远程连

[转] Asp.Net 导出 Excel 数据的9种方案

湛刚 de BLOG 原文地址 Asp.Net 导出 Excel 数据的9种方案 简介 Excel 的强大之处在于它不仅仅只能打开Excel格式的文档,它还能打开CSV格式.Tab格式.website table 等多钟格式的文档.它具备自动识别行号,字符,格式化数字等功能,例如:如果你在Excel 单元格中输入数字 "123456789012" 会自动转化为"1.23457E+11". 背景介绍 正因为Excel的强大和易用,大家都喜欢将数据导出为 Excel 备

导出EXCEL数据时防止数值变科学计数的办法

网上有很多说法,最简单直接正确的做法是判断一下是否为数值以及长度,然后给单元格加上以下CSS即可: mso-generic-font-family:auto;   mso-font-charset:134;    mso-number-format:"\@";   mso-background-source:auto;   mso-pattern:auto; 这个办法我是从:http://www.cnblogs.com/joinger/articles/1322931.html 这个博

Java中导出Excel数据,封装Bean

在Java中封装导出Excel数据的总体思路为: 1.导出,根据查询得到的数据(一般我们页面上用的是查询条件带分页的),我们需要把查询条件带到后台重新查询,并且是查询全部数据,不带分页 2.不能用ajax异步提交.应该用location.href=""的方式,把处理的流交给浏览器来处理,JS无法处理流计算. 3.封装统一的Bean实体,主要输入导出的列名,excel名,数据集即可导出. 用到的Maven地址: <!-- poi --> <dependency>

导出Excel数据

先要导入jxl架包,其中的abc.xls为测试Excel,具体代码如下,仅供参考: import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import jxl.Cell; import jxl.Sheet; import jxl.Wo

php导出excel数据使用csv替代xls格式

php导出excel数据使用csv替代xls格式 一直以来需要将某些后台数据导出来以供运营人员查看,因为他们都用excel.所以最初的选择是用phpExcel这个工具包来实现导出数据为xls格式,后来发现数据量比较大的情况下(超过1W),就比较慢了. http://www.zroran.com/it/php/9.html 之前使用phpexcel的过程如下 $objPHPExcel = new PHPExcel(); $excelobj = $objPHPExcel->setActiveShee

基于POI导出Excel数据

基于POI导出Excel数据 在项目中会有许多报表业务,需要导出数据.在这里我们采用基于POI的方式解析.POI有俩种解析方式HSSF(xls格式)和XSSF(xlsx). 以BOS物流的运单管理界面为例: 前端代码 导出运单信息,生成报表: 1.添加导出按钮 2.添加导出事件 后台代码实现 POI生成Excel步骤写Excel过程一样,新建Excel文档----新建Sheet---新建Row---新建Cell单元格---写单元格数据. Maven导入jar包信息请参加一键上传里面的配置 Act

NPOI导出EXCEL数据量大,分多个sheet显示数据

//NPOIHelper 类关键代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.IO; using NPOI.HSSF.UserModel; using System.Collections; using System.Web; namespace Yikeba_htmlConverter { publi