首页  ·  知识 ·  云计算
ASP.NET读出XML数据生成Excel
天高云淡  http://www.cnblogs.com/morningwang/archive/2007/05  综合  编辑:dezai  图片来源:网络
概要描述:读取XML数据,最终生成Excel数据。一.新建一个book.xml,代码内容如下: 1?xml version="1.0" encoding="utf-8" ?&

概要描述:读取XML数据,最终生成Excel数据。
一.新建一个book.xml,代码内容如下:

 1
 2http://tempuri.org/Book.xsd">
 3 
 4    C# Program
 5   
 6      Wang
 7      Lei
 8   

 9    10
10 

11 
12    VB.Net Program
13   
14      James
15      Wang
16   

17    8
18 

19 
20    Sql server 2005 database manager
21   
22      Elvis
23      Yu
24   

25    5
26 

27
二.新建一个Default.aspx的页面,在Default.aspx页面上拖放一个按钮,双击进入按钮的事件,代码如下:
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
 10using System.IO;
 11
 12public partial class _Default : System.Web.UI.Page
 13{
 14    protected void Page_Load(object sender, EventArgs e)
 15    {
 16
 17    }
 18    protected void Button1_Click(object sender, EventArgs e)
 19    {
 20        try
 21        {
 22            //要转换的XML文件
 23            string XMLFileName = Path.Combine(Request.PhysicalApplicationPath, "book.xml");
 24            DataSet dsBook = new DataSet();
 25            dsBook.ReadXml(XMLFileName);
 26            int rows = dsBook.Tables[0].Rows.Count + 1;
 27            int cols = dsBook.Tables[0].Columns.Count;
 28
 29            //将要生成的Excel文件
 30            string ExcelFileName = Path.Combine(Request.PhysicalApplicationPath, "book.xls");
 31            if (File.Exists(ExcelFileName))
 32            {
 33                File.Delete(ExcelFileName);
 34            }
 35            StreamWriter writer = new StreamWriter(ExcelFileName, false);
 36            writer.WriteLine("");
 37            writer.WriteLine("");
 38            writer.WriteLine(" 39            writer.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
 40            writer.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
 41            writer.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
 42            writer.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40/\">");
 43            writer.WriteLine(" ");
 44            writer.WriteLine("  Automated Report Generator Example");
 45            writer.WriteLine(string.Format("  {0}T{1}Z", DateTime.Now.ToString("yyyy-mm-dd"), DateTime.Now.ToString("HH:MM:SS")));
 46            writer.WriteLine("  Your Company Here");
 47            writer.WriteLine("  11.6408");
 48            writer.WriteLine("
");
 49            writer.WriteLine(" ");
 50            writer.WriteLine("  8955");
 51            writer.WriteLine("  11355");
 52            writer.WriteLine("  480");
 53            writer.WriteLine("  15");
 54            writer.WriteLine("  False");
 55            writer.WriteLine("  False");
 56            writer.WriteLine("
");
 57            writer.WriteLine(" ");
 58            writer.WriteLine("  ");
 65            writer.WriteLine("  ");
 68            writer.WriteLine("
");
 69            writer.WriteLine(" ");
 70            writer.WriteLine(string.Format(" 

 71            writer.WriteLine("   x:FullRows=\"1\">");
 72            //生成标题
 73            writer.WriteLine("");
 74            foreach (DataColumn eachCloumn in dsBook.Tables[0].Columns)
 75            {
 76                writer.Write("");
 77                writer.Write(eachCloumn.ColumnName.ToString());
 78                writer.WriteLine("
");
 79            }
 80            writer.WriteLine("
");
 81
 82            //生成数据记录
 83            foreach (DataRow eachRow in dsBook.Tables[0].Rows)
 84            {
 85                writer.WriteLine("");
 86                for (int currentRow = 0; currentRow != cols; currentRow++)
 87                {
 88                    writer.Write("");
 89                    writer.Write(eachRow[currentRow].ToString());
 90                    writer.WriteLine("
");
 91                }
 92                writer.WriteLine("
");
 93            }
 94            writer.WriteLine(" 
");
 95            writer.WriteLine("  ");
 96            writer.WriteLine("   ");
 97            writer.WriteLine("   ");
 98            writer.WriteLine("    ");
 99            writer.WriteLine("     3");
100            writer.WriteLine("     1");
101            writer.WriteLine("   
");
102            writer.WriteLine("  
");
103            writer.WriteLine("   False");
104            writer.WriteLine("   False");
105            writer.WriteLine(" 
");
106            writer.WriteLine(" ");
107            writer.WriteLine(" ");
108            writer.WriteLine("  ");
109            writer.WriteLine("   False");
110            writer.WriteLine("   False");
111            writer.WriteLine(" 
");
112            writer.WriteLine("
");
113            writer.WriteLine(" ");
114            writer.WriteLine("  ");
115            writer.WriteLine("   False");
116            writer.WriteLine("   False");
117            writer.WriteLine(" 
");
118            writer.WriteLine("
");
119            writer.WriteLine("");
120            writer.Close();
121            Response.Write("");
122        }
123        catch (Exception ex)
124        {
125            Response.Write("");
126        }
127    }
128}
129文章转载:http://blog.csdn.net/chengking/archive/2006/08/10/1045853.aspx

本文作者:天高云淡 来源:http://www.cnblogs.com/morningwang/archive/2007/05
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读