0

I'm trying to format Column Number 5 to number. I know that formatting can be done by setCellStyle but how can i implement the same in my below code?

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

1 Answer 1

1

You can set the default column style by passing a CellStyle object to the setDefaultColumnStyle() method for your sheet object. 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().

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply. When i tried to import org.apache.poi.ss , i'm getting package org.apache.poi.ss does not exist compilation error.
No idea why that would be, those packages are part of the same .jar as HSSFWorkbook and the other classes you used. I just downloaded the latest version of the library for this, and it's all in the poi-3.17.jar file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.