将图片添加到另一张图片时出现问题。将图片添加到第一张或唯一一张的图纸上没有问题。我不知道如何解决它。
它也看起来并不容易,POI不允许您将图像添加到其他工作表
提前感谢您对此问题的任何帮助
示例代码:
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
class ImageTest {
public static void main(String[] args) {
try {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("My Sample Excel");
//FileInputStream obtains input bytes from the image file
InputStream inputStream = new FileInputStream("/home/axel/Bilder/Wasserlilien.jpg");
//Get the contents of an InputStream as a byte[].
byte[] bytes = IOUtils.toByteArray(inputStream);
//Adds a picture to the workbook
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
//close the input stream
inputStream.close();
//Returns an object that handles instantiating concrete classes
CreationHelper helper = wb.getCreationHelper();
//Creates the top-level drawing patriarch.
Drawing drawing = sheet.createDrawingPatriarch();
//Create an anchor that is attached to the worksheet
ClientAnchor anchor = helper.createClientAnchor();
//create an anchor with upper left cell _and_ bottom right cell
anchor.setCol1(1); //Column B
anchor.setRow1(2); //Row 3
anchor.setCol2(2); //Column C
anchor.setRow2(3); //Row 4
//Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
//Reset the image to the original size
//pict.resize(); //don't do that. Let the anchor resize the image!
//Create the Cell B3
Cell cell = sheet.createRow(2).createCell(1);
//set width to n character widths = count characters * 256
//int widthUnits = 20*256;
//sheet.setColumnWidth(1, widthUnits);
//set height to n points in twips = n * 20
//short heightUnits = 60*20;
//cell.getRow().setHeight(heightUnits);
//Write the Excel file
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("myFile.xlsx");
wb.write(fileOut);
fileOut.close();
} catch (IOException ioex) {
}
}
}
答案 0 :(得分:0)
这真的取决于你的目标。如果你想在多张纸上的同一个地方插入一张图片,那么你可以把你的代码放到这样的循环中:
try {
Workbook wb = new XSSFWorkbook();
//FileInputStream obtains input bytes from the image file
InputStream inputStream = new FileInputStream("C:\\path\\to\\image\\image.jpg");
//Get the contents of an InputStream as a byte[].
byte[] bytes = IOUtils.toByteArray(inputStream);
//Adds a picture to the workbook
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
//close the input stream
inputStream.close();
//Returns an object that handles instantiating concrete classes
CreationHelper helper = wb.getCreationHelper();
for(int i = 0; i < 3; i++) {
Sheet sheet = wb.createSheet("Sheet Number " + i);
//Creates the top-level drawing patriarch.
Drawing drawing = sheet.createDrawingPatriarch();
//Create an anchor that is attached to the worksheet
ClientAnchor anchor = helper.createClientAnchor();
//create an anchor with upper left cell _and_ bottom right cell
anchor.setCol1(1); //Column B
anchor.setRow1(2); //Row 3
anchor.setCol2(2); //Column C
anchor.setRow2(3); //Row 4
//Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
//Create the Cell B3
Cell cell = sheet.createRow(2).createCell(1);
}
//Write the Excel file
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("C:\\Temp\\myFile.xlsx");
wb.write(fileOut);
fileOut.close();
} catch (IOException ioex) {
ioex.printStackTrace();
}
此代码创建一个包含三张纸的Excel文件,并将图像放在所有三张纸的同一位置。我刚刚对此进行了测试,效果很好。如果您希望将其放在多张纸上的不同位置,您应该能够从此代码中推断出如何执行此操作。您可以将循环与if / case语句一起使用,也可以只按顺序创建工作表。