/// <summary> /// Writes the given <see cref="ISpreadsheetDataSet"/> to a spreadsheet file at the given path. /// The path's extension will be replaced by the value of this instance's <see cref="Extension"/> property. /// </summary> /// <param name="dataSet">The <see cref="ISpreadsheetDataSet"/> to write.</param> /// <param name="path">The path to write to.</param> public abstract void Write(ISpreadsheetDataSet dataSet, string path);
/// <summary> /// Writes the given <see cref="ISpreadsheetDataSet"/> to a spreadsheet file at the given path. /// The path's extension will be replaced by the value of this instance's <see cref="Extension"/> property. /// </summary> /// <param name="dataSet">The <see cref="ISpreadsheetDataSet"/> to write.</param> /// <param name="path">The path to write to.</param> public override void Write(ISpreadsheetDataSet dataSet, string path) { string directory = Path.GetDirectoryName(path); string tempPath = Path.Combine(directory, Path.GetRandomFileName()); if (!String.IsNullOrEmpty(directory) && !Directory.Exists(directory)) { Directory.CreateDirectory(directory); } try { using (XmlWriter xw = XmlWriter.Create(tempPath, new XmlWriterSettings() { Indent = true })) { xw.WriteStartElement("office", "document-content", ns["office"]); // Declare the namespaces. foreach (string key in ns.Keys) { xw.WriteAttributeString("xmlns", key, null, ns[key]); } // Font faces. xw.WriteStartElement("font-face-decls", ns["office"]); WriteFontFace(xw, "Arial", "Arial", "swiss", "variable"); WriteFontFace(xw, "Arial Unicode MS", "'Arial Unicode MS'", "system", "variable"); WriteFontFace(xw, "Tahoma", "Tahoma", "system", "variable"); xw.WriteEndElement(); // Cell and table styles. xw.WriteStartElement("automatic-styles", ns["office"]); WriteCellStyle(xw, "ce1", "table-cell", "Default", "N37"); WriteCellStyle(xw, "ce2", "table-cell", "Default", "N5042"); WriteTableStyle(xw, "ta1", "table", "Default", "true", "lr-tb"); xw.WriteEndElement(); // Main body. xw.WriteStartElement("body", ns["office"]); xw.WriteStartElement("spreadsheet", ns["office"]); foreach (ISpreadsheetDataTable table in dataSet.Tables) { WriteTableStartElement(xw, table.Name, "ta1", "false"); WriteTableColumnDefinition(xw, "co1", "Default", table.Columns.Count.ToString(CultureInfo.InvariantCulture)); // Header. xw.WriteStartElement("table-row", ns["table"]); foreach (ISpreadsheetDataColumn column in table.Columns) { WriteCell(xw, typeof(string), column.Name); } xw.WriteEndElement(); // Content rows. for (int i = 0; i < table.Rows.Count; i++) { xw.WriteStartElement("table-row", ns["table"]); for (int j = 0; j < table.Columns.Count; j++) { WriteCell(xw, table.Columns[j].DataType, table.Rows[i][table.Columns[j].Name]); } xw.WriteEndElement(); if (i % 100 == 0) { xw.Flush(); } } xw.WriteEndElement(); xw.Flush(); } xw.WriteEndElement(); xw.WriteEndElement(); xw.WriteEndElement(); } SavePackage(tempPath, CreatePath(path)); } finally { if (File.Exists(tempPath)) { File.Delete(tempPath); } } }