【发布时间】:2021-06-28 17:09:00
【问题描述】:
我正在尝试使用 selenium 从网站获取字符串并将该字符串写入 Excel 文档。
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);
我在控制台中打印出一条错误消息,但我在 Google 上找不到任何表明存在问题的信息。
java.lang.ClassCastException: org.apache.poi.hssf.usermodel.HSSFCell cannot be cast to jxl.Cell
【问题讨论】:
-
嗯,信息真的很清楚。你有一个类的实例,你试图把它转换成它不是的东西。告诉鸡蛋“你现在是一条鱼”也行不通。所以,问问自己为什么要写下那个演员表。它的目的是什么?为什么你认为你要投射的对象是那个特定的类?
-
您能否分享您的 pom.xml 或构建 .gradle 并向我展示依赖关系,尤其是在您依赖 JExcel 接口 API 的地方