Well, I got this job, albeit with some effort. To create a stream:
MemoryStream stream = new MemoryStream(); using (SpreadsheetDocument package = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook)) { Excel.CreateSpreadsheet(package, Excel_Methods.CreateSpotQuoteOut(), true); } stream.Seek(0, SeekOrigin.Begin); System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(stream, "spreadsheet.xlsx"); attach.ContentDisposition.CreationDate = DateTime.Now; attach.ContentDisposition.ModificationDate = DateTime.Now; attach.ContentDisposition.Inline = false; attach.ContentDisposition.Size = stream.Length; attach.ContentType.MediaType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
In addition, I found that mine weren’t sent right after I created them, and the reason for this is that “standalone = yes” was not added to the xml declaration of all pages, so in my AddParts function after adding the parts, I passed them to this function:
private static void AddXMLStandalone(OpenXmlPart part) { System.IO.StreamWriter writer = new System.IO.StreamWriter(part.GetStream()); XmlDocument doc = new XmlDocument(); doc.Load(part.GetStream()); doc.InnerXml = doc.InnerXml.Substring(doc.InnerXml.IndexOf("?>") + 2); doc.InnerXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + doc.InnerXml; part.GetStream().SetLength(doc.InnerXml.Length); doc.Save(writer); writer.Flush(); writer.Close(); }
Good luck
source share