如何使用asp.net和c#stream式传输Excel 2007或Word 2007文件
我正在做一个networking应用程序,需要stream式传输各种文件。 我可以做PDF,图像和较旧的Office文档。 但是,当我试图用2007年的文件,它打破。 这是我的代码:
Response.Buffer = true; Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); switch (FileExtension.ToLower()) { case "pdf": Response.ContentType = "application/pdf"; break; case "doc": Response.ContentType = "application/msword"; break; case "docx": Response.ContentType = "application/vnd.ms-word.document.12"; break; case "xls": Response.ContentType = "application/vnd.ms-excel"; break; case "xlsx": Response.ContentType = "application/vnd.ms-excel.12"; break; default: Response.ContentType = "image/jpeg"; break; } Response.BinaryWrite(buffer);
我得到的错误是:
在文本内容中find无效的字符。 处理资源'http://DomainName/GetFile.aspx时出错... PK
有什么build议么?
根据简单的网页search,Word和Excel的正确的MIMEtypes是:
application/vnd.openxmlformats-officedocument.wordprocessingml.document application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
http://www.bram.us/2007/05/25/office-2007-mime-types-for-iis/
编辑:
以下简化示例适用于我。 它和你的不同之处在于它使用了一个通用的处理程序,而不是一个web表单(这对于类似的东西来说更适合)。
要testing它,请确保在应用程序的顶级文件夹中有一个名为Book1.xlsx的Excel 2007文件。
DownloadSpreadsheet.ashx: <%@ WebHandler Language="C#" Class="DownloadSpreadsheetHandler" %> using System; using System.Web; using System.IO; public class DownloadSpreadsheetHandler: IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; string path = context.Server.MapPath("~/Book1.xlsx"); using (FileStream spreadsheet = File.OpenRead(path)) { CopyStream(spreadsheet, context.Response.OutputStream); } } public bool IsReusable { get { return false; } } private static void CopyStream(Stream input, Stream output) { byte[] buffer = new byte[32768]; while (true) { int read = input.Read(buffer, 0, buffer.Length); if (read <= 0) return; output.Write(buffer, 0, read); } } }
.doc application/msword .dot application/msword .docx application/vnd.openxmlformats-officedocument.wordprocessingml.document .dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template .docm application/vnd.ms-word.document.macroEnabled.12 .dotm application/vnd.ms-word.template.macroEnabled.12 .xls application/vnd.ms-excel .xlt application/vnd.ms-excel .xla application/vnd.ms-excel .xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet .xltx application/vnd.openxmlformats-officedocument.spreadsheetml.template .xlsm application/vnd.ms-excel.sheet.macroEnabled.12 .xltm application/vnd.ms-excel.template.macroEnabled.12 .xlam application/vnd.ms-excel.addin.macroEnabled.12 .xlsb application/vnd.ms-excel.sheet.binary.macroEnabled.12 .ppt application/vnd.ms-powerpoint .pot application/vnd.ms-powerpoint .pps application/vnd.ms-powerpoint .ppa application/vnd.ms-powerpoint .pptx application/vnd.openxmlformats-officedocument.presentationml.presentation .potx application/vnd.openxmlformats-officedocument.presentationml.template .ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow .ppam application/vnd.ms-powerpoint.addin.macroEnabled.12 .pptm application/vnd.ms-powerpoint.presentation.macroEnabled.12 .potm application/vnd.ms-powerpoint.presentation.macroEnabled.12 .ppsm application/vnd.ms-powerpoint.slideshow.macroEnabled.12
对于我们希望用Excel打开的csv格式文件,我们使用: Response.ContentType = "application/msexcel";
也许我们逃避这一点,因为它不是一个真正的xls文件,但是。
- 在AngularJS中保存来自API控制器的Excel文件
- 用于Excel XML的MIMEtypes(ASP.NET 3.5)
- 使用ASP.net中的macros编辑Excel文档
- 改变OleDb性能?
- C# – 将上传的Excel文件保存到MemoryStream中,然后用它填充DataTable
- Excel单元格中的HTML数据。 Excel更改值,导致数据库无效导入
- NPOI Excel数字格式不显示在asp.net中的Excel工作表中
- 从.NET代码中导出Excel数据时,引发了“System.OutOfMemoryException”
- 如何通过oledb阅读器或excel库,excel datareader或NPOI等(Interop除外)如何检查一个单元格包含公式或不在Excel中?