我正在尝试使用apache poi创建包含图像的Excel文件。发生的事情是我没有填写其他单元格中的数据。
ApachePOIImageDemo.java
public class ApachePOIImageDemo {
public static void main(String[] args) {
System.out.println("------------ Start Main()-------------!");
try {
List<Car> carList = new ArrayList<Car>();
Car car1 = new Car("white","GJ01OP1324","/home/vivek/1.jpg","White Maruthi 800");
Car car2 = new Car("Black","GJ01OP1244","/home/vivek/1.jpg","Black Mercedes");
Car car3 = new Car("Red","GJ01OP244","/home/vivek/1.jpg","Red Toyota Innova");
Car car4 = new Car("Blue","MH01OP1224","/home/vivek/1.jpg","Blue Renault Datson");
carList.add(car1);
carList.add(car2);
carList.add(car3);
carList.add(car4);
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("My Sample Excel");
CreationHelper creationHelper = workbook.getCreationHelper();
int index = 0;
for (Car carInstance : carList){
int columnNumber = 0;
Row row = sheet.createRow(index);
Cell cell0 = row.createCell(columnNumber++);
cell0.setCellValue(index);
Cell cell1 = row.createCell(columnNumber++);
cell1.setCellValue(carInstance.getCarColor());
Cell cell2 = row.createCell(columnNumber++);
cell2.setCellValue(carInstance.getCarPlateNumber());
InputStream inputStream = new FileInputStream(carInstance.getCarImage());
byte[] bytes = IOUtils.toByteArray(inputStream);
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
inputStream.close();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = creationHelper.createClientAnchor();
anchor.setCol1(3);
anchor.setRow1(index);
anchor.setCol2(4);
anchor.setRow2(index+1);
Picture picture = drawing.createPicture(anchor, pictureIdx);
columnNumber++;
Cell cell4 = row.createCell(columnNumber++);
cell4.setCellValue(carInstance.getDescription());
index++;
}
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("myFile.xlsx");
workbook.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("------------ End Main() -------------!");
}
}
我的结果: Click here
我想设置列的自动高度和宽度。
实际上,我的图像大小不同。因此,我需要在文件中设置自动高度-宽度。
谢谢。
答案 0 :(得分:0)
您可以像这样自动调整列大小:
sheet.autoSizeColumn(numberOfYourCOlumnWithImage);