EPPlus,带有MemoryStream作为电子邮件附件-文件为空 [英] EPPlus with MemoryStream as email attachment -- file is empty

查看:63
本文介绍了EPPlus,带有MemoryStream作为电子邮件附件-文件为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个控制台应用程序,该应用程序将数据移动到excel文件中(使用EPPlus库).我将ExcelPackage保存为MemoryStream,并且要将其附加到电子邮件中.但是,当我收到电子邮件时,Excel文件为空-0个字节.

I'm building a console app that moves data into an excel file (using EPPlus library). I'm saving the ExcelPackage as a MemoryStream, and I want to attach it to an email. However, when I receive the email, the Excel file is empty -- 0 bytes.

有想法吗?

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

以下是用于创建电子邮件附件的代码:

Here's the code for creating the email attachment:

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

推荐答案

经过更多搜索后,我找到了解决方案.显然,在将附件作为附件传递之前,我需要显式设置MemoryStream的起始位置.下面的代码行达到了目的:

After some more searching, I found the solution. Apparently I needed to explicitly set the starting position of the MemoryStream before I passed it in as an attachment. The following line of code did the trick:

outputStream.Position = 0;

这篇关于EPPlus,带有MemoryStream作为电子邮件附件-文件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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