我正在尝试读取xlsx文件,但我收到错误“工作簿无法解析类型”。如何解决?
InputStream input = new BufferedInputStream(
new FileInputStream("F://myFile.xlsx"));
POIFSFileSystem fs = new POIFSFileSystem( input );
Workbook wb1 = WorkbookFactory.create(new File(fs)); // error is here
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while( rows.hasNext() ) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println("\n");
Iterator cells = row.cellIterator();
答案 0 :(得分:1)
Java无法直接读取Excel文件。您需要第三方库来完成此任务。从给定的代码看,您需要添加Apache POI中的库,特别是POI-HSSF and POI-XSSF。
答案 1 :(得分:0)
首先,您需要在类路径中包含所有适当的Apache POI component jars and their dependencies。其次,您需要导入POI类。
一旦下载了Apache POI并添加了components to your classpath,您的代码就会如下所示:
import org.apache.poi.ss.usermodel.*;
public class POIExample {
public static void main(String[] args) {
File f = new File("F://myFile.xlsx"));
Workbook wb1 = WorkbookFactory.create(f);
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
System.out.println("\n");
for (Cell cell : row) {
System.out.println("Found Cell");
}
}
}
}