简体   繁体   English

如何使用Java在Selenium中已经打开的Excel工作表上书写

[英]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.

相关问题 通过使用 java selenium 如何在 Excel 表中写入数据 - By using java selenium how to write data in Excel sheet 如何使用Java打开Excel工作表? - How to open an excel sheet using Java? 如何使用Java for Selenium WebDriver在一个Excel工作表中编写2种不同的结果 - How to write the 2 different results in one excel sheet using java for selenium webdriver 数据驱动框架——如何使用Selenium WebDriver和java在excel表中读写 - Data Driven Framework — how to read and write in excel sheet using Selenium WebDriver with java 如何在Java中使用硒单击Excel工作表中的按钮 - How to click on button inside Excel Sheet using selenium in java 如何通过创建动态Web项目使用java写入excel表 - How to write into excel sheet using java by creating dynamic web project 如何使用jlx在Java的同一Excel工作表中写入数据循环? - How to write loop of data in the same excel sheet in Java using jlx? 如何使用Java + Struts2在浏览器中打开Excel工作表? - How to open an Excel sheet in browser using Java+Struts2? 在for循环中使用row.getlastcellnum()方法在Selenium中写入Excel工作表会导致java.lang.IllegalArgumentException - Write excel sheet in Selenium using row.getlastcellnum() method in for loop causes java.lang.IllegalArgumentException 无法使用硒中的poi jar将所有记录写到excel工作表中 - not able to write all the record in excel sheet using poi jar in selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM