SXSSFWorkbook插入图片,java代码
时间: 2024-01-15 20:19:10 浏览: 189
以下是使用SXSSFWorkbook插入图片的Java代码示例:
```java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class InsertImageExample {
public static void main(String[] args) {
// 创建工作簿
SXSSFWorkbook workbook = new SXSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
// 加载图片
try {
Drawing<?> drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 5, 8);
int pictureIndex = workbook.addPicture(InsertImageExample.class.getResourceAsStream("image.jpg"), Workbook.PICTURE_TYPE_JPEG);
Picture picture = drawing.createPicture(anchor, pictureIndex);
picture.resize();
// 保存工作簿
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
workbook.write(fileOut);
fileOut.close();
System.out.println("图片插入成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文