[英]Trying to write data from a website to an Excel spreadsheet
我正在尝试使用 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 或 build.gradle 并向我展示依赖关系,尤其是在你依赖 JExcel 接口 API 的地方。 您可能有较旧或不兼容的 Jexcel API 直接或间接依赖项。 在您的外部 Jar 文件中检查 Jxl 版本并使用更高版本。 Jxl 看起来像这样
<dependency>
<groupId>jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.4.2</version>
</dependency>
例外情况是,由于 HSSFCell class 及其父级不扩展Cell
Cell
我可以通过快速浏览一下HSSFCell javadocs来确认这一点。 在页面顶部的 java.lang.Object 下,列出了超类的层次结构,直到您进入 HSSFCell。 jxl.Cell 不会出现在该层次结构中。
查看 HSSFRow HSSFRow.getCell()
和HSSFRow.createCell()
的 javadocs 都返回 object 类型的HSSFCell
。
如果您将cell
声明为HSSFCell
object 并删除类型转换,您应该能够调用HSSFCell.setCellValue(java.lang.String)
方法并传入您的字符串。
HSSFCell cell = row.getCell(5);
if (cell == null) {
cell = row.createCell(5);
}
cell.setCellValue(IterateToEAMethods.VKEYVariable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.