/// <summary> /// Cria a workbook e todos os seus objetos no formato do Excel (COM) /// </summary> /// <param name="eApp"></param> /// <param name="xWorkbook"></param> private Excel.Workbook createWorkbook(Excel.Application eApp, XWorkbook xWorkbook) { Excel.Workbook eBook = eApp.Workbooks.Add(); eBook.Title = xWorkbook.getName(); PrepareWorkbook(eBook); int numWorksheets = 1; foreach (XWorksheet xWorksheet in xWorkbook.getWorksheets()) { Excel.Worksheet eWorksheet = numWorksheets == 1 ? (Excel.Worksheet)eBook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) : (Excel.Worksheet)eBook.Worksheets.Add(Missing.Value, eBook.Worksheets[numWorksheets - 1], Missing.Value, Missing.Value); eWorksheet.Name = xWorksheet.getName(); int i = 1; foreach (XDataTable xDataTable in xWorksheet.getDataTables()) { Excel.Range title = eWorksheet.Cells[i, 1]; title.Value = xDataTable.getName(); Excel.Font titleFont = title.Font; titleFont.Bold = true; titleFont.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); Marshal.ReleaseComObject(titleFont); Marshal.ReleaseComObject(title); Excel.ListObjects eListObjects = eWorksheet.ListObjects; Excel.Range bBegin = (Excel.Range)eWorksheet.Cells[++i, 1]; Excel.ListObject eListObject = eListObjects.Add(Excel.XlListObjectSourceType.xlSrcRange, bBegin, Missing.Value, Excel.XlYesNoGuess.xlNo, Missing.Value); eListObject.Name = xDataTable.getName() ?? ""; eListObject.ShowTotals = false; eListObject.ListRows.Add(); eListObject.ListRows.Add(); int columns = 1; foreach (XDataTableColumn sheetColumn in xDataTable.getDataTableColumns()) { Excel.ListColumns eListColumns = eListObject.ListColumns; Excel.ListColumn eListColumn = columns == 1 ? eListColumns[1] : eListColumns.Add(); eListColumn.Name = sheetColumn.getName(); eListColumn.TotalsCalculation = Excel.XlTotalsCalculation.xlTotalsCalculationSum; eListColumn.Range.EntireColumn.ColumnWidth = 14; if (xDataTable.getDataTableColumns().Count == 1 && xDataTable.getDataTableColumns()[0].getDataContent() != null) { var datacontent = xDataTable.getDataTableColumns()[0].getDataContent(); if (datacontent is XDataArray) { var datacontentimpl = (XDataArray)datacontent; var arraysymbol = datacontentimpl.getArray(); int numSymbols = 0; foreach (var symbol in arraysymbol) { if (eListColumn.DataBodyRange.Count < numSymbols) { eListObject.ListRows.Add(); } eListColumn.DataBodyRange[numSymbols + 1] = symbol; numSymbols++; } } } columns++; Marshal.ReleaseComObject(eListColumn); Marshal.ReleaseComObject(eListColumns); } Marshal.ReleaseComObject(eListObject); Marshal.ReleaseComObject(bBegin); Marshal.ReleaseComObject(eListObjects); } numWorksheets++; Marshal.ReleaseComObject(eWorksheet); } return(eBook); }