Apache POI如何使用条件格式更改dataformat

时间:2012-12-04 09:04:46

标签: java excel apache-poi xlsx

我是Apache POI的用户,我制作excel文件(.xlsx),我需要在单元格上使用条件格式。我的问题是这样的:我有一个公式,如果这个公式返回一个介于0和1之间的值,我希望显示一个十进制的结果,我想用任何小数显示结果。有可能使用条件格式?如果是的话,我该怎么办?

5 个答案:

答案 0 :(得分:2)

error_reporting(E_ALL);

这项工作与POI 3.12

非常吻合

答案 1 :(得分:1)

以下是满足您要求的代码:

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();

答案 2 :(得分:0)

使用DataFormatter格式化单元格值。

检查示例:http://www.roseindia.net/java/poi/setdataformat.shtml

API链接:http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html

这可能会对你有帮助。

答案 3 :(得分:0)

    style.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));

通过使用这种数据格式,我们可以将值作为字符串赋值给单元格。

答案 4 :(得分:0)

您好,没有官方的方法,但是我通过一个小技巧使其运行:

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();
    }
}

用法:

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);
}