簡體   English   中英

通過ClosedXML.dll打開Excel文件顯示錯誤“ Excel發現不可讀的內容”

[英]Opening Excel File by ClosedXML.dll is showing error “Excel found unreadable content”

嗨,我已經使用ClosedXML.dll創建了一個excel文件,當我要打開該文件時,它顯示“ Excel發現不可讀的內容”消息。 我的文件內容包含瑞典文。 我不知道根本原因是什么? 有什么方法可以設定語言? 如何刪除該警告,請幫助我。 這是代碼捕捉。

using (XLWorkbook wb = new XLWorkbook())
{
    wb.Worksheets.Add(dt);  //dt is DataTable

    Response.Clear();
    Response.ClearHeaders();
    Response.Buffer = true;
    Response.Charset = "";
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment;filename="+fileName+".xlsx");
    Response.ContentEncoding = Encoding.UTF8;
    using (MemoryStream MyMemoryStream = new MemoryStream())
    {
        MyMemoryStream.Capacity = (int)MyMemoryStream.Length;
        wb.SaveAs(MyMemoryStream);
        MyMemoryStream.WriteTo(Response.OutputStream);
        Response.Flush();
        Response.End();
    }
}

經過大量的工作之后,我從msdn博客網站找到了解決方案。 那是

Response.Flush();
Response.SuppressContent = true;

甚至我都不知道SuppressContent屬性的內部作品,但它確實對我有用。 它可能會刪除我的Excel工作表的某些樣式並減小文件的大小。 而且我還刪除了Response.End(),因為它引發了線程異常中止。

您可能忘記了將流的位置設置為0。

System.IO.MemoryStream stream = new System.IO.MemoryStream();
document.SaveAs(stream);
stream.Position = 0;
stream.Close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM