首页 文章

Apache POI setCellType未正确设置数值的单元格类型

提问于
浏览
1

我正在尝试将一些数据导出到Excel文件中 . 我正在使用POI .

据我所知,Cell.setCellType(double)设置了相应的excel等效数值 . 但这并没有改变细胞的类型 .

打开生成的excel文件时的列类型是 Home tab 中的 General .

Poi Generated Cell Type for Numeric value

以下是测试它的示例代码 .

package com.example;

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestExcelMain {
    public static void main(String[] args) throws Exception {
        System.out.println("started");

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);

        cell.setCellValue(3.14159);

        workbook.write(new FileOutputStream("Test.xlsx"));

        System.out.println("finished");
    }
}

有没有其他方法可以将单元格类型设置为适当的类型?

我也尝试了Cell.setCellType(Cell.CELL_TYPE_NUMERIC)完全相同的结果 .

Correct type of cell that I expect is Number.

我也尝试使用所有 XSSF classess(下面的代码),如本文所述setCellType(HSSFCELL.CELL_TYPE_NUMERIC) is not working in apache poi

package com.example;

import java.io.FileOutputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestExcelMain {
    public static void main(String[] args) throws Exception {
        System.out.println("started");

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);

        cell.setCellValue(3.14159);
        cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);

        workbook.write(new FileOutputStream("Test.xlsx"));

        System.out.println("finished");
    }
}

Edit

正如Jim Garrison所回答的,设置单元格样式确实会将类型更改为数字 . 以下是工作代码 .

package com.example;

import java.io.FileOutputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestExcelMain {
    public static void main(String[] args) throws Exception {
        System.out.println("started");

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);

        cell.setCellValue(3.14159);
        cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);

        XSSFDataFormat format = workbook.createDataFormat();
        XSSFCellStyle style = workbook.createCellStyle(); 
        style.setDataFormat(format.getFormat("0.0"));

        cell.setCellStyle(style);

        workbook.write(new FileOutputStream("Test.xlsx"));

        System.out.println("finished");
    }
}

1 回答

  • 5

    来自Javadoc的 setCellValue(double value) (强调我的):

    value - 要设置此单元格的数值 . 对于公式,我们将设置预先计算的值,对于数值,我们将设置其值 . 对于其他类型,我们将单元格更改为数字单元格并设置其值 .

    因此,无论之前的细胞是什么,此方法无论如何都会将细胞类型更改为数字 .

    请注意,"General"与单元格类型无关,而与单元格格式有关 . 要更改单元格的显示方式,请使用 setCellStyle() .

相关问题