打开使用EPPlus生成的Excel文件时显示错误对话框

时间:2022-10-24 10:20:55

I am creating an Excel file using the EPPlus library. When I create file and open up the file, the following pop up message shows:

我正在使用EPPlus库创建一个Excel文件。当我创建文件并打开文件时,以下弹出消息显示:

We found a problem with some content in 'ExcelDemo.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, Click Yes

我们发现“ExcelDemo.xlsx”中的某些内容存在问题。你想让我们尽可能多地恢复吗?如果您信任此工作簿的来源,请单击“是”

I am using following code

我正在使用以下代码

using (ExcelPackage pck = new ExcelPackage())
{
    //Create the worksheet
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");

    //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
    ws.Cells[1, 2].Value = "Excel Download";

    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx");
    Response.BinaryWrite(pck.GetAsByteArray());
}

Is there problem in my code or is this an Excel issue?

我的代码中是否存在问题或者这是Excel问题?

3 个解决方案

#1


21  

At the start, you need to add in a:

一开始,您需要添加:

Response.Clear();

Then at the end add a

然后在最后添加一个

Response.End();

#2


2  

I will share my solution. I am using a template excel file and then create new excel from it.

我将分享我的解决方案。我正在使用模板excel文件,然后从中创建新的Excel。

Receiving the same error. My code was

收到同样的错误。我的代码是

 using (Stream newFileStream = File.Open(this.tempFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
 using (Stream originalFile = File.Open(this.initialFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
 using (ExcelPackage excelPackage = new ExcelPackage(newFile, template))
 {
      // ... Do work here
 }

I had to change code to:

我不得不将代码更改为:

FileInfo intialInfo = new FileInfo(this.initialFilePath);
FileInfo tempFileInfo = new FileInfo(this.tempFilePath);
using (ExcelPackage excelPackage = new ExcelPackage(tempFileInfo, intialInfo))
{
     //... Do work here
}

Also I am using ASP MVC and the response is:

我也使用ASP MVC,响应是:

byte[] result = exporter.GetBytesFromGeneratedExcel();
return this.File(result, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Test.xlsx");

#3


1  

In my case the problem was in calling

在我的情况下问题是在呼叫

package.Save();

and using

和使用

Response.BinaryWrite(package.GetAsByteArray());

at the same time.

与此同时。

When you call package.GetAsByteArray() it perfoms following operations internally:

当你调用package.GetAsByteArray()时,它会在内部执行以下操作:

this.Workbook.Save();
this._package.Close();
this._package.Save(this._stream);

So, calling package.Save two times leads to this error when opening in Excel.

因此,在Excel中打开时,调用package.Save两次会导致此错误。

#1


21  

At the start, you need to add in a:

一开始,您需要添加:

Response.Clear();

Then at the end add a

然后在最后添加一个

Response.End();

#2


2  

I will share my solution. I am using a template excel file and then create new excel from it.

我将分享我的解决方案。我正在使用模板excel文件,然后从中创建新的Excel。

Receiving the same error. My code was

收到同样的错误。我的代码是

 using (Stream newFileStream = File.Open(this.tempFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
 using (Stream originalFile = File.Open(this.initialFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
 using (ExcelPackage excelPackage = new ExcelPackage(newFile, template))
 {
      // ... Do work here
 }

I had to change code to:

我不得不将代码更改为:

FileInfo intialInfo = new FileInfo(this.initialFilePath);
FileInfo tempFileInfo = new FileInfo(this.tempFilePath);
using (ExcelPackage excelPackage = new ExcelPackage(tempFileInfo, intialInfo))
{
     //... Do work here
}

Also I am using ASP MVC and the response is:

我也使用ASP MVC,响应是:

byte[] result = exporter.GetBytesFromGeneratedExcel();
return this.File(result, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Test.xlsx");

#3


1  

In my case the problem was in calling

在我的情况下问题是在呼叫

package.Save();

and using

和使用

Response.BinaryWrite(package.GetAsByteArray());

at the same time.

与此同时。

When you call package.GetAsByteArray() it perfoms following operations internally:

当你调用package.GetAsByteArray()时,它会在内部执行以下操作:

this.Workbook.Save();
this._package.Close();
this._package.Save(this._stream);

So, calling package.Save two times leads to this error when opening in Excel.

因此,在Excel中打开时,调用package.Save两次会导致此错误。