简体   繁体   English

使用Java从字符串到数字格式化Excel单元格

[英]Formatting excel cells using Java from String to Number

I'm trying to format Column Number 5 to number. 我正在尝试将第5列的格式设置为数字。 I know that formatting can be done by setCellStyle but how can i implement the same in my below code? 我知道格式化可以通过setCellStyle完成,但是如何在下面的代码中实现相同的格式?

FileInputStream ExcelFileToRead = null;
HSSFWorkbook wb = null;
FileOutputStream outputStream = null;
short colNum = 5;

try {
 ExcelFileToRead = new FileInputStream("config/test/test.xls");
} catch (FileNotFoundException e) {
 e.printStackTrace();
}
try {
 wb = new HSSFWorkbook(ExcelFileToRead);
} catch (IOException e) {
 e.printStackTrace();
}
HSSFSheet sheet = wb.getSheetAt(0);
HSSFCell cell = null;
HSSFRow row = null;

sheet.createRow(0).createCell((short) 6).setCellValue("100");
sheet.createRow(3).createCell(colNum).setCellValue("120");
sheet.createRow(5).createCell(colNum).setCellValue("300");
sheet.createRow(11).createCell(colNum).setCellValue("500");
sheet.createRow(15).createCell(colNum).setCellValue("900");
sheet.createRow(18).createCell(colNum).setCellValue("1000");
sheet.createRow(23).createCell(colNum).setCellValue("10");
sheet.createRow(28).createCell(colNum).setCellValue("20");
sheet.createRow(30).createCell(colNum).setCellValue("30");
sheet.createRow(49).createCell(colNum).setCellValue("40");

outputStream = new FileOutputStream("config/test/test.xls");
wb.write(outputStream);
outputStream.close();

You can set the default column style by passing a CellStyle object to the setDefaultColumnStyle() method for your sheet object. 您可以通过将CellStyle对象传递给工作表对象的setDefaultColumnStyle()方法来设置默认列样式。 The format is parsed from the string that you pass. 格式是从您传递的字符串中解析出来的。 So "0" is an integer, "0.0" is a decimal to 1 decimal place, "0.00" is a decimal to 2 decimal places, etc. There's a lot more to this, and you can see the list of formats using HSSFDataFormat.getBuiltinFormats() . 因此, "0"是一个整数, "0.0"是一个十进制到1个小数位, "0.00"是一个十进制到2个小数位, HSSFDataFormat.getBuiltinFormats()还有很多其他内容,您可以使用HSSFDataFormat.getBuiltinFormats()查看格式列表HSSFDataFormat.getBuiltinFormats()

You should also pass your values in as a number type rather than a string, otherwise they cell values will be text rather than a number. 您还应该以数字类型而不是字符串的形式传递值,否则它们的单元格值将是文本而不是数字。

Here's an example setting the whole column as number. 这是将整个列设置为数字的示例。

public static void main(String[] args) throws Exception {
    short colNum = 5;

    FileInputStream workbookStream = new FileInputStream("config/test/test.xls");
    HSSFWorkbook workbook = new HSSFWorkbook(workbookStream);

    HSSFSheet worksheet =  workbook.getSheetAt(0);

    // set column style here
    DataFormat dataFormat = workbook.createDataFormat();
    CellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setDataFormat(dataFormat.getFormat("0"));
    worksheet.setDefaultColumnStyle(colNum, cellStyle);

    worksheet.createRow(0).createCell((short)6).setCellValue(100);
    //...
    worksheet.createRow(49).createCell(colNum).setCellValue(40);

    FileOutputStream outputStream = new FileOutputStream("config/test/test.xls");
    workbook.write(outputStream);
    outputStream.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM