programing

asp.net 및 c#을 사용하여 Excel 2007 또는 Word 2007 파일을 스트리밍하는 방법

javajsp 2023. 9. 21. 20:05

asp.net 및 c#을 사용하여 Excel 2007 또는 Word 2007 파일을 스트리밍하는 방법

저는 웹 앱 작업을 하고 있는데 다양한 파일을 스트리밍해야 합니다.저는 pdf, 이미지, 오래된 오피스 문서를 할 수 있습니다.그런데 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

좋은 의견이라도 있나?

간단한 웹 검색에 따르면 단어와 엑셀의 정확한 마임 유형은 다음과 같습니다.

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/

편집:

다음과 같은 단순화된 샘플이 저에게 적합합니다.웹 양식 대신 일반 핸들러를 사용한다는 점에서 여러분의 것과는 다릅니다.

테스트하기 위해 응용프로그램의 최상위 폴더에 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 파일이 아니기 때문에 해결할 수 있을 것입니다.

언급URL : https://stackoverflow.com/questions/2519026/how-do-you-stream-an-excel-2007-or-word-2007-file-using-asp-net-and-c-sharp