导出excel的一种方法
最近在做一个根据模板导出excel的东西,思想是利用excel生的xml电子表格的xml,替换里面的内容,直接看代码吧
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 <Author>雨林木风</Author>
10 <LastAuthor>雨林木风</LastAuthor>
11 <Created>2009-12-18T13:00:27Z</Created>
12 <Company>WWW.YlmF.CoM</Company>
13 <Version>11.9999</Version>
14 </DocumentProperties>
15 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
16 <WindowHeight>9570</WindowHeight>
17 <WindowWidth>16035</WindowWidth>
18 <WindowTopX>0</WindowTopX>
19 <WindowTopY>120</WindowTopY>
20 <ProtectStructure>False</ProtectStructure>
21 <ProtectWindows>False</ProtectWindows>
22 </ExcelWorkbook>
23 <Styles>
24 <Style ss:ID="Default" ss:Name="Normal">
25 <Alignment ss:Vertical="Center"/>
26 <Borders/>
27 <Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>
28 <Interior/>
29 <NumberFormat/>
30 <Protection/>
31 </Style>
32 </Styles>
33 <Names>
34 <NamedRange ss:Name="年龄" ss:RefersTo="=Sheet1!R2C2"/>
35 <NamedRange ss:Name="姓名" ss:RefersTo="=Sheet1!R1C2"/>
36 </Names>
37 <Worksheet ss:Name="Sheet1">
38 <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"
39 x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
40 <Row>
41 <Cell><Data ss:Type="String">姓名:</Data></Cell>
42 <Cell><Data ss:Type="String">shadow</Data><NamedCell ss:Name="姓名"/></Cell>
43 </Row>
44 <Row>
45 <Cell><Data ss:Type="String">年龄:</Data></Cell>
46 <Cell><Data ss:Type="Number">23</Data><NamedCell ss:Name="年龄"/></Cell>
47 </Row>
48 </Table>
49 <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
50 <Print>
51 <ValidPrinterInfo/>
52 <PaperSizeIndex>9</PaperSizeIndex>
53 <HorizontalResolution>600</HorizontalResolution>
54 <VerticalResolution>600</VerticalResolution>
55 </Print>
56 <Selected/>
57 <Panes>
58 <Pane>
59 <Number>3</Number>
60 <ActiveRow>1</ActiveRow>
61 <ActiveCol>1</ActiveCol>
62 </Pane>
63 </Panes>
64 <ProtectObjects>False</ProtectObjects>
65 <ProtectScenarios>False</ProtectScenarios>
66 </WorksheetOptions>
67 </Worksheet>
68 </Workbook>
69
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 <Author>雨林木风</Author>
10 <LastAuthor>雨林木风</LastAuthor>
11 <Created>2009-12-18T13:00:27Z</Created>
12 <Company>WWW.YlmF.CoM</Company>
13 <Version>11.9999</Version>
14 </DocumentProperties>
15 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
16 <WindowHeight>9570</WindowHeight>
17 <WindowWidth>16035</WindowWidth>
18 <WindowTopX>0</WindowTopX>
19 <WindowTopY>120</WindowTopY>
20 <ProtectStructure>False</ProtectStructure>
21 <ProtectWindows>False</ProtectWindows>
22 </ExcelWorkbook>
23 <Styles>
24 <Style ss:ID="Default" ss:Name="Normal">
25 <Alignment ss:Vertical="Center"/>
26 <Borders/>
27 <Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>
28 <Interior/>
29 <NumberFormat/>
30 <Protection/>
31 </Style>
32 </Styles>
33 <Names>
34 <NamedRange ss:Name="年龄" ss:RefersTo="=Sheet1!R2C2"/>
35 <NamedRange ss:Name="姓名" ss:RefersTo="=Sheet1!R1C2"/>
36 </Names>
37 <Worksheet ss:Name="Sheet1">
38 <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"
39 x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
40 <Row>
41 <Cell><Data ss:Type="String">姓名:</Data></Cell>
42 <Cell><Data ss:Type="String">shadow</Data><NamedCell ss:Name="姓名"/></Cell>
43 </Row>
44 <Row>
45 <Cell><Data ss:Type="String">年龄:</Data></Cell>
46 <Cell><Data ss:Type="Number">23</Data><NamedCell ss:Name="年龄"/></Cell>
47 </Row>
48 </Table>
49 <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
50 <Print>
51 <ValidPrinterInfo/>
52 <PaperSizeIndex>9</PaperSizeIndex>
53 <HorizontalResolution>600</HorizontalResolution>
54 <VerticalResolution>600</VerticalResolution>
55 </Print>
56 <Selected/>
57 <Panes>
58 <Pane>
59 <Number>3</Number>
60 <ActiveRow>1</ActiveRow>
61 <ActiveCol>1</ActiveCol>
62 </Pane>
63 </Panes>
64 <ProtectObjects>False</ProtectObjects>
65 <ProtectScenarios>False</ProtectScenarios>
66 </WorksheetOptions>
67 </Worksheet>
68 </Workbook>
69
.cs
1 XmlDocument xml = new XmlDocument();
2 xml.Load(Server.MapPath("~/Book1.xml"));
3 XmlNamespaceManager xnm = new XmlNamespaceManager(xml.NameTable);
4 //先为xml添加命名空间,否则selectNode无效
5 xnm.AddNamespace("o", "urn:schemas-microsoft-com:office:office");
6 xnm.AddNamespace("x", "urn:schemas-microsoft-com:office:excel");
7 xnm.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
8 xnm.AddNamespace("html", "http://www.w3.org/TR/REC-html40");
9 //更改姓名,方法是为要输入的单元格命名,用xpath找到该单元格在xml的结点,并找到该结点的前一个结点即为所要找的结点,为它赋值
10 xml.SelectSingleNode("//ss:NamedCell[@ss:Name='姓名']", xnm).PreviousSibling.InnerText = this.TextBox1.Text;
11 //更改年龄
12 xml.SelectSingleNode("//ss:NamedCell[@ss:Name='年龄']", xnm).PreviousSibling.InnerText = this.TextBox2.Text;
13
14 //输出xml为xls
15 Response.Clear();
16 Response.ContentType = "application/x-excel";
17 Response.AppendHeader("Content-Disposition", "attachment; filename=test.xls");
18 Response.Write(xml.InnerXml);
19 Response.End();
2 xml.Load(Server.MapPath("~/Book1.xml"));
3 XmlNamespaceManager xnm = new XmlNamespaceManager(xml.NameTable);
4 //先为xml添加命名空间,否则selectNode无效
5 xnm.AddNamespace("o", "urn:schemas-microsoft-com:office:office");
6 xnm.AddNamespace("x", "urn:schemas-microsoft-com:office:excel");
7 xnm.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
8 xnm.AddNamespace("html", "http://www.w3.org/TR/REC-html40");
9 //更改姓名,方法是为要输入的单元格命名,用xpath找到该单元格在xml的结点,并找到该结点的前一个结点即为所要找的结点,为它赋值
10 xml.SelectSingleNode("//ss:NamedCell[@ss:Name='姓名']", xnm).PreviousSibling.InnerText = this.TextBox1.Text;
11 //更改年龄
12 xml.SelectSingleNode("//ss:NamedCell[@ss:Name='年龄']", xnm).PreviousSibling.InnerText = this.TextBox2.Text;
13
14 //输出xml为xls
15 Response.Clear();
16 Response.ContentType = "application/x-excel";
17 Response.AppendHeader("Content-Disposition", "attachment; filename=test.xls");
18 Response.Write(xml.InnerXml);
19 Response.End();
其实最后还是xml的,不过改了扩展名为xls
浙公网安备 33010602011771号