How to create Excel file using OpenXML without creating a local file?

血红的双手。 提交于 2019-12-28 05:58:11

问题


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);

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.

Any suggestion?


回答1:


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");

Note that as per this StackOverflow question you don't need to dispose the Stream as it will be done for you by the FileStreamResult class.



来源:https://stackoverflow.com/questions/29887503/how-to-create-excel-file-using-openxml-without-creating-a-local-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!