[]
GcExcel允许用户将包含各类透视表样式和格式的Excel文件保存到PDF文件中。
通过该功能,用户可以自定义数据透视表在PDF中的显示方式。这包括可以将带有自定义数据透视表布局、数据透视表字段、方向、页面大小等的Excel文件根据设置保存到PDF文件中。
IPivotTable接口的getStyle()和setStyle()方法可用于获取或设置透视表样式。在GcExcel中导出具有透视表样式的PDF时,可以参阅下表中方法及其描述:
方法 | 描述 |
---|---|
这些方法可用于获取或设置在透视表中是否显示列标题。 | |
这些方法可用于获取或设置在透视表中是否显示行标题。 | |
这些方法可用于获取或设置在透视表中的镶边列。 | |
这些方法可用于获取或设置在透视表中的镶边行。 | |
这些方法可用于获取或设置在透视表中的最后一列。 | |
这些方法可用于获取或设置指定样式是否应用在透视表上。 | |
这些方法可用于获取或设置当前字段的格式化字符串。 |
请参阅以下示例代码,以导出具有透视表样式和格式的Excel文件。
// Initialize workbook
Workbook workbook = new Workbook();
// Fetch default worksheet
IWorksheet worksheet = workbook.getWorksheets().get(0);
// Create PivotTable
Object sourceData = new Object[][] {
{ "Order ID", "Product", "Category", "Amount", "Date", "Country" },
{ 1, "Carrots", "Vegetables", 4270, new GregorianCalendar(2012, 1, 6), "United States" },
{ 2, "Broccoli", "Vegetables", 8239, new GregorianCalendar(2012, 1, 7), "United Kingdom" },
{ 3, "Banana", "Fruit", 617, new GregorianCalendar(2012, 1, 8), "United States" },
{ 4, "Banana", "Fruit", 8384, new GregorianCalendar(2012, 1, 10), "Canada" },
{ 5, "Beans", "Vegetables", 2626, new GregorianCalendar(2012, 1, 10), "Germany" },
{ 6, "Orange", "Fruit", 3610, new GregorianCalendar(2012, 1, 11), "United States" },
{ 7, "Broccoli", "Vegetables", 9062, new GregorianCalendar(2012, 1, 11), "Australia" },
{ 8, "Banana", "Fruit", 6906, new GregorianCalendar(2012, 1, 16), "New Zealand" },
{ 9, "Apple", "Fruit", 2417, new GregorianCalendar(2012, 1, 16), "France" },
{ 10, "Apple", "Fruit", 7431, new GregorianCalendar(2012, 1, 16), "Canada" },
{ 11, "Banana", "Fruit", 8250, new GregorianCalendar(2012, 1, 16), "Germany" },
{ 12, "Broccoli", "Vegetables", 7012, new GregorianCalendar(2012, 1, 18),"United States" },
{ 13, "Carrots", "Vegetables", 1903, new GregorianCalendar(2012, 1, 20), "Germany" },
{ 14, "Broccoli", "Vegetables", 2824, new GregorianCalendar(2012, 1, 22), "Canada" },
{ 15, "Apple", "Fruit", 6946, new GregorianCalendar(2012, 1, 24), "France" }, };
worksheet.getRange("A1:F16").setValue(sourceData);
IPivotCache pivotcache = workbook.getPivotCaches().create(worksheet.getRange("A1:F16"));
IPivotTable pivottable = worksheet.getPivotTables().add(pivotcache,
worksheet.getRange("H5"), "pivottable1");
// Create PivotTable style
ITableStyle style = workbook.getTableStyles().add("pivotStyle");
// Set the table style as a pivot table style
style.setShowAsAvailablePivotStyle(true);
style.getTableStyleElements().get(TableStyleElementType.WholeTable).getBorders()
.setLineStyle(BorderLineStyle.DashDotDot);
style.getTableStyleElements().get(TableStyleElementType.WholeTable).getBorders()
.setColor(Color.FromArgb(204, 153, 255));
style.getTableStyleElements().get(TableStyleElementType.WholeTable).getInterior()
.setColor(Color.FromArgb(169, 208, 142));
style.getTableStyleElements().get(TableStyleElementType.WholeTable).getFont().setItalic(true);
style.getTableStyleElements().get(TableStyleElementType.WholeTable)
.getFont().setThemeColor(ThemeColor.Accent2);
// Apply the style to current pivot table
pivottable.setStyle(style);
pivottable.setShowTableStyleColumnHeaders(true);
pivottable.setShowTableStyleRowHeaders(true);
pivottable.setShowTableStyleColumnStripes(true);
pivottable.setShowTableStyleRowStripes(true);
pivottable.setShowTableStyleLastColumn(true);
// Add pivot filed and set number format code
// Add two fileds
IPivotField field_product = pivottable.getPivotFields().get(1);
field_product.setOrientation(PivotFieldOrientation.RowField);
IPivotField field_Amount = pivottable.getPivotFields().get(3);
field_Amount.setOrientation(PivotFieldOrientation.DataField);
// Set number format code
field_Amount.setNumberFormat("#,##0");
// Saving workbook to xlsx
workbook.save("PivotTableStyleAndNumberFormat.pdf", SaveFileFormat.Pdf);