[英]How to write on already open excel sheet in Selenium using java
I have 2 excel sheets. 我有2个Excel工作表。 Current my requirement is, I need to perform some action on excel column values and write that value in one of above mentioned excel sheets.
目前,我的要求是,我需要对excel列值执行一些操作,并将该值写入上述excel表格之一。 Below I mentioned the my code but it throws error at row3 line.
下面我提到了我的代码,但是它在row3行抛出错误。
File file=new File("D:\\Udaya files\\D-udaya files\\desktop_udaya files\\markting\\Gartner Dashboard\\Gartner Dashboard 28-12-2018\\AdminReportHTCoverPage.xlsx");
XSSFWorkbook wb=new XSSFWorkbook(file);
XSSFSheet sheet=wb.getSheetAt(1);
XSSFRow row=null;
int rowcount=sheet.getLastRowNum()+1;
File dashboard=new File("D:\\Udaya files\\D-udaya files\\desktop_udaya files\\markting\\Gartner Dashboard\\Gartner Dashboard 28-12-2018\\Weekly Dashboard_Garner users FY 18.xlsx");
XSSFWorkbook wb1=new XSSFWorkbook(dashboard);
XSSFSheet sheet1=wb.getSheet("December 28th");
XSSFRow row1=null;
int dashcount=sheet.getLastRowNum()+1;
int free=0;
int paid=0;
int paid_count=0;
for(int i=0;i<dashcount;i++)
{
String name=sheet1.getRow(i).getCell(1).getStringCellValue();
for(int j=0;j<rowcount;j++)
{
String firstname=sheet.getRow(i).getCell(1).getStringCellValue();
if(name.equals(firstname))
{
if(sheet.getRow(i).getCell(3).getStringCellValue()== "0" || sheet.getRow(i).getCell(3).getStringCellValue()== "")
{
free=free+1;
}
else
{
int value= (int) sheet.getRow(i).getCell(3).getNumericCellValue();
paid=paid+value;
paid_count=paid_count+1;
}
}
Row row3=sheet1.getRow(i);
row3.getCell(2).setCellValue(free);
row3.getCell(3).setCellValue(paid_count);
row3.getCell(4).setCellValue(paid);
}
}
getLastRowNum() not working properly for sheet1 file. getLastRowNum()对于sheet1文件不能正常工作。 It not displaying the correct number.
它没有显示正确的数字。
XSSFSheet.getLastRowNum()
is zero-based. XSSFSheet.getLastRowNum()
从零开始。 This is why you think that it does not represent the correct number if you get row number - 1
as a result. 这就是为什么您认为如果得到
row number - 1
结果row number - 1
并不能代表正确的数字。
If row number is 5
in the Excel Sheet, XSSFSheet.getLastRowNum()
will return 4. 如果Excel工作表中的行号为
5
,则XSSFSheet.getLastRowNum()
将返回4。
You can also find good examples in the link below; 您还可以在下面的链接中找到良好的示例; http://poi.apache.org/components/spreadsheet/quick-guide.html
http://poi.apache.org/components/spreadsheet/quick-guide.html
What kind of error you get in your code? 您在代码中遇到哪种错误?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.