Asp.net 2.0 装载XML文件中数据到Excel文件中(示例代码下载)

(一). 概要

从读取XML文件数据,生成Excel文件.

(二).运行效果图示例

1. 要转换数据的XML文件

Asp.net 2.0 装载XML文件中数据到Excel文件中(示例代码下载)

2. 生成的Excel文件显示效果

Asp.net 2.0 装载XML文件中数据到Excel文件中(示例代码下载)

(三). 代码

1 try
2 {
3//要转换的XML文件
4stringXMLFileName=Path.Combine(Request.PhysicalApplicationPath,"book.xml");
5DataSetdsBook=newDataSet();
6dsBook.ReadXml(XMLFileName);
7introws=dsBook.Tables[0].Rows.Count+1;
8intcols=dsBook.Tables[0].Columns.Count;
9
10//将要生成的Excel文件
11stringExcelFileName=Path.Combine(Request.PhysicalApplicationPath,"book.xls");
12if(File.Exists(ExcelFileName))
13{
14File.Delete(ExcelFileName);
15}

16StreamWriterwriter=newStreamWriter(ExcelFileName,false);
17writer.WriteLine("<?xmlversion=\"1.0\"?>");
18writer.WriteLine("<?mso-applicationprogid=\"Excel.Sheet\"?>");
19writer.WriteLine("<Workbookxmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
20writer.WriteLine("xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
21writer.WriteLine("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
22writer.WriteLine("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
23writer.WriteLine("xmlns:html=\"http://www.w3.org/TR/REC-html40/\">");
24writer.WriteLine("<DocumentPropertiesxmlns=\"urn:schemas-microsoft-com:office:office\">");
25writer.WriteLine("<Author>AutomatedReportGeneratorExample</Author>");
26writer.WriteLine(string.Format("<Created>{0}T{1}Z</Created>",DateTime.Now.ToString("yyyy-mm-dd"),DateTime.Now.ToString("HH:MM:SS")));
27writer.WriteLine("<Company>YourCompanyHere</Company>");
28writer.WriteLine("<Version>11.6408</Version>");
29writer.WriteLine("</DocumentProperties>");
30writer.WriteLine("<ExcelWorkbookxmlns=\"urn:schemas-microsoft-com:office:excel\">");
31writer.WriteLine("<WindowHeight>8955</WindowHeight>");
32writer.WriteLine("<WindowWidth>11355</WindowWidth>");
33writer.WriteLine("<WindowTopX>480</WindowTopX>");
34writer.WriteLine("<WindowTopY>15</WindowTopY>");
35writer.WriteLine("<ProtectStructure>False</ProtectStructure>");
36writer.WriteLine("<ProtectWindows>False</ProtectWindows>");
37writer.WriteLine("</ExcelWorkbook>");
38writer.WriteLine("<Styles>");
39writer.WriteLine("<Styless:ID=\"Default\"ss:Name=\"Normal\">");
40writer.WriteLine("<Alignmentss:Vertical=\"Bottom\"/>");
41writer.WriteLine("<Borders/>");
42writer.WriteLine("<Font/>");
43writer.WriteLine("<Interior/>");
44writer.WriteLine("<Protection/>");
45writer.WriteLine("</Style>");
46writer.WriteLine("<Styless:ID=\"s21\">");
47writer.WriteLine("<Alignmentss:Vertical=\"Bottom\"ss:WrapText=\"1\"/>");
48writer.WriteLine("</Style>");
49writer.WriteLine("</Styles>");
50writer.WriteLine("<Worksheetss:Name=\"MyReport\">");
51writer.WriteLine(string.Format("<Tabless:ExpandedColumnCount=\"{0}\"ss:ExpandedRowCount=\"{1}\"x:FullColumns=\"1\"",cols.ToString(),rows.ToString()));
52writer.WriteLine("x:FullRows=\"1\">");
53
54//生成标题行
55writer.WriteLine("<Row>");
56foreach(DataColumneachCloumnindsBook.Tables[0].Columns)
57{
58writer.Write("<Cellss:StyleID=\"s21\"><Datass:Type=\"String\">");
59writer.Write(eachCloumn.ColumnName.ToString());
60writer.WriteLine("</Data></Cell>");
61}

62writer.WriteLine("</Row>");
63
64//生成数据记录行
65foreach(DataRoweachRowindsBook.Tables[0].Rows)
66{
67writer.WriteLine("<Row>");
68for(intcurrentRow=0;currentRow!=cols;currentRow++)
69{
70writer.Write("<Cellss:StyleID=\"s21\"><Datass:Type=\"String\">");
71writer.Write(eachRow[currentRow].ToString());
72writer.WriteLine("</Data></Cell>");
73}

74writer.WriteLine("</Row>");
75}

76writer.WriteLine("</Table>");
77writer.WriteLine("<WorksheetOptionsxmlns=\"urn:schemas-microsoft-com:office:excel\">");
78writer.WriteLine("<Selected/>");
79writer.WriteLine("<Panes>");
80writer.WriteLine("<Pane>");
81writer.WriteLine("<Number>3</Number>");
82writer.WriteLine("<ActiveRow>1</ActiveRow>");
83writer.WriteLine("</Pane>");
84writer.WriteLine("</Panes>");
85writer.WriteLine("<ProtectObjects>False</ProtectObjects>");
86writer.WriteLine("<ProtectScenarios>False</ProtectScenarios>");
87writer.WriteLine("</WorksheetOptions>");
88writer.WriteLine("</Worksheet>");
89writer.WriteLine("<Worksheetss:Name=\"Sheet2\">");
90writer.WriteLine("<WorksheetOptionsxmlns=\"urn:schemas-microsoft-com:office:excel\">");
91writer.WriteLine("<ProtectObjects>False</ProtectObjects>");
92writer.WriteLine("<ProtectScenarios>False</ProtectScenarios>");
93writer.WriteLine("</WorksheetOptions>");
94writer.WriteLine("</Worksheet>");
95writer.WriteLine("<Worksheetss:Name=\"Sheet3\">");
96writer.WriteLine("<WorksheetOptionsxmlns=\"urn:schemas-microsoft-com:office:excel\">");
97writer.WriteLine("<ProtectObjects>False</ProtectObjects>");
98writer.WriteLine("<ProtectScenarios>False</ProtectScenarios>");
99writer.WriteLine("</WorksheetOptions>");
100writer.WriteLine("</Worksheet>");
101writer.WriteLine("</Workbook>");
102writer.Close();
103Response.Write("<scriptlanguage=\"javascript\">"+"alert('"+"转换成功!转换后的Excel文件名为:"+ExcelFileName+"')"+"</script>");
104}

105 catch (Exceptionex)
106 {
107Response.Write("<scriptlanguage=\"javascript\">"+"alert('"+"操作失败!出错信息:"+ex.Message+"')"+"</script>");
108}

(四). 示例代码下载

http://www.cnblogs.com/Files/ChengKing/XMLChangeToExcel.rar

你可能感兴趣的:(xml,.net,Excel,asp.net,asp)