using System.IO;
private void Button2_Click(object sender, System.EventArgs e)
{
GetData();
initDataGrid();
string phyPath=Server.MapPath("./");
// phyPath=phyPath.Substring(1);
// Response.Write("<script language=javascript>alert('"+phyPath+"');</script>");
// return;
if (this.ds.Tables[0].Rows.Count<=0)
{
Response.Write("<script language=javascript>alert('无记录!');</script>");
return ;
}
this.ds.WriteXml(phyPath+"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.xml",XmlWriteMode.WriteSchema);
Response.Write("<script language=javascript>alert('export success');</script>");
GetExcel(phyPath+"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.xml",this.Page);
}
public static void GetExcel(string filename,Page p1)
{
string t_name=filename.Substring(filename.LastIndexOf(@"/")+1);
p1.Response.Clear();
p1.Response.Buffer= true;
p1.Response.Charset="gb2312";
p1.Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(t_name));
p1.Response.ContentEncoding=System.Text.Encoding.GetEncoding("gb2312");
p1.Response.ContentType = "application/ms-excel";
p1.EnableViewState = false;
FileInfo fn=new FileInfo(filename);
FileStream fs=fn.OpenRead();
byte[] buffer=new byte[fn.Length];
fs.Read(buffer,0,buffer.Length);
fs.Close();
p1.Response.BinaryWrite(buffer);
p1.Response.End();
}
本文介绍了一个使用 C# 实现的功能,该功能能够将数据导出为 XML 文件,并进一步将 XML 文件转换为 Excel 文件供用户下载。文中包含了具体的代码实现细节,包括如何处理文件路径、写入 XML 数据及生成 Excel 文件。
2731

被折叠的 条评论
为什么被折叠?



