public TResult WriteCustomProperties <TResult>( Func <Action <string, string>, TResult> callback) { var customPropsPart = workbook.AddCustomFilePropertiesPart(); var properties = new CustomDocumentProperty[] { }; var result = callback( (string name, string value) => { var property = new CustomDocumentProperty(); property.FormatId = Guid.NewGuid().ToString("B"); property.Name = name; property.VTBString = new VTBString(value); property.PropertyId = properties.Length + 2; properties = properties.Append(property).ToArray(); }); customPropsPart.Properties = new Properties(properties); var writer = OpenXmlWriter.Create(customPropsPart); writer.WriteStartElement(customPropsPart.Properties); writer.WriteEndElement(); writer.Close(); return(result); }
public void X006_Xlsx_DeleteAdd_CoreExtendedProperties() { var docName = "Spreadsheet.xlsx"; var ba = File.ReadAllBytes(s_TestFileLocation + docName); using (MemoryStream ms = new MemoryStream()) { ms.Write(ba, 0, ba.Length); using (SpreadsheetDocument doc = SpreadsheetDocument.Open(ms, true)) { var corePart = doc.CoreFilePropertiesPart; var appPart = doc.ExtendedFilePropertiesPart; doc.DeletePart(corePart); doc.DeletePart(appPart); doc.AddCoreFilePropertiesPart(); doc.AddExtendedFilePropertiesPart(); doc.AddCustomFilePropertiesPart(); doc.AddDigitalSignatureOriginPart(); doc.AddExtendedPart("relType", "contentType/xml", ".xml"); var tnPart = doc.AddThumbnailPart(ThumbnailPartType.Jpeg); doc.DeletePart(tnPart); tnPart = doc.AddThumbnailPart("image/jpg"); OpenXmlValidator v = new OpenXmlValidator(FileFormatVersions.Office2013); var errs = v.Validate(doc); Assert.Equal(1, errs.Count()); } } }
private CustomFilePropertiesPart AddExtended(SpreadsheetDocument document) { CustomFilePropertiesPart propertiesPart = document.CustomFilePropertiesPart; if (propertiesPart == null) { propertiesPart = document.AddCustomFilePropertiesPart(); if (propertiesPart != null && propertiesPart.Properties == null) { propertiesPart.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties(); } } return(propertiesPart); }
public override void Write() { ExportFileName = PopulatedName(ExportFileName); if (!String.IsNullOrWhiteSpace(ExportFileName)) { DocProperties["FileName"] = ExportFileName; DocProperties["TableCount"] = _dataSet.Tables.Count.ToString(); if (PopulatePropertiesOnly) { if (_dataSet != null) { foreach (DataTable dTable in _dataSet.Tables) { if (dTable.Rows.Count > 0) { foreach (DataColumn dColumn in dTable.Columns) { DocProperties[dColumn.ColumnName] = dTable.Rows[0][dColumn].ToString(); } } } } } switch (DestinationType) { case OfficeFileType.WordDocument: WordprocessingDocument doc; if (File.Exists(TemplateFileName)) { doc = WordprocessingDocument.CreateFromTemplate(TemplateFileName); doc = (WordprocessingDocument)doc.SaveAs(ExportFileName); } else { doc = WordprocessingDocument.Create(ExportFileName, WordprocessingDocumentType.Document); } CustomFilePropertiesPart customProp = doc.CustomFilePropertiesPart; if (customProp == null) { customProp = doc.AddCustomFilePropertiesPart(); } SetFileProperties(customProp); MainDocumentPart mainDoc = doc.MainDocumentPart; if (mainDoc == null) { mainDoc = doc.AddMainDocumentPart(); } DocumentSettingsPart settingsPart = mainDoc.GetPartsOfType <DocumentSettingsPart>().First(); UpdateFieldsOnOpen updateFields = new UpdateFieldsOnOpen { Val = new OnOffValue(true) }; settingsPart.Settings.PrependChild <UpdateFieldsOnOpen>(updateFields); settingsPart.Settings.Save(); if (!PopulatePropertiesOnly) { if (mainDoc.Document == null) { mainDoc.Document = new word.Document(); } word.Body body = new word.Body(); bool firstTable = true; foreach (DataTable dt in _dataSet.Tables) { if (!firstTable) { body.Append(GetPageBreak()); } else { firstTable = false; } body.Append(GetParagraph(dt.TableName)); body.Append(GetWordTable(dt)); } mainDoc.Document.Append(body); } mainDoc.Document.Save(); doc.Dispose(); break; case OfficeFileType.ExcelWorkbook: SpreadsheetDocument spreadSheet; if (File.Exists(TemplateFileName)) { spreadSheet = SpreadsheetDocument.CreateFromTemplate(TemplateFileName); spreadSheet = (SpreadsheetDocument)spreadSheet.SaveAs(ExportFileName); } else { spreadSheet = SpreadsheetDocument.Create(ExportFileName, SpreadsheetDocumentType.Workbook); spreadSheet.Save(); } using (SpreadsheetDocument workbook = spreadSheet) { CustomFilePropertiesPart excelCustomProp = workbook.AddCustomFilePropertiesPart(); SetFileProperties(excelCustomProp); if (workbook.WorkbookPart == null) { workbook.AddWorkbookPart(); } if (workbook.WorkbookPart.Workbook == null) { workbook.WorkbookPart.Workbook = new excel.Workbook(); } if (workbook.WorkbookPart.Workbook.Sheets == null) { workbook.WorkbookPart.Workbook.Sheets = new excel.Sheets(); } excel.Sheets sheets = workbook.WorkbookPart.Workbook.Sheets; foreach (DataTable table in _dataSet.Tables) { excel.SheetData sheetData = null; WorksheetPart sheetPart = null; excel.Sheet sheet = null; foreach (OpenXmlElement element in sheets.Elements()) { if (element is Sheet) { sheet = (Sheet)element; if (sheet.Name.Value.Equals(table.TableName, StringComparison.CurrentCultureIgnoreCase)) { //Assign the sheetPart sheetPart = (WorksheetPart)workbook.WorkbookPart.GetPartById(sheet.Id.Value); sheetData = sheetPart.Worksheet.GetFirstChild <SheetData>(); break; } } sheet = null; } if (sheet == null) { sheetPart = workbook.WorkbookPart.AddNewPart <WorksheetPart>(); //Create a new WorksheetPart sheetData = new excel.SheetData(); //create a new SheetData sheetPart.Worksheet = new excel.Worksheet(sheetData); /// Create a new Worksheet with the sheetData and link it to the sheetPart... string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart); //get the ID of the sheetPart. sheet = new excel.Sheet() { Id = relationshipId, SheetId = 1, Name = table.TableName }; //create a new sheet sheets.Append(sheet); //append the sheet to the sheets. } List <String> columns = new List <string>(); foreach (System.Data.DataColumn column in table.Columns) { columns.Add(column.ColumnName); } if (PrintTableHeader) { excel.Row headerRow = new excel.Row(); foreach (string column in columns) { excel.Cell cell = new excel.Cell { DataType = excel.CellValues.String, CellValue = new excel.CellValue(GetColumnName(table.Columns[column])) }; headerRow.AppendChild(cell); } sheetData.AppendChild(headerRow); } foreach (DataRow dsrow in table.Rows) { excel.Row newRow = new excel.Row(); foreach (String col in columns) { excel.Cell cell = new excel.Cell { DataType = excel.CellValues.String, CellValue = new excel.CellValue(dsrow[col].ToString()) // }; newRow.AppendChild(cell); } sheetData.AppendChild(newRow); } sheetPart.Worksheet.Save(); } workbook.WorkbookPart.Workbook.Save(); workbook.Save(); workbook.Close(); } break; } } }