简体   繁体   中英

Trying to write data from a website to an Excel spreadsheet

I am trying to take a string from a website using selenium and write that string to an Excel document.

        File file= new File("Q:\\A_Parts routing\\03_Systeme\\Selenium\\Vega Automatisierung Teil 1\\AutomatisierterSteuerungsantrag.xls"); 
        FileInputStream fis = new FileInputStream(file);
        //int j=1; j++;
        //Access the workbook
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        //Access the worksheet, so that we can update / modify it.
        HSSFSheet sheet = wb.getSheetAt(0);
        // declare a Cell object
        //HSSFCell cell = null; 
        //cell = sheet.getRow(1).getCell(11);
        System.out.println("VKEY "+VKEYVariable+ " is written in Excelsheet");
        // Access the 5. cell in second row to update the value
        //for (int r=1; r < sheet.getPhysicalNumberOfRows(); r++)
        try {
            HSSFRow row = sheet.getRow(i+1);
            if(row == null) {
                row = sheet.createRow(i+1);
            }
            Cell cell = (Cell) row.getCell(5);
            if (cell == null) {
                cell = (Cell) row.createCell(5);
            }
            ((HSSFCell) cell).setCellValue(IterateToEAMethods.VKEYVariable);
        } catch (Exception e) {
            System.out.println(e);
        }
        //Open FileOutputStream to write updates
        FileOutputStream fos =new FileOutputStream(file);
        //write changes
        wb.write(fos);
        

I am getting an error message printed out the console but I couldn't find anything on Google that indicated a problem.

java.lang.ClassCastException: org.apache.poi.hssf.usermodel.HSSFCell cannot be cast to jxl.Cell

在此处输入图像描述

can you share your pom.xml or build.gradle and show me the dependencies please especially where you have a dependency for JExcel Interface API. You may have older or incompatible Jexcel API direct or indirect dependencies. In your External Jar file check the Jxl version and use a higher version to it. the Jxl would look like this

<dependency>
<groupId>jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.4.2</version>
</dependency>

The exception is indicting that a cast to Cell is not possible because the HSSFCell class and its parents don't extend Cell . I was able to confirm this by taking a quick gander at the HSSFCell javadocs . At the top of the page, under java.lang.Object, the hierarchy of super classes is listed until you get down to HSSFCell. jxl.Cell does not appear in that hierarchy.

Looking at the javadocs for HSSFRow HSSFRow.getCell() and HSSFRow.createCell() both return an object type of HSSFCell .

If you declare cell as an HSSFCell object and remove the type casting you should be able to call the HSSFCell.setCellValue(java.lang.String) method and pass in your string.

HSSFCell cell = row.getCell(5);
if (cell == null) {
    cell = row.createCell(5);
}
cell.setCellValue(IterateToEAMethods.VKEYVariable);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM