简体   繁体   中英

Converting into number format while writing from text file to excel in java

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

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. Eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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