using FastReport.Utils; using System; using System.Collections; using System.IO; using System.Reflection; namespace FastReport.Export.OoXML { /// /// Base class for Microsoft Office 2007 export objects /// public class OOExportBase : ExportBase { private ZipArchive zip; /// /// Default XML header /// #region Constants protected const string xml_header = ""; #endregion #region Properties internal ZipArchive Zip { get { return zip; } set { zip = value; } } #endregion #region Helpers internal string Quoted(string p) { return "\"" + p + "\" "; } internal string QuotedRoot(string p) { return "\"/" + p + "\" "; } #endregion } /// /// Base class for export Office Open objects /// internal abstract class OoXMLBase { #region Private fileds private ArrayList relations = new ArrayList(); private int id; #endregion #region Constants public const string xml_header = ""; #endregion #region Abstract public abstract string RelationType { get; } public abstract string ContentType { get; } public abstract string FileName { get; } #endregion #region Helpers protected string Quoted(string p) { return String.Concat("\"", p, "\" "); } protected string Quoted(long p) { return String.Concat("\"", p.ToString(), "\" "); } protected string Quoted(float p) { return String.Concat("\"", Converter.ToString(p), "\" "); } protected string GetDashStyle(System.Drawing.Drawing2D.DashStyle style) { switch (style) { case System.Drawing.Drawing2D.DashStyle.Solid: return ""; case System.Drawing.Drawing2D.DashStyle.Dot: return ""; case System.Drawing.Drawing2D.DashStyle.Dash: return ""; case System.Drawing.Drawing2D.DashStyle.DashDot: return ""; case System.Drawing.Drawing2D.DashStyle.DashDotDot: return ""; } throw new Exception("Unsupported dash style"); } private string TranslatePath(string source, string dest) { int j; string result = ""; char[] charSeparators = new char[] { '\\', '/' }; string[] rel_dir_name = Path.GetDirectoryName(source).Split(charSeparators); string[] items_dir_name = Path.GetDirectoryName(dest).Split(charSeparators); for (int i = 0; ; i++) { if (i == rel_dir_name.Length || i == items_dir_name.Length || items_dir_name[i].CompareTo(rel_dir_name[i]) != 0) { for (j = i; j < rel_dir_name.Length; j++) result += "../"; for (j = i; j < items_dir_name.Length; j++) result += items_dir_name[j]; break; } } if (result != "") result += "/"; return result; } #endregion #region Properties internal string rId { get { return "rId" + id.ToString(); } } public ArrayList RelationList { get { return relations; } } #endregion #region Protected methods protected void ExportRelations(OOExportBase export_base) { if (relations.Count != 0) { string relation_dir_name = Path.GetDirectoryName(FileName) + "/_rels/"; string relation_file_name = Path.GetFileName(FileName) + ".rels"; string related_path = ""; MemoryStream file = new MemoryStream(); ExportUtils.WriteLn(file, xml_header); ExportUtils.WriteLn(file, ""); foreach (OoXMLBase relation_item in relations) { if (relation_item is OoHyperlink) { OoHyperlink item = relation_item as OoHyperlink; ExportUtils.WriteLn(file, ""); } else { related_path = TranslatePath(FileName, relation_item.FileName) + Path.GetFileName(relation_item.FileName); ExportUtils.WriteLn(file, ""); } } ExportUtils.WriteLn(file, ""); file.Position = 0; export_base.Zip.AddStream(ExportUtils.TruncLeadSlash(relation_dir_name + relation_file_name), file); } } #endregion #region Internal Methods internal bool AddRelation(int Id, OoXMLBase related_object) { if (!relations.Contains(related_object)) { related_object.id = Id; relations.Add(related_object); return true; } return false; } #endregion } /// /// Core document properties /// class OoXMLCoreDocumentProperties : OoXMLBase { #region Class overrides public override string RelationType { get { return "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"; } } public override string ContentType { get { return "application/vnd.openxmlformats-package.core-properties+xml"; } } public override string FileName { get { return "docProps/core.xml"; } } #endregion public void Export(OOExportBase OoXML) { string Title = OoXML.Report.ReportInfo.Name; string Author = OoXML.Report.ReportInfo.Author; string Subject = OoXML.Report.ReportInfo.Description; if (Author.Length == 0) Author = "FastReport.NET"; if (Title.Length == 0) Title = Path.GetFileNameWithoutExtension(OoXML.Report.FileName); MemoryStream file = new MemoryStream(); ExportUtils.WriteLn(file, xml_header); ExportUtils.WriteLn(file, ""); //Out.WriteLine("2009-06-17T07:33:19Z"); ExportUtils.WriteLn(file, "" + Title + ""); if (Subject.Length != 0) ExportUtils.WriteLn(file, "" + Subject.Replace("<", "<").Replace(">", ">") + ""); ExportUtils.WriteLn(file, "" + Author + ""); ExportUtils.WriteLn(file, ""); file.Position = 0; OoXML.Zip.AddStream(ExportUtils.TruncLeadSlash(FileName), file); } } /// /// Core document properties /// class OoXMLApplicationProperties : OoXMLBase { #region Class overrides public override string RelationType { get { return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"; } } public override string ContentType { get { return "application/vnd.openxmlformats-officedocument.extended-properties+xml"; } } public override string FileName { get { return "docProps/app.xml"; } } #endregion public void Export(OOExportBase OoXML) { MemoryStream file = new MemoryStream(); ExportUtils.WriteLn(file, xml_header); ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, "0"); ExportUtils.WriteLn(file, "false"); // Heading description ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, "Worksheets"); ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, "2"); ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, ""); // Titles description ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, "Page1"); ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, ""); ExportUtils.WriteLn(file, "false"); ExportUtils.WriteLn(file, "false"); ExportUtils.WriteLn(file, "false"); ExportUtils.WriteLn(file, "12.0000"); ExportUtils.WriteLn(file, ""); file.Position = 0; OoXML.Zip.AddStream(ExportUtils.TruncLeadSlash(FileName), file); } } internal class OoXMLThemes : OoXMLBase { #region Class overrides public override string RelationType { get { return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"; } } public override string ContentType { get { return "application/vnd.openxmlformats-officedocument.theme+xml"; } } public override string FileName { get { return "ppt/theme/theme1.xml"; } } #endregion public void Export(OOExportBase OoXML, string ThemeRes, string ThemePath) { //ResourceSet set = new ResourceSet(); // get a reference to the current assembly Assembly a = Assembly.GetExecutingAssembly(); // get a list of resource names from the manifest //string[] resNames = a.GetManifestResourceNames(); using (Stream o = a.GetManifestResourceStream("FastReport.Resources.OoXML.theme1.xml")) { // write the required bytes MemoryStream fs = new MemoryStream(); const int BUFFER_SIZE = 4096; o.CopyTo(fs, BUFFER_SIZE); fs.Position = 0; OoXML.Zip.AddStream(ExportUtils.TruncLeadSlash(ThemePath), fs); } } } }