【问题标题】:Java : class org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream cannot be cast to class java.util.zip.ZipFile$ZipFileInputStreamJava:类 org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream 不能转换为类 java.util.zip.ZipFile$ZipFileInputStream
【发布时间】: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);
        }

【问题讨论】:

    标签: java openxml


    【解决方案1】:

    这是因为当您的资源在 jar 文件中时,您不能将其视为文件。
    如果需要文件,请将资源内容写入临时文件,然后使用它。
    像这样的:

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.file.Files;
    import java.nio.file.StandardCopyOption;
    import org.junit.Test;
    
    public class FirstTest {
        @Test
        public void resourceTest() throws IOException {
            final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            final InputStream resource = classLoader.getResourceAsStream("resource");
            final File file = new File("d:/temp", "fileName");
            Files.copy(resource, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
        }
    }
    

    【讨论】:

    • 感谢您的回答!如何写入我的临时文件?
    猜你喜欢
    • 2021-02-27
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2012-05-22
    相关资源
    最近更新 更多