Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JSpreadsheet
>
Tutorial
> Set Cell Format
The following example shows you how to set cell format.
import com.independentsoft.office.spreadsheet.Cell; import com.independentsoft.office.spreadsheet.Workbook; import com.independentsoft.office.spreadsheet.Worksheet; import com.independentsoft.office.spreadsheet.styles.CellFormat; import com.independentsoft.office.spreadsheet.styles.MasterCellFormat; import com.independentsoft.office.spreadsheet.styles.NumberFormat; public class Example { public static void main(String[] args) { try { Workbook book = new Workbook(); MasterCellFormat commaFormat = new MasterCellFormat(); commaFormat.setNumberFormat(new NumberFormat(1, "#,##0.00")); MasterCellFormat currencyFormat = new MasterCellFormat(); currencyFormat.setNumberFormat(new NumberFormat(2, "$#,##0.00")); book.getStyles().getMasterCellFormats().add(commaFormat); book.getStyles().getMasterCellFormats().add(currencyFormat); CellFormat format1 = new CellFormat(); format1.setNumberFormatID(1); CellFormat format2 = new CellFormat(); format2.setNumberFormatID(2); Cell a1 = new Cell(9999.99); a1.setFormat(format1); Cell a2 = new Cell(9999.99); a2.setFormat(format2); Worksheet sheet1 = new Worksheet(); sheet1.set("A1", a1); sheet1.set("A2", a2); 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*