2017-07-27 266 views
0

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

    } 

} 

此代码给我我所进行的操作,将其添加第三列一个表

+0

将其舍入为2个十进制数。 –

+0

谢谢,但我想首先在java中使用apache poi库执行单元操作如何在java中执行此操作 –

+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"); 
相关问题