2
votes

I'm an user of Apache POI and i make excel file (.xlsx) and i need to use the conditional formatting on a cell. My problem is this : i've a formula, if this formula returns a value that is between 0 and 1 i want show the result with one decimal at the contrary i want to show the result with any decimal. It is possible use the conditional formatting? If yes, how i can do it?

5

5 Answers

2
votes
Cell new_cell = row.createCell(index_column_found);
XSSFCellStyle cs = wBook.createCellStyle();
XSSFDataFormat df = wBook.createDataFormat();
cs.setDataFormat(df.getFormat(cell_custom_format));
new_cell.setCellStyle(cs);

This work very well with POI 3.12

1
votes

Here is the code that will satisfy your requirement:

FileOutputStream out = new FileOutputStream("dateFormat.xls");
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfworkbook.createSheet("new sheet");
HSSFCellStyle cs = hssfworkbook.createCellStyle();
HSSFDataFormat df = hssfworkbook.createDataFormat();
cs.setDataFormat(df.getFormat("#,##0.0"));

for(int i=0 ;i <100 ; i++ )
{
    double value = new Random(i).nextGaussian();
    HSSFRow row = sheet.createRow((short) i);
    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue(value);
    if(value>=0 && value<=1)
        cell.setCellStyle(cs);
}

hssfworkbook.write(out);
out.close();
0
votes
    style.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));

by using this dataformat we can assighn value as string to the cell.

0
votes

Hi there is no official way but i got it running with an small hack:

static void setNumberFormat(final XSSFConditionalFormattingRule r,final XSSFSheet s, final String format) {
    final int id = s.getWorkbook().createDataFormat().getFormat(format);
    try {
        final Method m = XSSFConditionalFormattingRule.class.getDeclaredMethod("getDxf", boolean.class);
        m.setAccessible(true);
        final org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxf dxf = (CTDxf)m.invoke(r, true);
        final org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmt nf = dxf.addNewNumFmt();
        nf.setNumFmtId(id);
        nf.setFormatCode(format);
        dxf.setNumFmt(nf);
    } catch(final Throwable t) {
        t.printStackTrace();
    }
}

Usage:

private static final String HUMAN1_BYTES = "[<1000]#0\"  B\";[<1000000]#0,\" KB\";#0,,\" MB\"";
private static final String HUMAN2_BYTES = "[<1000000000000]#0,,,\" GB\";[<1000000000000000]#0,,,,\" TB\";#0,,,,,\" PB\"";

public static void addConditionalFormattingRule(final XSSFSheet sheet, final CellRangeAddress[] regions, final byte op, final long limit, final String format) {
    final XSSFSheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
    final XSSFConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule(op, Long.toString(limit));
    setNumberFormat(rule, sheet, format);
    sheetCF.addConditionalFormatting(regions, rule);
}

public static void setHumanSizeColumns(final XSSFSheet sheet, final char... cols) {
    if(cols == null) return;
    final CellRangeAddress[] ranges = new CellRangeAddress[cols.length];
    for(int i=0;i<cols.length;++i) ranges[i] = CellRangeAddress.valueOf("$"+cols[i]+"1:$"+cols[i]+"1048576");
    addConditionalFormattingRule(sheet, ranges, ComparisonOperator.LT, 1000000000, HUMAN1_BYTES);
    addConditionalFormattingRule(sheet, ranges, ComparisonOperator.GE, 1000000000, HUMAN2_BYTES);
}