2015-04-27 20 views
9

Có thể tạo và chỉnh sửa tài liệu excel bằng SDK OpenXML mà không cần tạo tệp cục bộ không?Cách tạo tệp Excel bằng OpenXML mà không cần tạo tệp cục bộ?

Theo tài liệu, phương thức "Tạo" yêu cầu tệp filepath, tạo bản sao cục bộ của tệp.

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

tôi đề cập bài viết MSDN ở đây: https://msdn.microsoft.com/en-us/library/office/ff478153.aspx

Yêu cầu của tôi là để tạo ra các tập tin, và nó sẽ được tải bởi trình duyệt trên nhấp chuột vào một nút.

Bất kỳ đề xuất nào?

Trả lời

18

Bạn có thể sử dụng sự quá tải của SpreadsheetDocument.Create mà phải mất một Stream và vượt qua nó một 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"); 

Lưu ý rằng theo this StackOverflow question bạn không cần phải vứt bỏ Stream vì nó sẽ được thực hiện cho bạn bằng cách lớp FileStreamResult.

+0

Cảm ơn petelids. Nó đã làm việc –

Các vấn đề liên quan