Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EPP plus Excel作为附件_C#_Excel_Email_Attachment_Epplus - Fatal编程技术网

C# EPP plus Excel作为附件

C# EPP plus Excel作为附件,c#,excel,email,attachment,epplus,C#,Excel,Email,Attachment,Epplus,您好,我正在使用EPPPlus返回excel文件,并将同一文件作为附件发送。 我可以打开excel文件并获得附件,但当我打开电子邮件附件时,我收到错误消息,说文件已损坏。 请查看belwo代码并向我建议更改 MemoryStream outputStream = new MemoryStream(); using (ExcelPackage pck = new ExcelPackage(outputStream)) {

您好,我正在使用EPPPlus返回excel文件,并将同一文件作为附件发送。 我可以打开excel文件并获得附件,但当我打开电子邮件附件时,我收到错误消息,说文件已损坏。 请查看belwo代码并向我建议更改

 MemoryStream outputStream = new MemoryStream();
                using (ExcelPackage pck = new ExcelPackage(outputStream))
                {
                    //Create the worksheet
                    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");

                    //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
                    ws.Cells["A1"].LoadFromDataTable(tbl, true);

                    //Format the header for column 1-3
                    using (ExcelRange rng = ws.Cells[1, 1, 1, tbl.Columns.Count])
                    {
                        rng.Style.Font.Bold = true;
                        rng.Style.Fill.PatternType = ExcelFillStyle.Solid;                      //Set Pattern for the background to Solid
                        rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));  //Set color to dark blue
                        rng.Style.Font.Color.SetColor(Color.White);
                    }
                    //Example how to Format Column 1 as numeric 
                    using (ExcelRange col = ws.Cells[2, 1, 2 + tbl.Rows.Count, 1])
                    {
                        col.Style.Numberformat.Format = "#,##0.00";
                        col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    }
                    MailMessage mail = null;
                    try
                    {
                        mail = new MailMessage();
                        mail.From = new MailAddress("placescms@adidas-group.com", "Adidas SystemMail");
                        mail.To.Add(new MailAddress(emailAddress));
                        mail.Subject = "Store Finder Geocoding";
                        mail.IsBodyHtml = true;
                        string message = "";
                        message += ".LLHere is the attachment with list of stores and geocode values you requested." + "</br>";
                        mail.Body = message;
                        outputStream.Position = 0;
                        Attachment attachment = new Attachment(outputStream, "Geocoding.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                        mail.Attachments.Add(attachment);
                        mail.SubjectEncoding = System.Text.Encoding.UTF8;
                        mail.BodyEncoding = System.Text.Encoding.UTF8;
                        SmtpClient client = new SmtpClient("localhost");
                        client.Send(mail);
                    }
                    catch (Exception ex)
                    {
                        //throw ex;
                    }
                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    Response.AddHeader("content-disposition", String.Format(CultureInfo.InvariantCulture, "attachment; filename={0}", "geo.xlsx"));
                    Response.BinaryWrite(pck.GetAsByteArray());
                    Response.End();
MemoryStream outputStream=new MemoryStream();
使用(ExcelPackage pck=新的ExcelPackage(outputStream))
{
//创建工作表
Excel工作表ws=pck.Workbook.Worksheets.Add(“演示”);
//从单元格A1开始,将数据表加载到工作表中。在第1行打印列名
ws.Cells[“A1”].LoadFromDataTable(tbl,true);
//设置第1-3列标题的格式
使用(ExcelRange rng=ws.Cells[1,1,1,tbl.Columns.Count])
{
rng.Style.Font.Bold=true;
rng.Style.Fill.PatternType=ExcelFillStyle.Solid;//将背景图案设置为Solid
rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79129189));//将颜色设置为深蓝色
rng.Style.Font.Color.SetColor(Color.White);
}
//如何将列1格式化为数字的示例
使用(ExcelRange col=ws.Cells[2,1,2+tbl.Rows.Count,1])
{
col.Style.Numberformat.Format=“#,###0.00”;
col.Style.HorizontalAlignment=ExcelHorizontalAlignment.Right;
}
MailMessage=null;
尝试
{
mail=新邮件消息();
mail.From=新邮件地址(“placescms@adidas-“阿迪达斯系统邮件”);
mail.To.Add(新邮件地址(emailAddress));
mail.Subject=“商店查找器地理编码”;
mail.IsBodyHtml=true;
字符串消息=”;
message+=“.ll这是附件,其中列出了您请求的门店和地理编码值。”+“
”; mail.Body=消息; outputStream.Position=0; Attachment Attachment=新附件(outputStream,“Geocoding.xlsx”,“application/vnd.openxmlformats of icedocument.spreadsheetml.sheet”); mail.Attachments.Add(附件); mail.SubjectEncoding=System.Text.Encoding.UTF8; mail.BodyEncoding=System.Text.Encoding.UTF8; SmtpClient=新的SmtpClient(“本地主机”); 客户端。发送(邮件); } 捕获(例外情况除外) { //掷骰子; } Response.ContentType=“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”; AddHeader(“content disposition”,String.Format(CultureInfo.InvariantCulture,“附件;文件名={0}”,“geo.xlsx”); BinaryWrite(pck.GetAsByteArray()); Response.End();
我还没有对此进行测试,但我猜您需要在创建outlook附件之前保存
ExcelPackage
。类似于:

// ...
mail.Body = message;

pck.Save();

outputStream.Position = 0;
Attachment attachment = new Attachment(outputStream, "Geocoding.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
mail.Attachments.Add(attachment);
// ...

因为它永远不会被保存,所以它不会向输出流写入任何内容。空流会导致文件格式不正确。

请提供任何建议?