見下面我的代碼:數據輸入Excel表,在輸出Excel工作表中自動顯示
FileInputStream fis=new FileInputStream("E://Pincodes Task//Pincodes-List.xlsx");
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sh=wb.getSheetAt(0);
int totalNoOfRows = sh.getLastRowNum();
for(int i = 1; i < totalNoOfRows; i++) {
XSSFRow row=sh.getRow(i);
pincode=new DataFormatter().formatCellValue(row.getCell(0));
fis.close();
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txtPincodeSearch"))
.sendKeys(pincode);
driver.findElement(By.name("ctl00$ContentPlaceHolder1$btnPincodeSearch")).click();
if(driver.findElement(By.xpath(".//*[@id='ctl00_ContentPlaceHolder1_lblNoResult']")).isDisplayed()) {
String noData="No data for "+pincode+" code";
XSSFSheet sh2=wb.getSheetAt(0);
XSSFRow row2=sh2.createRow(i);
row2.createCell(0).setCellValue(noData);
FileOutputStream fos=new FileOutputStream("E://Pincodes Task//Output-List.xlsx");
wb.write(fos);
fos.close();
} else {
if(driver.findElement(By.id("ctl00_ContentPlaceHolder1_grdPincode_ctl02_HyperLink1")).isEnabled()) {
driver.findElement(By.id("ctl00_ContentPlaceHolder1_grdPincode_ctl02_HyperLink1")).click();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
data=driver.findElement(By.xpath(".//*[@id='ctl00_ContentPlaceHolder1_TableCell4']")).getText();
XSSFSheet sh2=wb.getSheetAt(0);
XSSFRow row2=sh2.createRow(i);
row2.createCell(0).setCellValue(data);
FileOutputStream fos=new FileOutputStream("E://Pincodes Task//Output-List.xlsx");
wb.write(fos);
fos.close();
}
}
我需要從一個Excel工作表中讀取輸入,我需要保存的數據從網站採取的另一個Excel文件,但輸入數據會自動保存在輸出文件中。
你需要讓輸出XLSX單獨XSSFWorkbook比如,你正在使用的輸入和輸出XLSX – vvtx
Thanq @vvtx相同的實例您的回覆,我創建單獨的工作簿例如像這個 XSSFWorkbook wb2 = null;但我得到異常 - 線程「主」java.lang.NullPointerException異常 \t在PostalDetails.main(PostalDetails.java:49) – Krishna
代碼重新格式化,大寫 –