[]
        
(Showing Draft Content)

导出边框

GcExcel Java版允许用户在将边框导出到PDF文件。

要将带边框的excel文件导出为PDF格式,请参阅以下示例代码。

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

// Single cell border
sheet.getRange("B2").getBorders().setThemeColor(ThemeColor.Accent1);
sheet.getRange("B2").getBorders().setLineStyle(BorderLineStyle.SlantDashDot);
sheet.getRange("B2").getBorders().get(BordersIndex.DiagonalUp)
.setThemeColor(ThemeColor.Accent1);
sheet.getRange("B2").getBorders().get(BordersIndex.DiagonalUp)
.setLineStyle(BorderLineStyle.SlantDashDot);
sheet.getRange("B2").getBorders().get(BordersIndex.DiagonalDown)
.setThemeColor(ThemeColor.Accent1);
sheet.getRange("B2").getBorders().get(BordersIndex.DiagonalDown)
.setLineStyle(BorderLineStyle.SlantDashDot);

// Range border
sheet.getRange("D2:E3").getBorders().setThemeColor(ThemeColor.Accent1);
sheet.getRange("D2:E3").getBorders().setLineStyle(BorderLineStyle.DashDot);
sheet.getRange("D2:E3").getBorders().get(BordersIndex.DiagonalDown)
.setThemeColor(ThemeColor.Accent1);
sheet.getRange("D2:E3").getBorders().get(BordersIndex.DiagonalDown)
.setLineStyle(BorderLineStyle.DashDot);

// Merge cell border
sheet.getRange("B6:C7").merge();
sheet.getRange("B6:C7").getBorders().setThemeColor(ThemeColor.Accent1);
sheet.getRange("B6:C7").getBorders().setLineStyle(BorderLineStyle.Double);
sheet.getRange("B6:C7").getBorders().get(BordersIndex.DiagonalUp)
.setThemeColor(ThemeColor.Accent1);
sheet.getRange("B6:C7").getBorders().get(BordersIndex.DiagonalUp)
.setLineStyle(BorderLineStyle.Double);

// Apply border style on table
ITable table = sheet.getTables().add(sheet.getRange("B12:G22"), true);

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

// Set outline border for "whole table" style
ITableStyleElement wholeTableStyle = customTableStyle.getTableStyleElements()
.get(TableStyleElementType.WholeTable);
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);

// Set vertical border for "first row strip" style
ITableStyleElement firstRowStripStyle = customTableStyle.getTableStyleElements()
.get(TableStyleElementType.FirstRowStripe);
firstRowStripStyle.getBorders().get(BordersIndex.InsideVertical)
.setThemeColor(ThemeColor.Accent6);
firstRowStripStyle.getBorders().get(BordersIndex.InsideVertical)
.setLineStyle(BorderLineStyle.Dashed);

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

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