Sending an email attachment in memory using OpenXML -
I have an Excel file that was created using OpenXML 2 and I want to send it as an email attachment I am E.g.
System.IO.MemoryStream Stream = New System.IO.MemoryStream (); Spreadsheet document package = spreadsheet document. Create (stream, spreadsheet document type workbook)) AddParts (package);
To save the spreadsheet in the template file using the new system.flowstream (@ "c: Test.xlsx ", System.IO.FileMode.Create));
Works fine but the attempt to send the stream as an email attachment fails - when I do, just attach an empty file to the email
System.Net.Mail.Attachment file = new System.Net.Mail. Attachment (stream, "mobilebill.xlsx", "app / vnd.ms-excel");
How to do this?
OK, I have got this job, despite some efforts. To create a stream:
MemoryStream Stream = new memorystream (); (Spreadsheet document package = spreadsheet document. CREATE (stream, spreadsheet document type workbook)) using {Excel.CreateSpreadsheet (package, Excel_Methods.CreateSpotQuoteOut (), true); } Stream.Seek (0, SeekOrigin.Begin); System.Net.Mail.Attachment Attached = New System.Net.Mail.Attachment (Stream, "Spreadsheet.xlsx"); Attach. Resource Disposition. CreationDate = DateTime.Now; Attach. Modification Date = DateTime.Now; Attach.ContentDisposition.Inline = false; Attach.ContentDisposition.Size = stream.Length; Attach.ContentType.MediaType = "application / vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Apart from this, I came to know that I was not being sent right after making them, and the reason for this "standalone = Yes" was not being added to the XML declaration of all pages, So in my AddParts function, after adding parts, I passed them in this function: Private static Zero AddXMLStandalone (OpenXmlPart part) {System.IO.StreamWriter Author = New System. IO StreamWriter (part.GetStream ()); XmlDocument Document = New XmlDocument (); Doc.Load (part.GetStream ()); Doc.InnerXml = doc.InnerXml.Substring (doc.InnerXml.IndexOf ("? & Gt; + 2); Doc.InnerXml =" & lt ;? Xml version = \ "1.0 \" encoding = \ "UTF-8 \ "Standalone = \" yes \ "? Gt;" + doc.innerXml; part.GetStream () SetLength (doc.InnerXml.Length) .doc.Save (author); writer.Flush (); writer.Close );}
Good luck!
Comments
Post a Comment