internal void ExportAndZip(Report report, Stream stream) { string tempFolder = Config.GetTempFolder() + Path.GetRandomFileName(); Directory.CreateDirectory(tempFolder); try { Export(report, tempFolder + "/" + GetFileName(report) + GetFileExtension()); ZipArchive zip = new ZipArchive(); zip.AddDir(tempFolder); zip.SaveToStream(stream); } finally { Directory.Delete(tempFolder, true); } }
private void ExportODF(Stream stream) { string s; int fx, fy, dx, dy, Page; string FTempFolder = Path.GetTempPath() + Path.GetRandomFileName(); Directory.CreateDirectory(FTempFolder); Directory.CreateDirectory(FTempFolder + "\\Pictures"); Directory.CreateDirectory(FTempFolder + "\\Thumbnails"); Directory.CreateDirectory(FTempFolder + "\\META-INF"); int PicCount = 0; Page = 0; OdfMakeDocStyles(FTempFolder + "\\styles.xml"); #region Content.xml using (FileStream file = new FileStream(FTempFolder + "\\content.xml", FileMode.Create)) using (StreamWriter Out = new StreamWriter(file)) { Out.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); Out.Write("<office:document-content "); Out.Write(OdfMakeXmlHeader()); Out.WriteLine(">"); Out.WriteLine("<office:scripts/>"); ExportIEMStyle Style; OdfFontFaceDecals(Out); Out.WriteLine("<office:automatic-styles>"); OdfColumnStyles(Out); OdfRowStyles(Out); for (int i = 0; i < FMatrix.StylesCount; i++) { Style = FMatrix.StyleById(i); Out.WriteLine("<style:style style:name=\"ce" + i.ToString() + "\" " + "style:family=\"table-cell\" style:parent-style-name=\"Default\">"); if (FExportType == 0) { Out.Write("<style:text-properties style:font-name=\"" + Style.Font.Name + "\" " + "fo:font-size=\"" + ExportUtils.FloatToString(Style.Font.Size) + "pt\" "); if ((Style.Font.Style & FontStyle.Underline) > 0) { Out.Write("style:text-underline-style=\"solid\" " + "style:text-underline-width=\"auto\" " + "style:text-underline-color=\"font-color\" "); } if ((Style.Font.Style & FontStyle.Italic) > 0) { Out.Write("fo:font-style=\"italic\" "); } if ((Style.Font.Style & FontStyle.Bold) > 0) { Out.Write("fo:font-weight=\"bold\" "); } Out.Write("fo:color=\"" + ExportUtils.HTMLColor(Style.TextColor) + "\""); Out.WriteLine("/>"); Out.Write("<style:paragraph-properties "); if (Style.HAlign == HorzAlign.Left) { Out.Write("fo:text-align=\"start\" "); } if (Style.HAlign == HorzAlign.Center) { Out.Write("fo:text-align=\"center\" "); } if (Style.HAlign == HorzAlign.Right) { Out.Write("fo:text-align=\"end\" "); } if (Style.Padding.Left > 0) { Out.Write("fo:margin-left=\"" + ExportUtils.FloatToString(Style.Padding.Left / odfDivider) + "cm\" "); } if (Style.Padding.Right > 0) { Out.Write("fo:margin-right=\"" + ExportUtils.FloatToString(Style.Padding.Right / odfDivider) + "cm\" "); } if (Style.Padding.Top > 0) { Out.Write("fo:margin-top=\"" + ExportUtils.FloatToString(Style.Padding.Top / odfDivider) + "cm\" "); } if (Style.Padding.Bottom > 0) { Out.Write("fo:margin-bottom=\"" + ExportUtils.FloatToString(Style.Padding.Bottom / odfDivider) + "cm\" "); } Out.WriteLine("/>"); } OdfTableCellStyles(Style, Out); } if (FExportType == 1) { Out.WriteLine("<style:style style:name=\"pb\" " + "style:family=\"paragraph\" style:display=\"false\"/>"); for (int i = 0; i < FMatrix.StylesCount; i++) { Style = FMatrix.StyleById(i); Out.WriteLine("<style:style style:name=\"p" + i.ToString() + "\" " + "style:family=\"paragraph\" style:parent-style-name=\"Default\">"); Out.Write("<style:text-properties style:font-name=\"" + Style.Font.Name + "\" fo:font-size=\"" + ExportUtils.FloatToString(Style.Font.Size) + "pt\" "); if ((Style.Font.Style & FontStyle.Underline) > 0) { Out.Write(" style:text-underline-style=\"solid\" " + "style:text-underline-width=\"auto\" " + "style:text-underline-color=\"font-color\" "); } if ((Style.Font.Style & FontStyle.Italic) > 0) { Out.Write(" style:font-style=\"italic\" "); } if ((Style.Font.Style & FontStyle.Bold) > 0) { Out.Write(" style:font-weight=\"bold\" "); } Out.WriteLine(" fo:color=\"" + ExportUtils.HTMLColor(Style.TextColor) + "\"/>"); Out.Write("<style:paragraph-properties "); if (Style.HAlign == HorzAlign.Left) { Out.Write("fo:text-align=\"start\" "); } if (Style.HAlign == HorzAlign.Center) { Out.Write("fo:text-align=\"center\" "); } if (Style.HAlign == HorzAlign.Right) { Out.Write("fo:text-align=\"end\" "); } if (Style.Padding.Left > 0) { Out.Write("fo:margin-left=\"" + ExportUtils.FloatToString(Style.Padding.Left / odfDivider) + "cm\" "); } if (Style.Padding.Right > 0) { Out.Write("fo:margin-right=\"" + ExportUtils.FloatToString(Style.Padding.Right / odfDivider) + "cm\" "); } if (Style.Padding.Top > 0) { Out.Write("fo:margin-top=\"" + ExportUtils.FloatToString(Style.Padding.Top / odfDivider) + "cm\" "); } if (Style.Padding.Bottom > 0) { Out.Write("fo:margin-bottom=\"" + ExportUtils.FloatToString(Style.Padding.Bottom / odfDivider) + "cm\" "); } Out.WriteLine("/>"); Out.WriteLine("</style:style>"); } } Out.WriteLine("<style:style style:name=\"gr1\" style:family=\"graphic\">"); Out.WriteLine("<style:graphic-properties draw:stroke=\"none\" " + "draw:fill=\"none\" draw:textarea-horizontal-align=\"left\" " + "draw:textarea-vertical-align=\"top\" draw:color-mode=\"standard\" " + "draw:luminance=\"0%\" draw:contrast=\"0%\" draw:gamma=\"100%\" " + "draw:red=\"0%\" draw:green=\"0%\" draw:blue=\"0%\" " + "fo:clip=\"rect(0cm 0cm 0cm 0cm)\" draw:image-opacity=\"100%\" " + "style:mirror=\"none\"/>"); Out.WriteLine("</style:style>"); Out.WriteLine("</office:automatic-styles>"); // body Out.WriteLine("<office:body>"); Out.WriteLine("<office:spreadsheet>"); Out.WriteLine("<table:table table:name=\"Table\" table:style-name=\"ta1\" table:print=\"false\">"); for (int x = 1; x < FMatrix.Width; x++) { Out.WriteLine("<table:table-column table:style-name=\"co" + ExportUtils.FloatToString((FMatrix.XPosById(x) - FMatrix.XPosById(x - 1)) / odfDivider) + "\"/>"); } for (int y = 0; y < FMatrix.Height - 1; y++) { Out.WriteLine("<table:table-row table:style-name=\"ro" + ExportUtils.FloatToString((FMatrix.YPosById(y + 1) - FMatrix.YPosById(y)) / odfDivider) + "\">"); if (FMatrix.YPosById(y) >= FMatrix.PageBreak(Page)) { Page++; if (FPageBreaks) { Out.WriteLine("<table:table-row table:style-name=\"ro_breaked\"/>"); } } for (int x = 0; x < FMatrix.Width; x++) { int i = FMatrix.Cell(x, y); if (i != -1) { ExportIEMObject Obj = FMatrix.ObjectById(i); if (Obj.Counter == 0) { Obj.Counter = 1; FMatrix.ObjectPos(i, out fx, out fy, out dx, out dy); Out.Write("<table:table-cell table:style-name=\"ce" + Obj.StyleIndex.ToString() + "\" "); if (dx > 1) { Out.Write("table:number-columns-spanned=\"" + dx.ToString() + "\" "); } if (dy > 1) { Out.Write("table:number-rows-spanned=\"" + dy.ToString() + "\" "); } Out.WriteLine(">"); if (Obj.IsText) { s = ExportUtils.XmlString(Obj.Text, Obj.HtmlTags); Out.Write("<text:p"); if (FExportType == 1) { Out.Write(" text:style-name=\"p" + Obj.StyleIndex.ToString() + "\""); } Out.WriteLine(">" + s + "</text:p>"); } else { if (Obj.Width > 0) { PicCount++; if (Config.FullTrust) { using (FileStream emfFile = new FileStream(FTempFolder + "\\Pictures\\pic" + PicCount.ToString() + ".emf", FileMode.Create)) emfFile.Write(Obj.PictureStream.ToArray(), 0, (int)Obj.PictureStream.Length); } else { using (FileStream pngFile = new FileStream(FTempFolder + "\\Pictures\\pic" + PicCount.ToString() + ".png", FileMode.Create)) pngFile.Write(Obj.PictureStream.ToArray(), 0, (int)Obj.PictureStream.Length); } if (FExportType == 1) { Out.WriteLine("<text:p>"); } // need for fix of vertical position Out.WriteLine("<draw:frame draw:z-index=\"" + (PicCount - 1).ToString() + "\" " + "draw:name=\"Pictures" + PicCount.ToString() + "\" " + "draw:style-name=\"gr1\" " + "draw:text-style-name=\"P1\" " + "svg:width=\"" + ExportUtils.FloatToString(Obj.Width / odfDivider) + "cm\" " + "svg:height=\"" + ExportUtils.FloatToString(Obj.Height / odfDivider) + "cm\" " + "svg:x=\"0cm\" svg:y=\"0cm\">"); s = Config.FullTrust ? ".emf" : ".png"; Out.WriteLine("<draw:image " + "xlink:href=\"Pictures/pic" + PicCount.ToString() + s + "\" " + "text:anchor-type=\"frame\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"); Out.WriteLine("</draw:frame>"); if (FExportType == 1) { Out.WriteLine("</text:p>"); } } } Out.WriteLine("</table:table-cell>"); } else { Out.Write("<table:covered-table-cell table:style-name=\"ceb\""); if (FExportType == 1) { Out.WriteLine("><text:p text:style-name=\"pb\"/></table:covered-table-cell>"); } else { Out.WriteLine("/>"); } } } else { Out.Write("<table:table-cell"); if (FExportType == 1) { Out.WriteLine("><text:p text:style-name=\"pb\"/></table:table-cell>"); } else { Out.WriteLine("/>"); } } } Out.WriteLine("</table:table-row>"); } Out.WriteLine("</table:table>"); Out.WriteLine("</office:spreadsheet>"); Out.WriteLine("</office:body>"); Out.WriteLine("</office:document-content>"); } #endregion string ExportMime = FExportType == 0 ? "spreadsheet" : "text"; OdfCreateManifest(FTempFolder + "\\META-INF\\manifest.xml", PicCount, ExportMime); OdfCreateMime(FTempFolder + "\\mimetype", ExportMime); OdfCreateMeta(FTempFolder + "\\meta.xml", Creator); ZipArchive zip = new ZipArchive(); zip.AddDir(FTempFolder); zip.SaveToStream(Stream); Directory.Delete(FTempFolder, true); }