写一个很长的值在apache POI中出色

当我使用Apache POI向Excelsheet中的某个单元格写入长整型值时,它会使用一些数字操作来编写它。 例:

cell = row.createCell(6); cell.setCellValue(someObt.getLongValue()); // long value returned is 365646975447 // in excel sheet, it is written as 3.65647E+11. // but when i click on the cell, i can see 365646975447 as the value. 

我希望它显示我写的确切值。 我怎样才能做到这一点?

单元格的types是一般的而不是数字的。

你需要做这个cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

如果您手动复制粘贴365646975447在types为常规的Excel单元格中,它将显示3.65647E + 11。 如果您将其更改为数字,则显示正确。

尝试这个:

 import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.Row; public class Test { public static void main(String[] args) throws Exception { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("format sheet"); CellStyle style; DataFormat format = wb.createDataFormat(); short rowNum = 0; short colNum = 0; Row row = sheet.createRow(rowNum++); Cell cell = row.createCell(colNum); cell.setCellValue(365646975447.0); style = wb.createCellStyle(); style.setDataFormat(format.getFormat("0.0")); cell.setCellStyle(style); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); } } 

另外检查: http : //poi.apache.org/spreadsheet/quick-guide.html