2012-10-11 34 views
16

Tôi đang tạo ứng dụng bảng điều khiển chuyển dữ liệu vào tệp excel (sử dụng thư viện EPPlus). Tôi đang lưu ExcelPackage làm MemoryStream và tôi muốn đính kèm nó vào một email. Tuy nhiên, khi tôi nhận được email, tệp Excel trống - 0 byte.EPPlus với MemoryStream dưới dạng tệp đính kèm email - tệp rỗng

Suy nghĩ?

 MemoryStream outputStream = new MemoryStream(); 
     using (ExcelPackage package = new ExcelPackage(outputStream)) { 

       // export each facility's rollup and detail to tabs in Excel (two tabs per facility) 
       ExcelWorksheet facilityWorksheet = package.Workbook.Worksheets.Add(row["facility_id"].ToString()); 
       ExcelWorksheet facilityDetail = package.Workbook.Worksheets.Add(row["facility_id"].ToString() + "-detail"); 

       facilityWorksheet.Cells.LoadFromDataTable(rollupData, true); 
       facilityDetail.Cells.LoadFromDataTable(rawExceptions, true); 

       package.Save(); 
     } 

Dưới đây là đoạn code để tạo tập tin đính kèm email:

Attachment attachment = new Attachment(outputStream, "ECO_exceptions.xlsx", "application/vnd.ms-excel"); 
+1

Cảm ơn bạn, bài đăng hữu ích, nếu không gặp nhiều rắc rối, vui lòng chỉnh sửa cho gói đó.Đăng(); đang ở phần SỬ DỤNG. –

Trả lời

24

Sau khi một số tìm kiếm nhiều hơn, tôi tìm thấy giải pháp. Rõ ràng tôi cần thiết phải thiết lập rõ ràng vị trí bắt đầu của MemoryStream trước khi tôi chuyển nó thành một phần đính kèm. Dòng mã sau đã thực hiện thủ thuật:

outputStream.Position = 0; 
+0

Đã lưu ngày của tôi. Cảm ơn rất nhiều! –

+0

tyvm để chia sẻ :) +1 – Christos

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