Once you have a Cell object wrapper in JSExcelCell class, you can set and get it's properties.

1 Seting cell value

Each Cell can have a value (boolean, number, string or date) or a formula. The methods for setting cell values or a formula are described below.

Return Method Description
JSExcelCell setCellValue(boolean value) Sets the value of a cell to a boolean.
JSExcelCell setCellValue(double value) Sets the value of a cell to a double.
JSExcelCell setCellValue(String value) Sets the value of a cell to a String.
JSExcelCell setCellValue(java.uti.Date value) Sets the value of a cell to a Date.
JSExcelCell setCellFormula(String formula) Sets a formula on a cell.

2 Getting cell value

As value is set to a Cell it aquires a CellType. So we have BOOLEAN, NUMERIC, STRING and FORMULA cells.

Return Method Description
Object getCellValue() Returns the value for the Cell. If Cell is a formula, it's evaluted using the Workbook FormulaEvaluator instance.
String getCellFormula() Returns the string representing the formula for a Cell. If cell has no formula it will return null.
CellType getCellType() Returns the enumeration constant for the Cell type (BOOLEAN, NUMERIC, STRING, FORMULA, ...).

Notice that a Date Cell is a NUMERIC Cell with a style.

3 Getting cell properties

There are some methods to get cell properties:.

Return Method Description
Number getRowIndex() Return row index number refering row coordinates of the cell.
Number getColumnIndex() Return column index number refering column coordinates of the cell.

4 Naming cells

You can give a unique name to a Cell in a Workbook. This can be used to do template based operations as described in next chapters. To give a name or set a name you can use the following Cell methods.

Return Method Description
JSExcelCell setCellName(String name) Sets a name for the Cell.
String getCellName() Gets the name for the cell or null if it has no name.
String getColumnName() When name has the form tableName.columName returns the columnName or null is cell has no name.

Once a Cell has a name it can be accessed directly using Workbook getCellByName method.

Copy
<script>
    var wb = new Ax.ms.Excel();
    
    var sheet = wb.createSheet("test");
    var cell = sheet.getCell("A1");
    
    cell.setCellName("moon");
    cell.setCellValue("Moon is beautiful");
    
    console.log("cell name   : " + cell.getCellName());
    console.log("cell is     : " + wb.getCellByName("moon"));
    console.log("cell value  : " + wb.getCellByName("moon").getCellValue());
</script>
cell name   : moon
cell is     : A1
cell value  : Moon is beautiful

5 Setting comments

To set a cell comment simply call method setCellComment(String comment)

6 Setting cell style

Styles belongs to Workbook. To create a style, you need to call createStyle() on Wokbook. Then, configure the style and set it to cells. It's a good policy to share a style for as many cells you can avoiding creating repetitive styles.

6.1 Data format

You can setup a data format for a cell using predefined data formats. Avaliable data formats can be displayed accessing Ax.ms.Excel.DataFormat list in the Workbook object.

To see the list of predefined data formats use:

Copy
<script>
    console.log(Ax.ms.Excel.DataFormat);
</script>
[[General, 0, 0.00, #,##0, #,##0.00, "$"#,##0_);("$"#,##0), "$"#,##0_);[Red]("$"#,##0), "$"#,##0.00_);("$"#,##0.00), "$"#,##0.00_);[Red]("$"#,##0.00), 0%, 0.00%, 0.00E+00, # ?/?, # ??/??, m/d/yy, d-mmm-yy, d-mmm, mmm-yy, h:mm AM/PM, h:mm:ss AM/PM, h:mm, h:mm:ss, m/d/yy h:mm, reserved-0x17, reserved-0x18, reserved-0x19, reserved-0x1A, reserved-0x1B, reserved-0x1C, reserved-0x1D, reserved-0x1E, reserved-0x1F, reserved-0x20, reserved-0x21, reserved-0x22, reserved-0x23, reserved-0x24, #,##0_);(#,##0), #,##0_);[Red](#,##0), #,##0.00_);(#,##0.00), #,##0.00_);[Red](#,##0.00), _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_), _("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_), _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_), _("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_), mm:ss, [h]:mm:ss, mm:ss.0, ##0.0E+0, @]]

  • 0, "General"
  • 1, "0"
  • 2, "0.00"
  • 3, "#,##0"
  • 4, "#,##0.00"
  • 5, "$#,##0_);($#,##0)"
  • 6, "$#,##0_);[Red]($#,##0)"
  • 7, "$#,##0.00);($#,##0.00)"
  • 8, "$#,##0.00_);[Red]($#,##0.00)"
  • 9, "0%"
  • 0xa, "0.00%"
  • 0xb, "0.00E+00"
  • 0xc, "# ?/?"
  • 0xd, "# ??/??"
  • 0xe, "m/d/yy"
  • 0xf, "d-mmm-yy"
  • 0x10, "d-mmm"
  • 0x11, "mmm-yy"
  • 0x12, "h:mm AM/PM"
  • 0x13, "h:mm:ss AM/PM"
  • 0x14, "h:mm"
  • 0x15, "h:mm:ss"
  • 0x16, "m/d/yy h:mm"
  • 0x26, "#,##0_);[Red](#,##0)"
  • 0x27, "#,##0.00_);(#,##0.00)"
  • 0x28, "#,##0.00_);[Red](#,##0.00)"
  • 0x29, "_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)"
  • 0x2a, "_($* #,##0_);_($* (#,##0);_($* \"-\"_);_(@_)"
  • 0x2b, "_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)"
  • 0x2c, "_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)"
  • 0x2d, "mm:ss"
  • 0x2e, "[h]:mm:ss"
  • 0x2f, "mm:ss.0"
  • 0x30, "##0.0E+0"
  • 0x31, "@" - This is text format.
  • 0x31 "text" - Alias for "@"

You can use any of the predefined data formats by name. For example, to set the "#,##0" data format use:

Copy
<script>
    var wb = new Ax.ms.Excel();
    
    var sheet = wb.createSheet("test");
    var cell1 = sheet.getCell("A1");
    var cell2 = sheet.getCell("A2");
    cell1.setCellValue(200000000);
    cell2.setCellValue(200000000);
    
    var style = wb.createCellStyle();
    cell2.setCellStyle(style);

    // Set data format
    style.setDataFormat("#,##0");
    
    wb.setAutoSizeColumns();
    return wb;
</script>

6.2 Number format

TO DO

This section is incomplete and will be concluded as soon as possible.

6.3 Fonts

Fonts can be created from Workbook and assigned to a style.

Copy
<script>

    var wb = new Ax.ms.Excel();
    
    var sheet = wb.createSheet("test");
    var cell = sheet.getCell("A1");
    cell.setCellValue(200000000);

    var style = wb.createCellStyle();
    cell.setCellStyle(style);

    // Set font and size
    style.setFont(wb.createFont().setBold(true).setHeight(24));
    
    wb.setAutoSizeColumns();
    return wb;
</script>

Font style has serveral options as described below.

Return Method Description
JSExcelFont setFontName(String name) set the name for the font (i.e. Arial)
JSExcelFont setBold(boolean bold) set whether to use bold or not
JSExcelFont setItalic(boolean bold) set whether to use italics or not
JSExcelFont setStrikeout(boolean bold) set whether to use a strikeout horizontal line through the text or not
JSExcelFont setColor(IndexedColors color) set the color for the font.
JSExcelFont setFontHeight(int height) Set the font height in unit's of 1/20th of a point. Maybe you might want to use the setFontHeightInPoints which matches to the familiar 10, 12, 14 etc..
JSExcelFont setFontHeightInPoints(int height) Sets font height in the familiar unit of measure - points.

6.4 Borders

To setup a border style in a cell you must use any of the border types as defined in BorderStyle enum.

You can list border types by using:

Copy
<script>
    console.log(Ax.ms.Excel.BorderStyle);
</script>
[DOTTED, HAIR, MEDIUM, DASH_DOT_DOT, SLANTED_DASH_DOT, MEDIUM_DASH_DOT_DOT, DASHED, DASH_DOT, MEDIUM_DASH_DOT, MEDIUM_DASHED, DOUBLE, NONE, THICK, THIN]

You can control border style for left, right, bottom and top corners. Or set all borders using same style.

Copy
<script>

    var wb = new Ax.ms.Excel();
    
    var sheet = wb.createSheet("test");
    var cell1 = sheet.getCell("B2");
    var cell2 = sheet.getCell("D2");
    cell1.setCellValue(1000);
    cell2.setCellValue(2000);
    
    var style1 = wb.createCellStyle();
    var style2 = wb.createCellStyle();
    cell1.setCellStyle(style1);
    cell2.setCellStyle(style2);
    
    // Set border style
    style1.setBorderLeft(Ax.ms.Excel.BorderStyle.THICK);  
    style1.setBorderRight(Ax.ms.Excel.BorderStyle.THICK);  
    style2.setBorders(Ax.ms.Excel.BorderStyle.THICK);  
    
    wb.setAutoSizeColumns();
    return wb;
</script>

6.5 Colors

The enum Ax.ms.Excel.Color holding predefined colors is available in Workbook object for Apache POI IndexedColors class. The following examples lists the predefined colors constants.

Copy
<script>
    console.log(Ax.ms.Excel.Color);
</script>
[TAN, TURQUOISE1, TURQUOISE, LIGHT_TURQUOISE, LIGHT_GREEN, LIGHT_TURQUOISE1, VIOLET, BLACK, OLIVE_GREEN, GREY_80_PERCENT, GREY_50_PERCENT, AQUA, YELLOW1, BLUE_GREY, INDIGO, LIGHT_CORNFLOWER_BLUE, PLUM, BRIGHT_GREEN1, GREY_25_PERCENT, LIGHT_BLUE, LAVENDER, GREY_40_PERCENT, YELLOW, GOLD, LIGHT_ORANGE, CORAL, BLUE, WHITE1, RED1, PALE_BLUE, CORNFLOWER_BLUE, AUTOMATIC, BLUE1, ORCHID, SEA_GREEN, PINK, DARK_YELLOW, DARK_RED, BRIGHT_GREEN, ROYAL_BLUE, BROWN, BLACK1, LEMON_CHIFFON, ORANGE, WHITE, ROSE, GREEN, DARK_BLUE, LIGHT_YELLOW, MAROON, RED, PINK1, DARK_TEAL, SKY_BLUE, LIME, TEAL, DARK_GREEN]

6.5.1 Font color

Colors are assigned text font. So to change the color of a cell you need to create a style with an specific font with color.

The following example generates cells for all predefined colors.

Copy
<script>

    var wb = new Ax.ms.Excel();
    var sheet = wb.createSheet("test");
    
    var row = 0;
    for (var color of Ax.ms.Excel.Color) {
        var style = wb.createCellStyle();
        style.setFont(wb.createFont().setBold(true).setColor(color));
        
        var cell1 = sheet.getCell(row, 0).setCellValue(color + "");
        var cell2 = sheet.getCell(row, 1).setCellValue(color + "");
        cell2.setCellStyle(style);
        row++;
    }
    
    wb.setAutoSizeColumns();
    return wb;
</script>

6.5.2 Background color

Background color for a cell can be set in the style using setFillForegroundColor.

Copy
<script>

    var wb = new Ax.ms.Excel();
    
    var sheet = wb.createSheet("test");
    var cell1 = sheet.getCell("A1");
    var cell2 = sheet.getCell("A2");
    cell1.setCellValue(200000000);
    cell2.setCellValue(200000000);
    
    var style = wb.createCellStyle();
    cell2.setCellStyle(style);
    
    // Set data format
    style.setDataFormat("#,##0");
    
    // Set font and font color
    style.setFont(wb.createFont().setBold(true).setColor(Ax.ms.Excel.Color.WHITE).setHeight(16));

    // Set fill
    style.setFillForegroundColor(Ax.ms.Excel.Color.RED);
    
    wb.setAutoSizeColumns();
    return wb;
</script>

6.6 Align

To aling a cell you can use the Ax.ms.Excel.HorizontalAlignment enum from Workbook object.

To see the list of predefined HorizontalAlignment formats use:

Copy
<script>
    console.log(Ax.ms.Excel.HorizontalAlignment);
</script>
[CENTER, JUSTIFY, FILL, LEFT, CENTER_SELECTION, RIGHT, GENERAL, DISTRIBUTED]

To set the aligment in a style use setAligment method.

Copy
<script>
    var wb = new Ax.ms.Excel();
    
    var sheet = wb.createSheet("test");
    var cell = sheet.getCell("A1");
    cell.setCellValue("hello");

    var style = wb.createCellStyle();
    cell.setCellStyle(style);
    style.setAlign(Ax.ms.Excel.HorizontalAlignment.CENTER);
    
    return wb;
</script>

6.7 Text wrap

By default newlines are ignored and text is displayed in a single line. You can force wrap of text using setWrapText method.

Copy
<script>
    var wb = new Ax.ms.Excel();
    
    var sheet = wb.createSheet("test");
    var cell1 = sheet.getCell("A1");
    var cell2 = sheet.getCell("A2");
    cell1.setCellValue("Hello\nworld");
    cell2.setCellValue("Hello\nworld");
    
    var style = wb.createCellStyle();
    cell2.setCellStyle(style);

    // Set text wrap
    style.setWrapText(true);
    
    wb.setAutoSizeColumns();
    return wb;
</script>

7 Setting hyperlinks

You can create cell hyperlinks to external URL, MAIL and DOCUMENT references.

Copy
<script>
    var wb = new Ax.ms.Excel("test");
    var sheet1 = wb.createSheet("sheet1");
    var sheet2 = wb.createSheet("sheet2");
    
    // A HREF hyperlink
    sheet1.getCell("B5")
        .setCellValue("amazon")
        .setHyperlink("http://www.amazon.com")
        // Use default hyperlink style from workbook
        .setCellStyle(wb.getCellStyleHref());

    // A mail hyperlink
    sheet1.getCell("B6")
        .setCellValue("mailto")
        .setHyperlink("mailto:contact@acme.com?subject=Hyperlink")
        // Use default hyperlink style from workbook
        .setCellStyle(wb.getCellStyleHref());
    
    // A document hyperlink to other sheet and cell
    sheet2.getCell("D5").setCellValue("linked cell");
    sheet1.getCell("B7")
        .setCellValue("cell link")
        .setHyperlink("'Sheet2'!D5")
        // Use default hyperlink style from workbook
        .setCellStyle(wb.getCellStyleHref());
 
    return wb.toBlob();
</script>

In general you don't need to specify link type as setHyperlink automatically detects it. But if you need, you can send hyperlink type as second argument to function setHyperlink using values from Ax.ms.Excel.HyperlinkType enum.