EP Plus를 사용하여 Excel 파일을 생성하지 못했습니다.
EP Plus를 사용하여 Excel 파일을 생성하려고 하면 Excel에서 다음 오류 메시지를 표시합니다.
Excel에서 '내 파일 이름' 파일을 열 수 없습니다.파일 형식 또는 파일 확장명이 잘못되었기 때문에 xlsx'입니다.파일이 손상되지 않았는지, 파일 확장명이 파일 형식과 일치하는지 확인합니다.
내 코드는 다음과 같습니다.
public ActionResult Index()
{
using (ExcelPackage package = new ExcelPackage())
{
// I populate the worksheet here. I'm 90% sure this is fine
// because the stream file size changes based on what I pass to it.
var stream = new MemoryStream();
package.SaveAs(stream);
string fileName = "myfilename.xlsx";
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var cd = new System.Net.Mime.ContentDisposition
{
Inline = false,
FileName = fileName
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(stream, contentType, fileName);
}
}
내가 뭘 잘못했는지 알아요?
스트림 위치를 재설정하기만 하면 됩니다.stream.Position = 0;
응답에 직접 편지를 써서는 안 됩니다. MVC 방식이 아닙니다.올바른 MVC 파이프라인을 따르지 않으며 컨트롤러 작업 코드를 응답 개체에 단단히 연결합니다.
에서 파일 이름을 세 번째 매개 변수로 추가하는 경우File()
MVC가 자동으로 올바른 항목을 추가합니다.Content-Disposition
머리글...수동으로 추가할 필요가 없습니다.
요약하자면, 이것이 당신이 원하는 것입니다.
public ActionResult Index()
{
using (ExcelPackage package = new ExcelPackage())
{
// I populate the worksheet here. I'm 90% sure this is fine
// because the stream file size changes based on what I pass to it.
var stream = new MemoryStream();
package.SaveAs(stream);
string fileName = "myfilename.xlsx";
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
stream.Position = 0;
return File(stream, contentType, fileName);
}
}
코드가 표시되지 않습니다.stream
에 기록됨HttpResponse
아마도 그 안에서 행해지고 있을 것입니다.File
당신이 게시하지 않은 메소드.
작동하는 한 가지 방법은 다음과 같습니다.
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader(
"content-disposition", String.Format(CultureInfo.InvariantCulture, "attachment; filename={0}", fileName));
Response.BinaryWrite(package.GetAsByteArray());
Response.End();
Joe의 대답과 비슷하게, 나는 여전히 전화를 해야 했습니다.Response.ClearHeaders()
:
protected void btnDownload_Click(object sender, EventArgs e)
{
ExcelPackage pck = new ExcelPackage();
var ws = pck.Workbook.Worksheets.Add("Sample2");
ws.Cells["A1"].Value = "Sample 2";
ws.Cells["A1"].Style.Font.Bold = true;
var shape = ws.Drawings.AddShape("Shape1", eShapeStyle.Rect);
shape.SetPosition(50, 200);
shape.SetSize(200, 100);
shape.Text = "Sample 2 outputs the sheet using the Response.BinaryWrite method";
Response.Clear();
Response.ClearHeaders();
Response.BinaryWrite(pck.GetAsByteArray());
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=Sample2.xlsx");
Response.End();
}
언급URL : https://stackoverflow.com/questions/9608547/generating-an-excel-file-with-epplus-is-failing
'source' 카테고리의 다른 글
[NgStyle] 조건과 결합(그렇지 않은 경우) (0) | 2023.09.04 |
---|---|
단순 SELECT 쿼리가 큰 테이블에서 느림 (0) | 2023.08.30 |
프로시저가 컴파일되지 않음 (0) | 2023.08.30 |
응용 프로그램 메모리의 테이블을 쿼리에 매핑하는 방법 (0) | 2023.08.30 |
사용자 양식에서 동적으로 추가된 컨트롤 제거 (0) | 2023.08.30 |