Programmer-yyds commented on issue #879: URL: https://github.com/apache/poi/issues/879#issuecomment-3190828729
How do I delete temporary files if I use SXSSFWorkbook? SXSSFWorkbook has no getPackage () method. ``` public class ExcelImageWriter { static { // 启用临时文件模式,降低内存占用 ZipPackage.setUseTempFilePackageParts(true); System.setProperty("java.io.tmpdir", "/Users/shanjiacong/Desktop/temp"); } private final SXSSFWorkbook workbook; private final SXSSFSheet sheet; private final SXSSFDrawing drawing; private final AtomicInteger rowIndex = new AtomicInteger(0); private final String outputPath; public ExcelImageWriter(String outputPath) { this.workbook = new SXSSFWorkbook(100); this.sheet = workbook.createSheet("Images"); this.drawing = sheet.createDrawingPatriarch(); this.outputPath = outputPath; } public static void main(String[] args) throws Exception { ExcelImageWriter writer = new ExcelImageWriter("/Users/shanjiacong/Desktop/temp/output.xlsx"); byte[] bytes = readImageBytes("/Users/shanjiacong/Downloads/wps-excel-embed-image-main/test02.jpg"); for (int i = 0; i < 5; i++) { writer.addImage(bytes); } writer.save(); writer.close(); System.out.println("writeCompleted!"); } private static byte[] readImageBytes(String path) { try (InputStream is = new FileInputStream(path)) { return IOUtils.toByteArray(is); } catch (IOException e) { throw new RuntimeException(e); } } private static int detectPictureType(byte[] bytes) { try { String mimeType = URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(bytes)); if ("image/png".equals(mimeType)) { return Workbook.PICTURE_TYPE_PNG; } else if ("image/jpeg".equals(mimeType)) { return Workbook.PICTURE_TYPE_JPEG; } else { throw new IllegalArgumentException("Unsupported image type: " + mimeType); } } catch (IOException e) { throw new RuntimeException(e); } } public void addImage(byte[] image) { int rowNum = rowIndex.getAndIncrement(); Row row = sheet.createRow(rowNum); row.setHeightInPoints(60); int pictureIndex = workbook.addPicture(image, detectPictureType(image)); // 列1行(rowNum) 到 列2行(rowNum+1) XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 0, rowNum, 1, rowNum + 1); anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); drawing.createPicture(anchor, pictureIndex); } public void save() { try (FileOutputStream fos = new FileOutputStream(outputPath)) { workbook.write(fos); } catch (IOException e) { throw new RuntimeException(e); } } public void close() { try { workbook.close(); workbook.dispose(); // 删除临时文件 } catch (IOException e) { throw new RuntimeException(e); } } } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org For additional commands, e-mail: dev-h...@poi.apache.org