Document Solutions for Excel, Java Edition | Document Solutions
File Operations / Export to PDF / Export Table
In This Topic
    Export Table
    In This Topic

    DsExcel Java allows users to save worksheets with tables while exporting to a PDF file.

    In order to export an excel file with tables to PDF format, refer to the following example code.

    Java
    Copy Code
    // 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);