2013-03-16 2 views
2

Привет Я использую EPPPlus для возврата файла excel, а также для отправки того же файла, что и вложение. Я мог бы открыть файл excel, а также получить прикрепление, но когда я открою вложение электронной почты, я получаю сообщение об ошибке, говорящее, что файл поврежден. Пожалуйста, посмотрите на код belwo и предложите мне изменения.EPP plus Excel как приложение

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("[email protected]", "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(); 
+0

Любые предложения, пожалуйста? – user2132124

ответ

1

Я не проверял, но я думаю, что вам нужно, чтобы сохранить ExcelPackage, прежде чем создать приложение Outlook. Что-то вроде:

// ... 
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.