我正在尝试将图像插入Excel工作表。 成功插入Excel工作表,但问题出在这里。
当我试图在Excel工作表中插入多个图像时,第一个图像将被删除。从两天内开始,我试图解决这个问题,但是如果有人有任何想法,我什么地方都看不到绿色信号,请帮帮我。
下面是我的代码:
public static void main(String args[]) throws IOException {
openExcel();
pasteInExcel();
openExcel();
pasteInExcel();
openExcel();
pasteInExcel();
}
private static void pasteInExcel() throws IOException {
InputStream my_banner_image = new FileInputStream(imgPath);
byte[] bytes = org.apache.poi.util.IOUtils.toByteArray(my_banner_image);
int my_picture_id = my_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
my_banner_image.close();
XSSFDrawing drawing = my_sheet.createDrawingPatriarch();
XSSFPicture my_picture = drawing.createPicture(getAnchorPoint(), my_picture_id);
my_picture.resize();
fileClose();
}
public static void openExcel() throws IOException {
File f = new File(excelPath);
if (!f.exists()) {
my_workbook = new XSSFWorkbook();
my_sheet = my_workbook.createSheet("MyLogo");
} else {
my_workbook = new XSSFWorkbook(new FileInputStream(excelPath));
my_sheet = my_workbook.getSheet("MyLogo");
}
}
public static XSSFClientAnchor getAnchorPoint() {
System.out.println("Row is "+row);
XSSFClientAnchor my_anchor = new XSSFClientAnchor();
my_anchor.setCol1(2);
my_anchor.setRow1(row);
row = row + 5;
return my_anchor;
}
public static void fileClose() throws IOException {
FileOutputStream fos = new FileOutputStream(excelPath);
my_workbook.write(fos);
fos.close();
}
答案 0 :(得分:1)
我已经运行了您的代码(我添加了一些您未提供的缺少的字段声明)。但是无法重现您的问题。这是输出的excel文件。
在我的情况下,我正在使用具有以下依赖项的maven:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
这是完整代码(基于您提供的代码):
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Main{
private static Workbook my_workbook;
private static Sheet my_sheet;
private static String imgPath;
private static String excelPath;
private static int row=0;
public static void main(String args[]) throws IOException {
init();
openExcel();
pasteInExcel();
openExcel();
pasteInExcel();
openExcel();
pasteInExcel();
}
private static void init() {
excelPath = "C:\\temp\\test.xlsx";
imgPath = "C:\\temp\\test-image.png";
}
private static void pasteInExcel() throws IOException {
InputStream my_banner_image = new FileInputStream(imgPath);
byte[] bytes = org.apache.poi.util.IOUtils.toByteArray(my_banner_image);
int my_picture_id = my_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
my_banner_image.close();
XSSFDrawing drawing = (XSSFDrawing) my_sheet.createDrawingPatriarch();
XSSFPicture my_picture = drawing.createPicture(getAnchorPoint(), my_picture_id);
my_picture.resize();
fileClose();
}
public static void openExcel() throws IOException {
File f = new File(excelPath);
if (!f.exists()) {
my_workbook = new XSSFWorkbook();
my_sheet = my_workbook.createSheet("MyLogo");
} else {
my_workbook = new XSSFWorkbook(new FileInputStream(excelPath));
my_sheet = my_workbook.getSheet("MyLogo");
}
}
public static XSSFClientAnchor getAnchorPoint() {
System.out.println("Row is "+row);
XSSFClientAnchor my_anchor = new XSSFClientAnchor();
my_anchor.setCol1(2);
my_anchor.setRow1(row);
row = row + 5;
return my_anchor;
}
public static void fileClose() throws IOException {
FileOutputStream fos = new FileOutputStream(excelPath);
my_workbook.write(fos);
fos.close();
}
}
建议的解决方案: 尝试将poi和poi-ooxml的版本更改为3.11