亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用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 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號