【问题标题】:How do you stream an Excel 2007 or Word 2007 file using asp.net and c#如何使用 asp.net 和 c# 流式传输 Excel 2007 或 Word 2007 文件
【发布时间】:2010-03-25 20:14:25
【问题描述】:

我正在开发一个网络应用程序,需要流式传输各种文件。我可以处理 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);

我得到的错误是:

在文本内容中发现无效字符。错误处理资源'http://DomainName/GetFile.aspx... PK

有什么建议吗?

【问题讨论】:

    标签: c# asp.net excel ms-word


    【解决方案1】:

    根据简短的网络搜索,word 和 excel 的正确 mime 类型是:

    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 表单(无论如何这更适合这样的事情)。

    要对其进行测试,请确保在应用程序的顶级文件夹中有一个名为 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);
            }
        }
    
    }
    

    【讨论】:

    • 试过这个,稍微好一点。我至少得到了打开/保存/取消对话框。但是,如果我在 docx 文件上单击打开,我只会得到一个空白的 html 页面。如果我单击对话框上的保存,将其保存到文件,然后打开它,我会单击一些错误消息,然后获取正确的文件。对于 xlsx 文件,如果我保存然后打开,我会得到与 docx 相同的内容,即一些错误消息然后它会打开。但是,如果我在 xlsx 文件上单击打开,我必须单击一些错误,然后打开 excel,但我没有数据,而是有我的登录页面
    • 做到了。为什么它会这样工作而不是其他方式我无法理解,但我现在太忙了,无暇顾及。
    • 在 context.Response 上还有一个方便的方法,名为 WriteFile,可以让您避免手动复制文件流。
    • 还有Response.BinaryWrite(),可以填写File.ReadAllByte(filePath)。允许我跳过 CopyStream 循环。
    【解决方案2】:
    .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
    

    【讨论】:

    【解决方案3】:

    对于我们希望使用 Excel 打开的 csv 格式文件,我们使用:Response.ContentType = "application/msexcel";

    也许我们侥幸逃脱,因为它不是真正的 xls 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-08
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      相关资源
      最近更新 更多