Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JSpreadsheet
>
Tutorial
> Create Table
The following example shows you how to create a table.
import com.independentsoft.office.spreadsheet.AutoFilter; import com.independentsoft.office.spreadsheet.Cell; import com.independentsoft.office.spreadsheet.Column; import com.independentsoft.office.spreadsheet.Workbook; import com.independentsoft.office.spreadsheet.Worksheet; import com.independentsoft.office.spreadsheet.tables.Table; import com.independentsoft.office.spreadsheet.tables.TableColumn; public class Example { public static void main(String[] args) { try { Worksheet sheet1 = new Worksheet(); sheet1.set("A1", new Cell("Column1")); sheet1.set("A2", new Cell(100)); sheet1.set("A3", new Cell(200)); sheet1.set("A4", new Cell(300)); sheet1.set("B1", new Cell("Column2")); sheet1.set("B2", new Cell(110)); sheet1.set("B3", new Cell(210)); sheet1.set("B4", new Cell(310)); sheet1.set("C1", new Cell("Column3")); sheet1.set("C2", new Cell(120)); sheet1.set("C3", new Cell(220)); sheet1.set("C4", new Cell(320)); sheet1.set("D1", new Cell("Column4")); sheet1.set("D2", new Cell(130)); sheet1.set("D3", new Cell(230)); sheet1.set("D4", new Cell(330)); Table table1 = new Table(); table1.setID(1); table1.setName("Table1"); table1.setDisplayName("Table1"); table1.setReference("A1:D4"); table1.setAutoFilter(new AutoFilter("A1:D4")); TableColumn tableColumn1 = new TableColumn(1, "Column1"); TableColumn tableColumn2 = new TableColumn(2, "Column2"); TableColumn tableColumn3 = new TableColumn(3, "Column3"); TableColumn tableColumn4 = new TableColumn(4, "Column4"); table1.getColumns().add(tableColumn1); table1.getColumns().add(tableColumn2); table1.getColumns().add(tableColumn3); table1.getColumns().add(tableColumn4); sheet1.getTables().add(table1); //set columns width Column columnInfo = new Column(); columnInfo.setFirstColumn(1); //from column A columnInfo.setLastColumn(4); //to column D columnInfo.setWidth(15); sheet1.getColumns().add(columnInfo); Workbook book = new Workbook(); book.getSheets().add(sheet1); book.save("c:\\test\\output.xlsx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*