【发布时间】:2020-12-30 08:09:24
【问题描述】:
我想在我的 excel 文件的特定单元格上写一些数据,但我总是遇到同样的错误。 我使用 Apache POI 写入和读取模板文件:
Exception in thread "Thread-4" org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : class org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream cannot be cast to class java.util.zip.ZipFile$ZipFileInputStream (org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream is in unnamed module of loader 'app'; java.util.zip.ZipFile$ZipFileInputStream is in module java.base of loader 'bootstrap')
主要:
private final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
private final File pathTemplate = newFile(Objects.requireNonNull(classLoader.getResource("excel/template.xlsx")).toURI());
public void updateRapport(int indexSheet, int rowwnum, int cellnum, String value, File file) throws IOException, InvalidFormatException {
Workbook workbook = WorkbookFactory.create(new File(file.getPath()));
// Get Sheet
Sheet sheet = workbook.getSheetAt(indexSheet);
System.out.println(sheet.getSheetName());
// Get Row
Row row = sheet.getRow(rowwnum);
// Get the Cell
Cell cell = row.getCell(cellnum);
// Update the cell
cell.setCellType(CellType.STRING);
cell.setCellValue(value);
// Write the output to the file
try(FileOutputStream fileOut = new FileOutputStream(file.getName()))
{
workbook.write(fileOut);
}
// Closing the workbook
workbook.close();
}
public static void main(String[] args) {
updateRapport(0,1,2,"ok",pathTemplate);
}
【问题讨论】: