【问题标题】:Trouble saving excel file through C#无法通过 C# 保存 excel 文件
【发布时间】:2019-09-25 08:02:37
【问题描述】:

我使用 C# 制作了一个 API,并使用 is 作为我的 Angular 项目的后端。我的任务是通过一个以角度制作的按钮将数据保存为我的页面中的 excel 文件。逻辑在后端。我已经尝试了一切来实现这一点,但似乎没有任何效果。我现在正在使用 EPPlus,使用 SaveAs 方法,将 excel 包转换为字节数组。

这是我在 C# 中的功能

public IHttpActionResult CreateExcelDocument()
    {
        //Creates a blank workbook. Using statement disposes the package.
        using (var p = new ExcelPackage())
        {
            var result = new MemoryStream();

            List<Hero> heroes = new List<Hero>();
            var documents = collection.Find(new BsonDocument()).ToList();
            foreach (BsonDocument docum in documents)
            {
                heroes.Add(BsonSerializer.Deserialize<Hero>(docum));
            }

            //Adding worksheet
            var ws = p.Workbook.Worksheets.Add("Heroes");

            //To set values in the spreadsheet use the Cells indexer.
            ws.Cells["A1"].Value = "ID";
            ws.Cells["B1"].Value = "Name";

            for(var row = 1; row <= heroes.Count(); row++)
            {
                ws.Cells[row, 1].LoadFromCollection(heroes[row].Id);
                ws.Cells[row, 2].LoadFromCollection(heroes[row].Name);
            }

            var stream = new MemoryStream(p.GetAsByteArray());

            //string filePath = @"c:\workbooks\myworkbook.xlsx";
            //FileInfo template = new FileInfo(filePath);
            //MemoryStream outputStream = new MemoryStream();
            p.SaveAs(stream);

            return Ok(stream);
        }
    }

这是如何获得它的角度

saveDocument(): Observable<any> {
return this.http.get<any>(this.heroesUrl)
  .pipe(
    tap(p => this.log('saved excel document' + p)),
    catchError(this.handleError<any>('saveDocument', []))
  );
}

我创建了一个消息组件来显示我的 API 请求的状态,因此使用 saveDocument 我得到了这个: HeroService: saved excel document[object Object],[object Object],[object Object],[object Object] ,[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象]

我正在使用 MongoDB、Angular 8 和 ASP.NET Web API 2 (C#)

编辑:这里是角度按钮

<button (click)="saveDocument()"> Save heroes </button>

【问题讨论】:

    标签: c# excel angular mongodb asp.net-web-api


    【解决方案1】:

    如果你已经有了字节数组,为什么不直接使用呢? SaveAs 命令仅适用于给定的 Workbook 对象,您可以在此处查看 (https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.workbook.saveas?view=vsto-2017)

    File.WriteAllBytes(filename, stream);
    

    编辑: 答案取自这里并适应您的问题:Save as using EPPlus?

    byte[] data = p.GetAsByteArray();
    
    string path = @"C:\test1.xlsx";
    File.WriteAllBytes(path, data);
    

    【讨论】:

    • 好的,我稍微改变了代码,只使用了字节数组,但我仍然看不到文件。这就是我添加 string filePath = @"c:\workbooks\myworkbook.xlsx"; File.WriteAllBytes(filePath, bin); 并更改 Ok 以返回 bin 的方式。
    • 嗯,OP 看起来不像是在使用 Microsoft Excel 库,你们说的是同一件事吗? OP 正在使用 EPPlus,这个答案依赖于 Microsoft Excel 互操作 - 它们是不同的。
    • 流可能是您的问题,或者包对象可能已经被释放。尝试编辑后的代码,如果它可以工作,那么问题将是由于流对象,如果它仍然不能工作,那么你的包对象可能已经被释放了。在链接的答案中查看 Han 的答案。
    猜你喜欢
    • 2013-08-30
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2022-08-22
    • 2021-05-20
    • 2021-11-21
    • 1970-01-01
    相关资源
    最近更新 更多