Java 使用单元格公式ApachePOI创建新列

Java 使用单元格公式ApachePOI创建新列,java,apache-poi,Java,Apache Poi,我正在与ApachePOI合作,我遇到了一个问题,比如使用两列除法添加新列,还希望获得百分比% 这是我的意见 请求输出 这是java代码 public class ApacheCreatePivotTab { public static void main(String[] args) throws Exception { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.cre

我正在与ApachePOI合作,我遇到了一个问题,比如使用两列除法添加新列,还希望获得百分比%

这是我的意见

请求输出

这是java代码

public class ApacheCreatePivotTab
{
    public static void main(String[] args) throws Exception
    {
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet();
        //Create some data to build the pivot table on
        setCellData(sheet);

        FileOutputStream fileOut = new FileOutputStream("output.xlsx");
        wb.write(fileOut);
        fileOut.close();
        wb.close();
    }
    public static void setCellData(XSSFSheet sheet)
    {
        Row row1 = sheet.createRow(0);
        // Create a cell and put a value in it.
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("Names");
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("utilization_pct");


        Row row2 = sheet.createRow(1);
        // Create a cell and put a value in it.
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("Michal");
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue("6772.00902935");


        Row row3 = sheet.createRow(2);
        // Create a cell and put a value in it.
        Cell cell31 = row3.createCell(0);
        cell31.setCellValue("Michal");
        Cell cell32 = row3.createCell(1);
        cell32.setCellValue("6118.1434599");

        Row row4 = sheet.createRow(3);
        // Create a cell and put a value in it.
        Cell cell41 = row4.createCell(0);
        cell41.setCellValue("Michal");
        Cell cell42 = row4.createCell(1);
        cell42.setCellValue("5000");

        Row row5 = sheet.createRow(4);
        // Create a cell and put a value in it.
        Cell cell51 = row5.createCell(0);
        cell51.setCellValue("Michal");
        Cell cell52 = row5.createCell(1);
        cell52.setCellValue("5279.50310559");


        Row row6 = sheet.createRow(5);
        // Create a cell and put a value in it.
        Cell cell61 = row6.createCell(0);
        cell61.setCellValue("Henry");
        Cell cell62 = row6.createCell(1);
        cell62.setCellValue("6170.8860759");

    }

}
这段代码给了我一张工作表,我在上面执行了一个操作,将在其中添加第三列

以添加第三列

   Row row1 = sheet.createRow(0);
   // Create a cell and put a value in it.
   Cell cell11 = row1.createCell(0);
   cell11.setCellValue("Names");
   Cell cell12 = row1.createCell(1);
   cell12.setCellValue("utilization_pct");
   Cell cell13 = row1.createCell(2);  // create third column
   cell13.setCellValue("After_div and adding %");
将单元格格式更改为数字的步骤

     Row row4 = sheet.createRow(3);
     // Create a cell and put a value in it.
     Cell cell41 = row4.createCell(0);
     cell41.setCellValue("Michal");
     Cell cell42 = row4.createCell(1);
     cell42.setCellValue("5000");
     Cell cell43 = row4.createCell(2);

     HSSFCellStyle styleForNumeric = wb.createCellStyle();
     styleForNumeric.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00")); // format cell to numeric
     cell43.setCellStyle(styleForNumeric);
     cell43.setCellValue("50.00");

只需将其四舍五入到2个十进制数字。谢谢,但我想首先使用java中的apache poi库执行单元格操作,我如何在java中执行此操作?您想创建新列吗?是的,必须在新列中添加此内容我不完全理解您的问题,您想要第三列,对吗?为什么不使用新的单元实例创建它,并在其中添加值呢