使用单元格公式apache poi创建新列

时间:2017-07-27 07:54:52

标签: java apache-poi

我正在使用apache poi,我有一个问题,例如使用两列分区添加新列,并且还希望获得百分比(%)

这是我的意见 enter image description here

请求输出

enter image description here

这是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");

    }

}

这段代码给了我一张表,我已经执行了将在其中添加第三列的操作

1 个答案:

答案 0 :(得分:1)

添加第三列

   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");