我是 Apache POI 的用户,我制作了 excel 文件 (.xlsx),我需要在单元格上使用条件格式。我的问题是:我有一个公式,如果这个公式返回一个介于 0 和 1 之间的值,我想用一位小数显示结果,相反,我想用任何小数显示结果。可以使用条件格式吗?如果是,我该怎么做?
问问题
20746 次
5 回答
2
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);
这与 POI 3.12 配合得很好
于 2016-03-03T19:45:13.313 回答
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();
于 2012-12-04T13:00:26.277 回答
0
使用 DataFormatter 格式化您的单元格值。
检查示例:http ://www.roseindia.net/java/poi/setdataformat.shtml
API 链接: http: //poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html
这可能会帮助你。
于 2012-12-04T09:17:08.677 回答
0
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));
通过使用这种数据格式,我们可以将值作为字符串分配给单元格。
于 2012-12-06T11:43:52.727 回答
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);
}
于 2019-08-08T23:04:12.417 回答