gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:29:49 26 4
gpt4 key购买 nike

我正在使用 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");

}

}

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

最佳答案

添加第三列

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

关于java - 使用单元格公式创建新列 apache poi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45344609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com