繁体   English   中英

OpenXML PowerTools:是否可以将 Excel 电子表格写入 a.Net MemoryStream?

[英]OpenXML PowerTools: is it possible to write an Excel spreadsheet to a .Net MemoryStream?

我正在使用 PowerTools 以编程方式创建 Excel 电子表格。

我使用 Nuget 在我的 C#/ASP.Net Core 项目中安装了 PowerTools 4.5.3.2。 该项目还使用 DocumentFormat.OpenXml v2.9.1。

我可以生成电子表格了。

但...

我想将完成的电子表格作为 .Net MemoryStream 传回给用户。

我仔细研究了https://github.com/OfficeDev/Open-Xml-PowerTools上的 PowerTools 文档和示例程序。

但在我的一生中,我还没有看到任何方法让 PowerTools 将生成的数据作为 MemoryStream 提供给我。 或者,就此而言,让 PowerTools 使用我传递给它的 MemoryStream 的任何方式。

问:有什么建议吗?

笔记

我想做的就是:

  1. 我的 ASP.Net web controller 调用 function 从头开始生成一个 .xlsx 电子表格。

  2. function 使用 OpenXML PowerTools(因为这似乎是在.xlsx 中包含 Pivot 表的最直接方法)

  3. Once the function returns, the controller needs to return the.xlsx back to the web client as an Asp.Net Core FileStreamResult object.

换句话说,我想做的就是 go 从 2(工作)到 3(这就是我卡住的地方)。 我似乎无法将成功创建的电子表格“转换”为与 MVC FileStreamResult 兼容的 object,以便 controller 传回给调用者。

帮助!

不,OpenXml PowerTools 似乎不会让您写入除实际磁盘文件之外的任何内容。

所以我使用了一个临时文件,然后将其转换为 byte[] 数组以传回给调用者:

using (OpenXmlMemoryStreamDocument streamDoc = OpenXmlMemoryStreamDocument.CreateSpreadsheetDocument())
{
    doc = streamDoc.GetSpreadsheetDocument();
    ms = new MemorySpreadsheet();
    ...
    string tmpFile = System.IO.Path.GetTempFileName();
    streamDoc.GetModifiedSmlDocument().SaveAs(tmpFile);
    byte[] fileBytes = System.IO.File.ReadAllBytes(tmpFile);
    return fileBytes;

PS:我从来没有解决过这个问题: OpenXML SpreadsheetDocument SaveAs()giving file-in-use error

我最终放弃了 PowerTools,转而使用全 OpenXml 解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM