gpt4 book ai didi

c# - 如何在不创建本地文件的情况下使用 OpenXML 创建 Excel 文件?

转载 作者:可可西里 更新时间:2023-11-01 07:43:02 25 4
gpt4 key购买 nike

是否可以在不创建本地文件的情况下使用 OpenXML SDK 创建和编辑 excel 文档?

根据文档,Create 方法需要一个 filepath,它会创建文件的本地副本。

SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);

我在这里指的是 MSDN 文章: https://msdn.microsoft.com/en-us/library/office/ff478153.aspx

我的要求是创建文件,并且应该在点击按钮时由浏览器下载。

有什么建议吗?

最佳答案

您可以使用 SpreadsheetDocument.Create 的重载获取一个 Stream 并传递给它一个 MemoryStream:

MemoryStream memoryStream = new MemoryStream();
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook);

//add the excel contents...

//reset the position to the start of the stream
memoryStream.Seek(0, SeekOrigin.Begin);

return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

请注意,根据 this StackOverflow question您不需要处理 Stream,因为它将由 FileStreamResult 类为您完成。

关于c# - 如何在不创建本地文件的情况下使用 OpenXML 创建 Excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29887503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com