Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

java - using poi , How to set the Cell type as number

I am using poi 3.6

I am able to create the excel properly. But when I trying to set the cell type as number , it always give me cell type as general.

i.e. In the newly create excel , when I right click and go to format cell ->there I always found number to be a General.

My code is like this

style.setAlignment(CellStyle.ALIGN_RIGHT);
dataCell.setCellValue(Float.parseFloat(value as String);
dataCell.setCellType(Cell.CELL_TYPE_NUMERIC);
dataCell.setCellStyle(style);

Can you please suggest what is missing here ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try this approach too:

HSSFRow row = sheet.createRow(sheet.getLastRowNum());
HSSFCell cell = row.createCell(0);
HSSFCellStyle style = workbook.createCellStyle();
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
cell.setCellStyle(style);
cell.setCellValue(Float.parseFloat("21.5"));

Of course, take a look at the documentation and examples about data formats.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...