[]
        
(Showing Draft Content)

导出表格

GcExcel支持将工作表中的表格导出到PDF。

请参阅以下示例代码,将包含表格的excel文件导出为PDF。

// Create a new workbook and access the default worksheet  
Workbook workbook = new Workbook();
IWorksheet sheet = workbook.getWorksheets().get(0);

// Add Table
ITable table = sheet.getTables().add(sheet.getRange("B5:G16"), true);
table.setShowTotals(true);

// Set values to the range
int[] data = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
sheet.getRange("C6:C16").setValue(data);
sheet.getRange("D6:D16").setValue(data);

// Set total functions
table.getColumns().get(1).setTotalsCalculation(TotalsCalculation.Average);
table.getColumns().get(2).setTotalsCalculation(TotalsCalculation.Sum);

// Create custom table style
ITableStyle customTableStyle = workbook.getTableStyles()
.get("TableStyleMedium10").duplicate();

ITableStyleElement wholeTableStyle = customTableStyle.getTableStyleElements()
.get(TableStyleElementType.WholeTable);
wholeTableStyle.getFont().setItalic(true);
wholeTableStyle.getBorders().get(BordersIndex.EdgeTop)
.setThemeColor(ThemeColor.Accent1);
wholeTableStyle.getBorders().get(BordersIndex.EdgeTop)
.setLineStyle(BorderLineStyle.Thick);
wholeTableStyle.getBorders().get(BordersIndex.EdgeRight)
.setThemeColor(ThemeColor.Accent1);
wholeTableStyle.getBorders().get(BordersIndex.EdgeRight)
.setLineStyle(BorderLineStyle.Thick);
wholeTableStyle.getBorders().get(BordersIndex.EdgeBottom)
.setThemeColor(ThemeColor.Accent1);
wholeTableStyle.getBorders().get(BordersIndex.EdgeBottom)
.setLineStyle(BorderLineStyle.Thick);
wholeTableStyle.getBorders().get(BordersIndex.EdgeLeft)
.setThemeColor(ThemeColor.Accent1);
wholeTableStyle.getBorders().get(BordersIndex.EdgeLeft)
.setLineStyle(BorderLineStyle.Thick);

ITableStyleElement firstRowStripStyle = customTableStyle.getTableStyleElements()
.get(TableStyleElementType.FirstRowStripe);
firstRowStripStyle.getFont().setBold(true);

// Apply custom style to table
table.setTableStyle(customTableStyle);

// Save to a pdf file
workbook.save("ExportTable.pdf", SaveFileFormat.Pdf);