NPOI Excel文件无效的文件格式或扩展

发布于 2025-02-04 17:48:20 字数 1419 浏览 0 评论 0原文

我正在尝试在.NET Core 6中创建一个应该返回Excel文件的端点。问题是,当我尝试打开Excel文件时,它说文件格式或扩展名无效。

那是我的代码:

控制器

[HttpPost("/api/report")]
public async Task ExportFile([FromBody] ReportDTO reportDTO)
{
    _excelService.GenerateReport(HttpContext, reportDTO);
}

服务

public void GenerateReport(HttpContext httpContext, ReportDTO reportDTO)
{
    IWorkbook workbook;
    workbook = new XSSFWorkbook();
    ISheet excelSheet = workbook.CreateSheet("Test");

    IRow row = excelSheet.CreateRow(0);
    row.CreateCell(0).SetCellValue("Test");
    row.CreateCell(1).SetCellValue("Hello");

    row = excelSheet.CreateRow(1);
    row.CreateCell(0).SetCellValue(1);
    row.CreateCell(1).SetCellValue("World");

    workbook.WriteExcelToResponse(httpContext, GetFileName(reportDTO));
}

扩展方法

public static void WriteExcelToResponse(this IWorkbook book, HttpContext httpContext, string templateName)
{
    var response = httpContext.Response;
    response.ContentType = "application/vnd.ms-excel";
    if (!string.IsNullOrEmpty(templateName))
    {
        var contentDisposition = new Microsoft.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        contentDisposition.SetHttpFileName(templateName);
        response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString();
    }
    book.Write(response.Body);
}

I'm trying to create an Endpoint in .NET Core 6 that should return a Excel File. The Problem is, that when i try to open the Excel File, it says the file format or extension is not valid.

That's my code:

Controller

[HttpPost("/api/report")]
public async Task ExportFile([FromBody] ReportDTO reportDTO)
{
    _excelService.GenerateReport(HttpContext, reportDTO);
}

Service

public void GenerateReport(HttpContext httpContext, ReportDTO reportDTO)
{
    IWorkbook workbook;
    workbook = new XSSFWorkbook();
    ISheet excelSheet = workbook.CreateSheet("Test");

    IRow row = excelSheet.CreateRow(0);
    row.CreateCell(0).SetCellValue("Test");
    row.CreateCell(1).SetCellValue("Hello");

    row = excelSheet.CreateRow(1);
    row.CreateCell(0).SetCellValue(1);
    row.CreateCell(1).SetCellValue("World");

    workbook.WriteExcelToResponse(httpContext, GetFileName(reportDTO));
}

Extension Method

public static void WriteExcelToResponse(this IWorkbook book, HttpContext httpContext, string templateName)
{
    var response = httpContext.Response;
    response.ContentType = "application/vnd.ms-excel";
    if (!string.IsNullOrEmpty(templateName))
    {
        var contentDisposition = new Microsoft.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        contentDisposition.SetHttpFileName(templateName);
        response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString();
    }
    book.Write(response.Body);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抠脚大汉 2025-02-11 17:48:20

根据您的描述,我建议您可以直接使用内存流并使用FileSult返回Excel文件。

更多详细信息,您可以参考以下示例:

    [HttpPost("/api/report")]
    public ActionResult ExportFile(/*[FromBody] ReportDTO reportDTO*/)
    {
        IWorkbook workbook = new XSSFWorkbook();
        ISheet excelSheet = workbook.CreateSheet("Test");

        IRow row = excelSheet.CreateRow(0);
        row.CreateCell(0).SetCellValue("Test");
        row.CreateCell(1).SetCellValue("Hello");

        row = excelSheet.CreateRow(1);
        row.CreateCell(0).SetCellValue(1);
        row.CreateCell(1).SetCellValue("World");


        using (var exportData = new MemoryStream())
        {
            workbook.Write(exportData);
            string saveAsFileName = string.Format("Export-{0:d}.xlsx", DateTime.Now).Replace("/", "-");
            byte[] bytes = exportData.ToArray();
            return File(bytes, "application/vnd.ms-excel", saveAsFileName);
        }

    }

”在此处输入图像描述”

According to your description, I suggest you could directly use memory stream and use Fileresult to return the Excel file.

More details, you could refer to below example:

    [HttpPost("/api/report")]
    public ActionResult ExportFile(/*[FromBody] ReportDTO reportDTO*/)
    {
        IWorkbook workbook = new XSSFWorkbook();
        ISheet excelSheet = workbook.CreateSheet("Test");

        IRow row = excelSheet.CreateRow(0);
        row.CreateCell(0).SetCellValue("Test");
        row.CreateCell(1).SetCellValue("Hello");

        row = excelSheet.CreateRow(1);
        row.CreateCell(0).SetCellValue(1);
        row.CreateCell(1).SetCellValue("World");


        using (var exportData = new MemoryStream())
        {
            workbook.Write(exportData);
            string saveAsFileName = string.Format("Export-{0:d}.xlsx", DateTime.Now).Replace("/", "-");
            byte[] bytes = exportData.ToArray();
            return File(bytes, "application/vnd.ms-excel", saveAsFileName);
        }

    }

Result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文