使用Response.ContentType导出到Excel [英] Export to Excel using Response.ContentType
本文介绍了使用Response.ContentType导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们有一个Web应用程序,我们生成一个Excel报告。数据保存在字符串构建器中,然后放入内存流。再次从MemoryStream到OutputStream。该文件将在网页中提示打开/保存对话框。
现在我希望我的excel文件保存到路径而不是弹出网页。
这是我的代码的一部分。请帮忙。
Hi,
We have a web application where we generate an excel report. Data is saved in a string builder and then put Memory Stream. Again from MemoryStream to OutputStream. The file will prompt in the webpage as 'Open/Save dialog box'.
Now i want my excel file to save to a path instead poping up in the web page.
Here is a part of my code. Please help.
public void ExportToExcelByXmlNew()
{
var sb = new StringBuilder();
var sb = new StringBuilder();
sb.AppendFormat("<?xml version=\"1.0\"?><?mso-application progid=\"Excel.Sheet\"?><Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sb.AppendFormat("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\"> <Author>SPI</Author> <LastAuthor>sajjeeva</LastAuthor> <Created>2012-09-13T17:57:30Z</Created> <Version>11.5606</Version> </DocumentProperties>");
sb.AppendFormat("<OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">");
sb.AppendFormat("<AllowPNG/>");
sb.AppendFormat("</OfficeDocumentSettings>");
sb.AppendFormat("<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sb.AppendFormat("<WindowHeight>8190</WindowHeight>");
sb.AppendFormat("<WindowWidth>15195</WindowWidth>");
sb.AppendFormat("<WindowTopX>120</WindowTopX>");
sb.AppendFormat("<WindowTopY>15</WindowTopY>");
sb.AppendFormat("<ProtectStructure>False</ProtectStructure>");
sb.AppendFormat("<ProtectWindows>False</ProtectWindows>");
sb.AppendFormat("</ExcelWorkbook>");
var ms = new MemoryStream();
ms = new MemoryStream();
try
{
ms.Write(Encoding.ASCII.GetBytes(sb.ToString()), 0, sb.Length);
ms.WriteTo(Response.OutputStream);
}
finally
{
ms.Close();
}
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("content-disposition",
"attachment; filename=" + "Store Performance Report.xls");
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
推荐答案
之前我在这样的项目中做了这个(它从gridview中获取数据并导出到文件):
I did this before in a project like this (it ges data from a gridview and exports to file):
protected void exportButton_OnClick(object sender, EventArgs e)
{
string fileName = Server.MapPath(@"~\export.xls");
using (StreamWriter sw = new StreamWriter(fileName))
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
historyGridView.RenderControl(hw);
}
}
FileInfo file = new FileInfo(fileName);
Response.Clear();
// add the header that specifies the default filename for the
// Download/SaveAs dialog
Response.AddHeader("Content-Disposition", "attachment; filename=export.xls");
// add the header that specifies the file size, so that the browser
// can show the download progress
Response.AddHeader("Content-Length", file.Length.ToString());
// specify that the response is a stream that cannot be read by the
// client and must be downloaded
Response.ContentType = "application/vnd.ms-excel";
// send the file stream to the client
Response.WriteFile(fileName);
}
这段代码完全符合您的要求:
This piece of code do exactly what you want:
'configuration
Response.ClearContent()
Response.AppendHeader("content-disposition", "attachment; filename=ListeRue.xls")
Response.ContentType = "application/excel"
'create holders
Dim stringWriter As New StringWriter
Dim htmlTextWriter As New HtmlTextWriter(stringWriter)
'save gridview content in an excel file
GridView4.RenderControl(htmlTextWriter)
Response.Write(stringWriter.ToString())
Response.End()
这篇关于使用Response.ContentType导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文