0

We have one existing application where we read one text file and write into excel using java. Text file will have first row as header and subsequent rows as records which is fetched from database. Currently while writing into excel, all the columns are getting converted into text format. We want to convert one column(consider column Z) as number.

String [] column = line.split("\\|~");
   for (int i = 0; i < column.length; i++) {
                    Cell tmpCell =  row.createCell(i);

                    tmpCell.setCellValue(new HSSFRichTextString(column[i].trim()));

                }

I am new to java, need your help in resolving this issue.

Thanks Santhosh

1 Answer 1

2

You can write to cell like this

cell.setCellValue(12345.00000)

But this alone will not be enough in case where you don't want to truncate. Using style and dataformats, you can avoid truncation. E.g.

CellStyle cellStyle = wb.createCellStyle();
DataFormat df = wb.createDataFormat();
cellStyle.setDataFormat(df.getFormat("0.0")); //you can adjust it as per your requirments
cell.setCellStyle(cellStyle);
Sign up to request clarification or add additional context in comments.

2 Comments

I just added cell.setCellType(Cell.CELL_TYPE_NUMERIC); and it worked good for me.
@Santhosha setCellType is deprecated. Do you have any other solution?

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.