// Create a new workbook Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); // Change column width and row height int[] columnWidths = { 24, 13, 14, 17 }; int[] rowHeights = { 15, 9, 19, 17, 17, 35, 32, 7, 17, 15, 15, 15, 15, 15, 15, 15, 15, 17 }; for (int i = 0; i < columnWidths.length; i++) { worksheet.getColumns().get(i).setColumnWidth(columnWidths[i]); } for (int i = 0; i < rowHeights.length; i++) { worksheet.getRows().get(i).setRowHeight(rowHeights[i]); } // Set data and style worksheet.getRange("A3").setValue("Invoice"); worksheet.getRange("A3").getFont().setSize(14); worksheet.getRange("A3").getFont().setBold(true); worksheet.getRange("C3").setValue("Date:"); worksheet.getRange("C3").setHorizontalAlignment(HorizontalAlignment.Right); worksheet.getRange("D3").setFormula2("=NOW()"); worksheet.getRange("D3").setNumberFormat("m/d/yyyy"); worksheet.getRange("D3").getBorders().get(BordersIndex.EdgeBottom).setLineStyle(BorderLineStyle.Medium); worksheet.getRange("A5").setValue("Shipping Address:"); worksheet.getRange("C5").setValue("Billing Address:"); worksheet.getRange("A6").setValue("150 Wayback Lane,\nNew York,\nNew York"); worksheet.getRange("C6").setValue("414 Rocket Drive,\nWesterville,\nOhio"); // Set table data Object[][] data = new Object[][] { {"Product", "Unit Price", "Quantity", "Total Price" }, {"Bike", 125, 1, "" }, {"Helmet", 35, 1, "" }, {"Gloves", 15.4, 1, "" }, {"Accessories", 20, 3, "" } }; worksheet.getRange("A9:D13").setValue(data); // Add table. ITable table = worksheet.getTables().add(worksheet.getRange("A9:D13"), true); // Set table Style worksheet.getRange("B10:B13").setNumberFormat("_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)"); worksheet.getRange("D10:D13").setFormulaR1C1("=[@Quantity]*[@Unit Price]"); worksheet.getRange("D10:D13").setNumberFormat("_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)"); table.getColumns().get(0).setTotalsCalculation(TotalsCalculation.None); table.getColumns().get(1).setTotalsCalculation(TotalsCalculation.None); table.getColumns().get(2).setTotalsCalculation(TotalsCalculation.None); table.getColumns().get(3).setTotalsCalculation(TotalsCalculation.Sum); table.setShowTotals(true); // Merge cells worksheet.getRange("A3:B3").merge(); worksheet.getRange("A5:B5").merge(); worksheet.getRange("A5:B5").getBorders().setLineStyle(BorderLineStyle.Medium); worksheet.getRange("C5:D5").merge(); worksheet.getRange("C5:D5").getBorders().setLineStyle(BorderLineStyle.Medium); worksheet.getRange("A6:B7").merge(); worksheet.getRange("A6:B7").setWrapText(true); worksheet.getRange("A6:B7").setVerticalAlignment(VerticalAlignment.Center); worksheet.getRange("A6:B7").getBorders().setLineStyle(BorderLineStyle.Medium); worksheet.getRange("C6:D7").merge(); worksheet.getRange("C6:D7").setWrapText(true); worksheet.getRange("C6:D7").setVerticalAlignment(VerticalAlignment.Center); worksheet.getRange("C6:D7").getBorders().setLineStyle(BorderLineStyle.Medium); worksheet.getSheetView().setDisplayGridlines(false); // Convert table to range. table.convertToRange(); // Save to an excel file workbook.save("ConvertToRange.xlsx");