private void DoExport(string saveFileName, string rootName, string elemName, Action <Row[]> buildContent, bool hasKey) { if (File.Exists(saveFileName)) { saveFileName = saveFileName.Replace(".xlsx", "_2.xlsx"); } Console.WriteLine("Export Excel {0}", saveFileName); selectedColor = 0; columnWidthAndStyle.Clear(); SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(saveFileName, SpreadsheetDocumentType.Workbook); // Add a WorkbookPart to the document. WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart(); spreadsheetDocument.ChangeIdOfPart(workbookpart, "rIdbk0"); workbookpart.Workbook = new Workbook(); AppendStylePart(spreadsheetDocument); // Add a WorksheetPart to the WorkbookPart. WorksheetPart worksheetPart = workbookpart.AddNewPart <WorksheetPart>("rIdsht0"); worksheetPart.Worksheet = new Worksheet(); AppendSheetViews(worksheetPart, hasKey); Columns columns = new Columns(); worksheetPart.Worksheet.AppendChild(columns); Row[] rows = AppendDefaultRows(worksheetPart, workbookpart, string.IsNullOrEmpty(elemName) ? rootName : rootName + "/" + elemName); buildContent(rows); AppendColumns(columns, hasKey); workbookpart.Workbook.Save(); // Close the document. spreadsheetDocument.Close(); RepackFile(saveFileName); }
public void ExportEnums() { string folder = Path.GetFullPath(PathManager.Instance.ExternalExcelExampleFolder); string saveFileName = Path.Combine(folder, "enum.xlsx"); if (File.Exists(saveFileName)) { File.Delete(saveFileName); } SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(saveFileName, SpreadsheetDocumentType.Workbook); // Add a WorkbookPart to the document. WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart(); spreadsheetDocument.ChangeIdOfPart(workbookpart, "rIdbk0"); workbookpart.Workbook = new Workbook(); AppendStylePart(spreadsheetDocument); // Add a WorksheetPart to the WorkbookPart. WorksheetPart worksheetPart = workbookpart.AddNewPart <WorksheetPart>("rIdsht0"); worksheetPart.Worksheet = new Worksheet(); Columns columns = new Columns(); columns.AppendChild(new Column() { Min = 1, Max = 1, Width = 20, }); columns.AppendChild(new Column() { Min = 2, Max = 2, Width = 10, }); columns.AppendChild(new Column() { Min = 3, Max = 3, Width = 20, }); columns.AppendChild(new Column() { Min = 4, Max = 4, Width = 20, }); worksheetPart.Worksheet.AppendChild(columns); SheetData sheetData = AppendSheet(worksheetPart, workbookpart); int maxlen = 0; foreach (var type in enums) { var nocommentAttribute = TypeUtil.GetCustomAttribute <NoCommentAttribute>(type, false); if (nocommentAttribute != null) { continue; } UInt32Value style = 5; Row row = sheetData.AppendChild(new Row() { CustomFormat = true, StyleIndex = style }); row.AppendChild(CreateCell(type.Name, style, ref maxlen)); string[] names = Enum.GetNames(type); foreach (var name in names) { if (row == null) { row = sheetData.AppendChild(new Row()); row.AppendChild(CreateCell("", null, ref maxlen)); } var value = Enum.Parse(type, name); row.AppendChild(CreateCell(TypeUtil.GetEnumComment(type, value as Enum), style, ref maxlen)); row.AppendChild(CreateCell(name, style, ref maxlen)); row.AppendChild(CreateCell((int)value, style, ref maxlen)); row = null; style = null; } } workbookpart.Workbook.Save(); // Close the document. spreadsheetDocument.Close(); RepackFile(saveFileName); }