为了账号安全,请及时绑定邮箱和手机立即绑定

使用java在excel中的两列中写入多条记录

使用java在excel中的两列中写入多条记录

阿波罗的战车 2023-10-13 16:27:37
我有两列,即Excel中的Setup和Request,需要使用java在Excel中写入多行public void WriteExcelValues() throws IOException {String Ustr= setup.getText();String urequest = request.getText();    File file = new File(System.getProperty("user.dir")                             + "/src/main/uat/testdata/FreeTestData.xlsx");    FileInputStream fis = new FileInputStream(file);    Workbook workbook = new XSSFWorkbook(fis);    Sheet sheet = workbook.getSheetAt(0);    int lastRow = sheet.getLastRowNum();    int n = 1;    for (int i = 1; i <= n; i++) {           Row row = sheet.getRow(i);         Cell cell = row.createCell(8);        Cell cell1 = row.createCell(9);        cell.setCellValue(Ustr);        cell1.setCellValue(urequest);        //cell.setCellValue("AcknowledgementId");     }    FileOutputStream fos = new FileOutputStream(file);       workbook.write(fos);    fos.close();}例如上面的代码并不完整,也不满足标准。
查看完整描述

2 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

Workbook workbook = new XSSFWorkbook();


Sheet sheet = workbook.createSheet();


Row headerRow = sheet.createRow(0);


headerRow.createCell(0).setCellValue("Setupid");

headerRow.createCell(1).setCellValue("Request");


int cellCount = 10;


for (int i = 1; i <= cellCount; i++) {


    Row row = sheet.createRow(i);


    row.createCell(0).setCellValue("cell " + i);

    row.createCell(1).setCellValue("cell " + i);

}


File file = new File(System.getProperty("user.dir") 

                            + "/src/main/uat/testdata/FreeTestData.xlsx");


if(file.exists()) {

    file.delete();

}


file.createNewFile();


FileOutputStream fos = new FileOutputStream(file);


workbook.write(fos);


fos.close();


workbook.close();


查看完整回答
反对 回复 2023-10-13
?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

我认为你的 for 循环不正确

 for(int i=1; i<=n; i++){

它应该是

for(int i=1; i<=lastRow; i++){


查看完整回答
反对 回复 2023-10-13
  • 2 回答
  • 0 关注
  • 64 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信