POI Java - 带小数的数字单元格

POI Java - Numeric Cell with Decimals

我在带有 POI 的单元格中遇到很多数字格式问题。

我需要在单元格中打印一个数字的 11 位小数,而且当您 select 数据

时,单元格有一个数字格式来求和

我有这个代码:

private void writeDecimal(HSSFRow row, Double data, int position) {
    String pattern = "#.0000000000";
    HSSFCell celda = row.createCell(position);
    CellStyle styleDecimal = styles.get(ITEM_DECIMAL); // Font and alignment
    styleDecimal.setDataFormat(libro.getCreationHelper().createDataFormat().getFormat(pattern));
    celda.setCellStyle(styleDecimal);
    celda.setCellType(Cell.CELL_TYPE_NUMERIC);
    celda.setCellValue(data);
}

但结果总是打印较少的小数点,因为 Excel 对数字进行四舍五入:

如果我将 Double 转换为 String,则打印 11 位小数,但如果我 select 所有数字,则不会求和。

知道如何解决这个问题吗?

谢谢

打开 xlsx 文件 select 列,右键单击单元格 -> 设置单元格格式...->自定义->类型 = 0.00000000000 单击确定。

现在无论您在该单元格上写什么,它都会以该格式打印,如果您select它也会显示总和。

按代码

import java.io.FileOutputStream;
import java.io.IOException;

    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;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;


    public class ChangeXlxsDataFormat {

        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub

             Workbook wb = new HSSFWorkbook();
                Sheet sheet = (Sheet) wb.createSheet("format sheet");
                CellStyle style;
                DataFormat format = wb.createDataFormat();
                Row row;
                Cell cell;
                short rowNum = 0;
                short colNum = 0;

                row = sheet.createRow(rowNum);
                cell = row.createCell(colNum);

                style = wb.createCellStyle();
                        row = sheet.createRow(rowNum++);
                cell = row.createCell(colNum);

                style = wb.createCellStyle();
                style.setDataFormat(format.getFormat("0.00000000000"));
                cell.setCellStyle(style);
                cell.setCellValue(5.12345678908);
                row = sheet.createRow(rowNum++);
                cell = row.createCell(colNum);
                cell.setCellValue(2.12345678908);
                FileOutputStream fileOut = new FileOutputStream("test.xls");
                wb.write(fileOut);
                fileOut.close();


        }

    }

此代码段不包含 celda.setCellStyle( styleDecimal )。真实代码中是否也缺少它?添加它。

编辑:

这是一个使用您的函数的简单示例。效果不错。

private static HSSFWorkbook workbook;

public static void main( String args[] ) throws IOException {
  workbook = new HSSFWorkbook();
  HSSFSheet sheet = workbook.createSheet( "sheet" );
  HSSFRow row = sheet.createRow( 0 );
  writeDecimal( row, 0.0781013, 0 );
  FileOutputStream fos = new FileOutputStream( "workbook.xls", false );
  workbook.write( fos );
  fos.close();
}

private static void writeDecimal( HSSFRow row, Double data, int position ) {
  String pattern = "#.0000000000";
  HSSFCell celda = row.createCell(position);
  CellStyle styleDecimal = workbook.createCellStyle(); // Font and alignment
  styleDecimal.setDataFormat(workbook.createDataFormat().getFormat(pattern));
  celda.setCellStyle(styleDecimal);
  celda.setCellType(Cell.CELL_TYPE_NUMERIC);
  celda.setCellValue(data);
}

您的代码中存在什么问题?我不知道。您的样式肯定没有应用到页面:零不应该用“#”呈现。