最终导出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