我正在尝试将文本文件转换为 Excel ......
在这样做时,会发生以下异常:
Exception in thread "main" java.lang.NoSuchMethodError:
org.apache.poi.poifs.filesystem.POIFSFileSystem.hasPOIFSHeader(Ljava/io/InputStream;)Z
at org.apache.poi.ss.usermodel.WorkbookFactory.create()
我怎样才能避免这种情况?我正在提供我的片段。
public void convertCSV(String textfile) throws Exception {
FileInputStream inputStr = new FileInputStream(new File("/home/directory/Desktop/HI/excel.xls"));
Workbook HssfWork = WorkbookFactory.create(inputStr);
Sheet sheet= HssfWork.getSheetAt(0);
int rowNo = 0;
int columnNo = 0;
Cell cell = null;
// Loop through the files.
//for(File file : csvFiles) {
Scanner scanner = new Scanner(textfile);
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
// Gets the row and if it doesn't exist it will create it.
Row Row = sheet.getRow(rowNo);
if(Row == null)
Row = sheet.createRow(rowNo);
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("\t");
// While there is more text to get it will loop.
while(lineScanner.hasNext()) {
// Gets the cell in that row and if it is null then it will be created.
org.apache.poi.ss.usermodel.Cell tempCell =Row.getCell(columnNo,org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK);
String output = lineScanner.next();
// Write the output to that cell.
tempCell.setCellValue(output);
columnNo++;
}
// Resets the column count for the new row.
columnNo = 0;
rowNo++;
}
// Writes the file and closes everything.
FileOutputStream out = new FileOutputStream(excelFile);
HssfWork.write(out);
inputStr.close();
out.close();
}