首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EPP加上Excel作为附件

EPP加上Excel作为附件
EN

Stack Overflow用户
提问于 2013-03-16 15:19:53
回答 1查看 3K关注 0票数 3

您好,我正在使用EPPPlus返回一个excel文件,也要发送相同的文件作为附件。我可以打开excel文件,也可以得到附件,但当我打开电子邮件附件时,我收到错误消息说文件已损坏。请看下面的代码,并建议我修改。

代码语言:javascript
复制
 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();
EN

回答 1

Stack Overflow用户

发布于 2013-10-01 00:22:57

我还没有对此进行测试,但我猜您需要在创建outlook附件之前保存ExcelPackage。类似于:

代码语言:javascript
复制
// ...
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);
// ...

因为它从未被保存过,所以它不会向输出流中写入任何内容。空流将导致格式不正确的xlsx文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15446666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档