简体   繁体   English

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

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

I'm creating an Excel spreadsheet programmatically with PowerTools.我正在使用 PowerTools 以编程方式创建 Excel 电子表格。

I installed PowerTools 4.5.3.2 in my C#/ASP.Net Core project with Nuget.我使用 Nuget 在我的 C#/ASP.Net Core 项目中安装了 PowerTools 4.5.3.2。 The project also uses DocumentFormat.OpenXml v2.9.1.该项目还使用 DocumentFormat.OpenXml v2.9.1。

I can generate the spreadsheet OK.我可以生成电子表格了。

BUT...但...

I would like to pass the completed spreadsheet back to the user as a.Net MemoryStream.我想将完成的电子表格作为 .Net MemoryStream 传回给用户。

I've pored over the PowerTools documentation and sample programs on https://github.com/OfficeDev/Open-Xml-PowerTools .我仔细研究了https://github.com/OfficeDev/Open-Xml-PowerTools上的 PowerTools 文档和示例程序。

But for the life of me, I haven't seen any way to have PowerTools give me the generated data as a MemoryStream.但在我的一生中,我还没有看到任何方法让 PowerTools 将生成的数据作为 MemoryStream 提供给我。 Or, for that matter, any way to make PowerTools use a MemoryStream I pass in to it.或者,就此而言,让 PowerTools 使用我传递给它的 MemoryStream 的任何方式。

Q: Any suggestions?问:有什么建议吗?

NOTE笔记

All I want to do is:我想做的就是:

  1. My ASP.Net web controller calls a function that generates an.xlsx spreadsheet from scratch.我的 ASP.Net web controller 调用 function 从头开始生成一个 .xlsx 电子表格。

  2. The function uses OpenXML PowerTools (because that seems the most straightforward way to include a Pivot Table in the.xlsx) 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. Once the function returns, the controller needs to return the.xlsx back to the web client as an Asp.Net Core FileStreamResult object.

In other words, all I want to do is go from 2 (working) to 3 (that's where I'm stuck).换句话说,我想做的就是 go 从 2(工作)到 3(这就是我卡住的地方)。 I can't seem to "convert" the successfully created spreadsheet into a MVC FileStreamResult compatible object for the controller to pass back to the caller.我似乎无法将成功创建的电子表格“转换”为与 MVC FileStreamResult 兼容的 object,以便 controller 传回给调用者。

Help!帮助!

No, it doesn't appear that OpenXml PowerTools will let you write out to anything but an actual disk file.不,OpenXml PowerTools 似乎不会让您写入除实际磁盘文件之外的任何内容。

So I used a temp file, then converted it to a byte[] array to pass back to the caller:所以我使用了一个临时文件,然后将其转换为 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: I never did solve this problem: OpenXML SpreadsheetDocument SaveAs() giving file-in-use error PS:我从来没有解决过这个问题: OpenXML SpreadsheetDocument SaveAs()giving file-in-use error

I ultimately gave up on PowerTools and went back to an all-OpenXml solution.我最终放弃了 PowerTools,转而使用全 OpenXml 解决方案。

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

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