使用 Apache POI 在 Excel 列中插入图像
Insert image in column to Excel using Apache POI
我正在尝试将图像插入到 Excel 中的单元格中。我已经很好地添加了图片,但我仍然可以在任何地方运行。我想说我想要这个专栏
可以先设置行列再设置图片
try {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("MYSheet");
InputStream inputStream = new FileInputStream("path_to_image.jpg");
byte[] imageBytes = IOUtils.toByteArray(inputStream);
int pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);
inputStream.close();
CreationHelper helper = workbook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(2);
drawing.createPicture(anchor, pictureureIdx);
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("output.xlsx");
workbook.write(fileOut);
fileOut.close();
}catch (Exception e) {
System.out.println(e);
}
我在 excel sheet 中定位图像时遇到了一些问题。这是对我有用的代码(Microsoft Excel 2019 版本 2110)
public static void main(String[] args) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
Path imagePath = Path.of("...path to your image...");
byte[] imageContent = Files.readAllBytes(imagePath);
int pictureIndex = workbook.addPicture(imageContent, Workbook.PICTURE_TYPE_PNG);
// Option 1: use constructor, parameters 5-8 define starting cell-row and ending cell-row for image position
// I have no clue what first 4 parameters are doing
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 3, 3, 6, 12);
// Option 2: use Creation Helper and setters for defining starting and ending cell
XSSFClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
anchor.setCol1(3);
anchor.setRow1(3);
anchor.setCol2(6);
anchor.setRow1(12);
XSSFDrawing drawingPatriarch = sheet.createDrawingPatriarch();
drawingPatriarch.createPicture(anchor, pictureIndex);
workbook.write(new FileOutputStream("output.xlsx"));
}
Maven 依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>
我正在尝试将图像插入到 Excel 中的单元格中。我已经很好地添加了图片,但我仍然可以在任何地方运行。我想说我想要这个专栏
可以先设置行列再设置图片
try {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("MYSheet");
InputStream inputStream = new FileInputStream("path_to_image.jpg");
byte[] imageBytes = IOUtils.toByteArray(inputStream);
int pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);
inputStream.close();
CreationHelper helper = workbook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(2);
drawing.createPicture(anchor, pictureureIdx);
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("output.xlsx");
workbook.write(fileOut);
fileOut.close();
}catch (Exception e) {
System.out.println(e);
}
我在 excel sheet 中定位图像时遇到了一些问题。这是对我有用的代码(Microsoft Excel 2019 版本 2110)
public static void main(String[] args) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
Path imagePath = Path.of("...path to your image...");
byte[] imageContent = Files.readAllBytes(imagePath);
int pictureIndex = workbook.addPicture(imageContent, Workbook.PICTURE_TYPE_PNG);
// Option 1: use constructor, parameters 5-8 define starting cell-row and ending cell-row for image position
// I have no clue what first 4 parameters are doing
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 3, 3, 6, 12);
// Option 2: use Creation Helper and setters for defining starting and ending cell
XSSFClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
anchor.setCol1(3);
anchor.setRow1(3);
anchor.setCol2(6);
anchor.setRow1(12);
XSSFDrawing drawingPatriarch = sheet.createDrawingPatriarch();
drawingPatriarch.createPicture(anchor, pictureIndex);
workbook.write(new FileOutputStream("output.xlsx"));
}
Maven 依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>