ASP.NET MVC EPPlus下载Excel文件 [英] ASP.NET MVC EPPlus Download Excel File

查看:707
本文介绍了ASP.NET MVC EPPlus下载Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用花哨的EPPlus库写一个Excel文件,并输出到用户下载。对于下面的方法,我只是用一些测试数据,以在code最小化,然后我会添加我使用后连接到数据库中的code。现在,我可以下载一个文件都很好,但是当我去打开文件,Excel的抱怨,这不是一个有效的文件,可能已损坏。当我去看看这个文件,它说这是0KB大。所以我的问题是,我要去哪里错了?我假设它与MemoryStream的。之前,所以我不完全知道用在这里什么都没有做多少工作与流。任何帮助将是AP preciated!

  [授权]
公众的ActionResult Download_PERS936AB()
{
    ExcelPackage PCK =新ExcelPackage();
    变种WS = pck.Workbook.Worksheets.Add(样品1);    ws.Cells [A1] =价值样品1。
    。ws.Cells [A1] = Style.Font.Bold真实;
    VAR形状= ws.Drawings.AddShape(Shape1,eShapeStyle.Rect);
    shape.SetPosition(50,200);
    shape.SetSize(200,100);
    shape.Text =样品1文字文字文字;    VAR的MemoryStream =新的MemoryStream();
    pck.SaveAs(MemoryStream的);
    返回新FileStreamResult(MemoryStream的应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet){FileDownloadName =PERS936AB.xlsx};
}


解决方案

下面就是我使用的是什么 - 我一直在使用这几个月,还没有一个问题:

 公众的ActionResult ChargeSummaryData(ChargeSummaryRptParams rptParams)
    {
        VAR fileDownloadName =sample.xlsx;
        VAR的contentType =应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet        VAR包= CreatePivotTable(rptParams);        VAR FILESTREAM =新的MemoryStream();
        package.SaveAs(FILESTREAM);
        fileStream.Position = 0;        VAR FSR =新FileStreamResult(FILESTREAM,则contentType);
        fsr.FileDownloadName = fileDownloadName;        返回FSR;
    }

一件事我注意到马上蝙蝠是,你不重置文件流位置返回到0。

So I'm using the fancy EPPlus library to write an Excel file and output it to the user to download. For the following method I'm just using some test data to minimize on the code, then I'll add the code I'm using to connect to database later. Now I can download a file all fine, but when I go to open the file, Excel complains that it's not a valid file and might be corrupted. When I go to look at the file, it says it's 0KB big. So my question is, where am I going wrong? I'm assuming it's with the MemoryStream. Haven't done much work with streams before so I'm not exactly sure what to use here. Any help would be appreciated!

[Authorize]
public ActionResult Download_PERS936AB()
{
    ExcelPackage pck = new ExcelPackage();
    var ws = pck.Workbook.Worksheets.Add("Sample1");

    ws.Cells["A1"].Value = "Sample 1";
    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 1 text text text";

    var memorystream = new MemoryStream();
    pck.SaveAs(memorystream);
    return new FileStreamResult(memorystream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "PERS936AB.xlsx" };
}

解决方案

Here's what I'm using - I've been using this for several months now and haven't had an issue:

    public ActionResult ChargeSummaryData(ChargeSummaryRptParams rptParams)
    {
        var fileDownloadName = "sample.xlsx";
        var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        var package = CreatePivotTable(rptParams);

        var fileStream = new MemoryStream();
        package.SaveAs(fileStream);
        fileStream.Position = 0;

        var fsr = new FileStreamResult(fileStream, contentType);
        fsr.FileDownloadName = fileDownloadName;

        return fsr;
    }

One thing I noticed right off the bat is that you don't reset your file stream position back to 0.

这篇关于ASP.NET MVC EPPlus下载Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆