[英]org.apache.poi for xls write number format issue
我的Java程序使用org.apache.poi来编写xls文档。 使用XL 2010使用正确的数字格式生成xls文件,但是使用XL 2013的数字未格式化,并且小数点后显示6位数字
示例:在xl 2010中,预期的12.123445显示为12.12
在xl 2013中,12.123445显示为12.123225,这不应该在小数点后舍入两位两位。
我尝试将org.apage.poi pom.xml升级为最新的3.14、3.16-beta2。
当前使用3.10-FINAL。
知道如何解决吗? 或任何可用于xl单元格值的org.apache.poi方法将其设置为数字,并舍入到小数点后两位。
程式码片段:
style = createStyle(sheet.getWorkbook(), (short) 10, fontWeight, fontColor);
Double number = Double.parseDouble(colHeader);
cell.setCellValue(number);
style.setDataFormat(format.getFormat("0.00"));
cell.setCellStyle(style);
createStyle():
protected HSSFCellStyle createStyle(Workbook wb, short fontSize, short boldWeight, short fontColor) {
Font font = fontMap.get(getMapKey(fontSize, boldWeight, fontColor));
if (font == null) {
font = wb.createFont();
font.setFontHeightInPoints(fontSize);
font.setFontName("Arial");
font.setBoldweight(boldWeight);
font.setColor(fontColor);
fontMap.put(getMapKey(fontSize, boldWeight, fontColor), font);
}
HSSFCellStyle style = ((HSSFWorkbook) wb).createCellStyle();
style.setFont(font);
return style;
}
我使用poi-bin-3.15-20160924 Jars创建了一个可执行示例,该jars将双精度格式从(12.123445)设置为(12.12),将(12.125)设置为(12.13):
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
public class WriteReadXlsFormula {
public static void main(String[] args){
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Calculate Simple Interest");
//To format a double
HSSFCellStyle style = workbook.createCellStyle();
style.setDataFormat(
workbook.getCreationHelper().createDataFormat().getFormat("#,##0.00"));
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("Pricipal Amount (P)");
header.createCell(1).setCellValue("Rate of Interest (r)");
header.createCell(2).setCellValue("Tenure (t)");
header.createCell(3).setCellValue("Interest (P r t)");
//Don't use the style and use DicimalFormat (comment 3 line of code under Cell contain the number to format comment)
/*
DecimalFormat df = new DecimalFormat("####.00");
dataRow.createCell(1).setCellValue(df.format(12.125445));
*/
Row dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue(14500d);
//Cell contain the number to format
dataRow.createCell(1).setCellType(CellType.NUMERIC);
dataRow.getCell(1).setCellStyle(style);
dataRow.getCell(1).setCellValue(df1.format(12.125445));
dataRow.createCell(2).setCellValue(3d);
dataRow.createCell(3).setCellFormula("A2*B2*C2");
try {
FileOutputStream out =
new FileOutputStream(new File("formula.xls"));
workbook.write(out);
out.close();
workbook.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.