关于JXL公式生效的处理方法在网上查了很久,
看到有许多是这样写的
Fornual formual = new Formual(0,11,"Sum(A1:A9)");
wrb.addCell(formual);
在我这里是报错的,根据在这篇博客上的答案https://blog.csdn.net/ittraining/article/details/38709733发现处理应该是这样
Fornual f = new Formula(1, 11, "Sum(A1:A9)":, getDataCellFormat(CellType.NUMBER_FORMULA));
1和11是单元格的位置,Sum(A1:A9)是公式,getDataCellFormat(CellType.NUMBER_FORMULA)应该是数据处理格式
然后实现getDataCellFormat方法,代码如下:
public WritableCellFormat getDataCellFormat(CellType type){
WritableCellFormat wcf = null;
try {
//字体样式
if(type == CellType.NUMBER || type == CellType.NUMBER_FORMULA){//数字
NumberFormat nf = new NumberFormat("#.00");
wcf = new WritableCellFormat(nf);
}else if(type == CellType.DATE || type == CellType.DATE_FORMULA){//日期
jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-MM-dd hh:mm:ss");
wcf = new jxl.write.WritableCellFormat(df);
}else{
WritableFont wf =
new WritableFont(WritableFont.TIMES,10, WritableFont.NO_BOLD,false);
wcf = new WritableCellFormat(wf);
}
//对齐方式
wcf.setAlignment(Alignment.CENTRE);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
//边框
wcf.setBorder(Border.LEFT,BorderLineStyle.THIN);
wcf.setBorder(Border.BOTTOM,BorderLineStyle.THIN);
wcf.setBorder(Border.RIGHT,BorderLineStyle.THIN);
//背景色
wcf.setBackground(Colour.WHITE);
wcf.setWrap(true);//自动换行
} catch (WriteException e) {
e.printStackTrace();
}
return wcf;
}
亲测可行