如何使用OpenXML创建Excel文件而不创建本地文件? [英] How to create Excel file using OpenXML without creating a local file?

查看:239
本文介绍了如何使用OpenXML创建Excel文件而不创建本地文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用OpenXML SDK创建和编辑Excel文档,而不创建本地文件?

Is it possible to create and edit an excel document using OpenXML SDK without creating a local file?

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

As per the documentation the "Create" method demands for a filepath, which creates a local copy of the file.

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

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

I'm referring the MSDN article here: https://msdn.microsoft.com/en-us/library/office/ff478153.aspx

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

My requirement is to create the file, and it should be downloaded by the browser on clicking a button.

任何建议?

推荐答案

您可以使用 SpreadsheetDocument.Create 需要一个 Stream 并传递一个 MemoryStream

You could use the overload of SpreadsheetDocument.Create that takes a Stream and pass it a 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");

请注意,根据,你不需要处理Stream,因为它将被完成,我该如何处理我的filestream-when-implementation-a-file-download-in-asp-net为您通过 FileStreamResult 类。

这篇关于如何使用OpenXML创建Excel文件而不创建本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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