如何使用OpenXML创buildExcel文件而不创build本地文件?

是否有可能使用OpenXML SDK创build和编辑Excel文档而不创build本地文件?

根据文档“创build”方法需要一个文件path,该文件path创build该文件的本地副本。

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

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

我的要求是创build文件,它应该由浏览器单击button下载。

任何build议?

您可以使用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"); 

请注意,根据此StackOverflow问题,您不需要处置Stream,因为它将由FileStreamResult类完成。