Java导出excel如何使用poi实现在同一个单元格既可以插入文字也可以插入多个图片

图片说明

注意:是同一个单元格中插入多个图片和文字,而不是不同单元格。

  fout.close(); 
} catch (IOException e) { 
  e.printStackTrace(); 
} 

System.out.println("Excel文件生成成功..."); 

}

public static List getErrorCondition(){
List list = new ArrayList();

ErrorCondition r1 = new ErrorCondition("张三", "4306821989021611", "L", "长度错误"); 
ErrorCondition r2 = new ErrorCondition("李四", "430682198902191112","X", "校验错误"); 
ErrorCondition r3 = new ErrorCondition("王五", "", "N", "身份证信息为空"); 

list.add(r1); 
list.add(r2); 
list.add(r3); 

return list; 

}
}
通过上面六个步骤就可以在指定的位置生成Excel文件了。
201622584633771.jpg (819×424)
java POI实现向Excel中插入图片
做Web开发免不了要与Excel打交道。今天老大给我一个任务-导出Excel。开始想的还是蛮简单的,无非就是查找,构建Excel,response下载即可。但是有一点不同,就是要加入图片,就是这个加入图片搞了好久。同时网络上确实没有发现比较好的资料,所以写这篇博文记录之,供自己和博友们查询,参考。
在POI中有HSSFPatriarch对象,该对象为画图的顶级管理器,它的createPicture(anchor, pictureIndex)方法就能够在Excel插入一张图片。所以要在Excel中插入图片,三步就可以搞定。一、获取HSSFPatriarch对象,二、new HSSFClientAnchor对象,三、调用createPicture方法即可。实现倒是非常容易实现,如果想把它做好还是有点儿难度的。这里我们先插入一张图片:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class ExcelImageTest {
public static void main(String[] args) {
FileOutputStream fileOut = null;

BufferedImage bufferImg = null;

//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray

try {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

bufferImg = ImageIO.read(new File("F:/图片/照片/无名氏/小昭11.jpg"));

ImageIO.write(bufferImg, "jpg", byteArrayOut);

  HSSFWorkbook wb = new HSSFWorkbook();   
  HSSFSheet sheet1 = wb.createSheet("test picture");  
  //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点) 
  HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();   
  //anchor主要用于设置图片的属性 
  HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8);   
  anchor.setAnchorType(3);   
  //插入图片  
  patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));  
  fileOut = new FileOutputStream("D:/测试Excel.xls");   
  // 写入excel文件   
   wb.write(fileOut);   
   System.out.println("----Excle文件已生成------"); 
} catch (Exception e) { 
  e.printStackTrace(); 
}finally{ 
  if(fileOut != null){ 
     try { 
      fileOut.close(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
  } 
} 

}
}
如下为执行后的结果:
201622584708310.jpg (482×235)
至于为什么会是这样的结果,主要是因为HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8)这个构造函数造成的,下面我就来解释这个构造函数:HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2, int row2);各个参数的含义如下:
dx1:the x coordinate within the first cell。
dy1:the y coordinate within the first cell。
dx2:the x coordinate within the second cell。
dy2:the y coordinate within the second cell。
col1:the column (0 based) of the first cell。
row1:the row (0 based) of the first cell。
col2:the column (0 based) of the second cell。
row2:the row (0 based) of the second cell。
这里dx1、dy1定义了该图片在开始cell的起始位置,dx2、dy2定义了在终cell的结束位置。col1、row1定义了开始cell、col2、row2定义了结束cell。
201622584737568.jpg (680×280)
下面是有两个不同的构造函数所创建的,从这幅图中我们可以清晰看到上面八个参数的含义和不同之处。
201622584801181.jpg (594×523)
上面是插入一张图片,那么实现插入多张图片呢?其实很简单,构造多个不同的HSSFClientAnchor对象,控制好那八个参数,如下:
?
1
2
3
4
5
6
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 1, (short)5, 8);
HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 9, (short)5, 16);

  //插入图片 
  patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); 
  patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
  其余代码一样,得到如下结果:

201622584816499.jpg (505×339)

在POI中有HSSFPatriarch对象,该对象为画图的顶级管理器,它的createPicture(anchor, pictureIndex)方法就能够在Excel插入一张图片。所以要在Excel中插入图片,三步就可以搞定。一、获取HSSFPatriarch对象,二、new HSSFClientAnchor对象,三、调用createPicture方法即可。实现倒是非常容易实现,如果想把它做好还是有点儿难度的。这里我们先插入一张图片:
01.public class ExcelImageTest {

02. public static void main(String[] args) {

03. FileOutputStream fileOut = null;

04. BufferedImage bufferImg = null;

05. //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray

06. try {

07. ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

08. bufferImg = ImageIO.read(new File("F:/图片/照片/无名氏/小昭11.jpg"));

09. ImageIO.write(bufferImg, "jpg", byteArrayOut);

10.

11. HSSFWorkbook wb = new HSSFWorkbook();

12. HSSFSheet sheet1 = wb.createSheet("test picture");

13. //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)

14. HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();

15. //anchor主要用于设置图片的属性

16. HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8);

17. anchor.setAnchorType(3);

18. //插入图片

19. patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

20. fileOut = new FileOutputStream("D:/测试Excel.xls");

21. // 写入excel文件

22. wb.write(fileOut);

23. System.out.println("----Excle文件已生成------");

24. } catch (Exception e) {

25. e.printStackTrace();

26. }finally{

27. if(fileOut != null){

28. try {

29. fileOut.close();

30. } catch (IOException e) {

31. e.printStackTrace();

32. }

33. }

34. }

35. }

36.}

主要是因为HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8)这个构造函数造成的,下面我就来解释这个构造函数:HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2, int row2);各个参数的含义如下:

  dx1:the x coordinate within the first cell。

  dy1:the y coordinate within the first cell。

  dx2:the x coordinate within the second cell。

  dy2:the y coordinate within the second cell。

  col1:the column (0 based) of the first cell。

  row1:the row (0 based) of the first cell。

  col2:the column (0 based) of the second cell。

  row2:the row (0 based) of the second cell。

  这里dx1、dy1定义了该图片在开始cell的起始位置,dx2、dy2定义了在终cell的结束位置。col1、row1定义了开始cell、col2、row2定义了结束cell。

HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 1, (short)5, 8);
HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 9, (short)5, 16);

        //插入图片  
        patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); 
        patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

生成简单的Excel文件
在现实的办公中,我们常常会有这样一个要求:要求把报表直接用excel打开。在实习中有这样一个需求。根据所选择的资源查询用户所提供附件的全部信息并生成excel供下载。但是在查询的时候我们需要来检测用户所提供的附件里面的信息是否有错误(身份证)。有错误的生成错误信息excel。
Apache的POI项目,是目前比较成熟的HSSF接口,用来处理Excel对象。其实POI不仅仅只能处理excel,它还可以处理word、PowerPoint、Visio、甚至Outlook。
这里我先介绍利用POI如何生成excel。
首先在生成Excel前,我们需要理解一下Excel文件的组织形式。在POI中,是这样理解的:一个Excel文件对应一个workbook,一个workerbook是有若干个sheet组成的。一个sheet有多个row,一个row一般存在多个cell。
对于上面的四个名词我们可以在下图理解

对于生成Excel,POI提供了如下几个基本对象:

  1. HSSFWorkbook:excel 的文档对象
    
    1. HSSFSheet:excel 的表单
    2. HSSFRow :excel 的行
    3. HSSFCell:excel 的格子单元
      

      从上面的图片和Excel的组织结构,我们就可以明白创建Excel的步骤。

      1、生成文档对象HSSHWorkbook。
      2、通过HSSFWorkbook生成表单HSSFSheet。
      3、通过HSSFSheet生成行HSSFRow
      4、通过HSSFRow生成单元格HSSFCell。
      下面是展示代码:
      身份证错误Bean(ErrorCondition.java)

?

12345678910111213141516171819202122232425262728293031323334353637383940414243444546

public class ErrorCondition { private String name; // 姓名 private String idCard; // 身份证 private String status; // 错误状态 private String message; // 错误信息 ErrorCondition(String name,String idCard,String status,String message){ this.name = name; this.idCard = idCard; this.status = status; this.message = message; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }

 处理类(ExportErrorExcel.java)

?

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970

public class ExportErrorExcel { public static void main(String[] args) { //第一步创建workbook HSSFWorkbook wb = new HSSFWorkbook(); //第二步创建sheet HSSFSheet sheet = wb.createSheet("身份证错误信息"); //第三步创建行row:添加表头0行 HSSFRow row = sheet.createRow(0); HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //居中 //第四步创建单元格 HSSFCell cell = row.createCell(0); //第一个单元格 cell.setCellValue("姓名"); //设定值 cell.setCellStyle(style); //内容居中 cell = row.createCell(1); //第二个单元格 cell.setCellValue("身份证"); cell.setCellStyle(style); cell = row.createCell(2); //第三个单元格 cell.setCellValue("错误状态"); cell.setCellStyle(style); cell = row.createCell(3); //第四个单元格 cell.setCellValue("错误信息"); cell.setCellStyle(style); //第五步插入数据 List list = ExportErrorExcel.getErrorCondition(); for (int i = 0; i < list.size(); i++) { ErrorCondition errorCondition = list.get(i); //创建行 row = sheet.createRow(i+1); //创建单元格并且添加数据 row.createCell(0).setCellValue(errorCondition.getName()); row.createCell(1).setCellValue(errorCondition.getIdCard()); row.createCell(2).setCellValue(errorCondition.getStatus()); row.createCell(3).setCellValue(errorCondition.getMessage()); } //第六步将生成excel文件保存到指定路径下 try { FileOutputStream fout = new FileOutputStream("D:\errorCondition.xls"); wb.write(fout); fout.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Excel文件生成成功..."); } public static List getErrorCondition(){ List list = new ArrayList(); ErrorCondition r1 = new ErrorCondition("张三", "4306821989021611", "L", "长度错误"); ErrorCondition r2 = new ErrorCondition("李四", "430682198902191112","X", "校验错误"); ErrorCondition r3 = new ErrorCondition("王五", "", "N", "身份证信息为空"); list.add(r1); list.add(r2); list.add(r3); return list; } }

 通过上面六个步骤就可以在指定的位置生成Excel文件了。

java POI实现向Excel中插入图片
做Web开发免不了要与Excel打交道。今天老大给我一个任务-导出Excel。开始想的还是蛮简单的,无非就是查找,构建Excel,response下载即可。但是有一点不同,就是要加入图片,就是这个加入图片搞了好久。同时网络上确实没有发现比较好的资料,所以写这篇博文记录之,供自己和博友们查询,参考。
在POI中有HSSFPatriarch对象,该对象为画图的顶级管理器,它的createPicture(anchor, pictureIndex)方法就能够在Excel插入一张图片。所以要在Excel中插入图片,三步就可以搞定。一、获取HSSFPatriarch对象,二、new HSSFClientAnchor对象,三、调用createPicture方法即可。实现倒是非常容易实现,如果想把它做好还是有点儿难度的。这里我们先插入一张图片:

?

123456789101112131415161718192021222324252627282930313233343536

public class ExcelImageTest { public static void main(String[] args) { FileOutputStream fileOut = null; BufferedImage bufferImg = null; //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray try { ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); bufferImg = ImageIO.read(new File("F:/图片/照片/无名氏/小昭11.jpg")); ImageIO.write(bufferImg, "jpg", byteArrayOut); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet1 = wb.createSheet("test picture"); //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点) HSSFPatriarch patriarch = sheet1.createDrawingPatriarch(); //anchor主要用于设置图片的属性 HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8); anchor.setAnchorType(3); //插入图片 patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); fileOut = new FileOutputStream("D:/测试Excel.xls"); // 写入excel文件 wb.write(fileOut); System.out.println("----Excle文件已生成------"); } catch (Exception e) { e.printStackTrace(); }finally{ if(fileOut != null){ try { fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

  如下为执行后的结果:

至于为什么会是这样的结果,主要是因为HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8)这个构造函数造成的,下面我就来解释这个构造函数:HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2, int row2);各个参数的含义如下:

  1.  dx1:the x coordinate within the first cell。
    
    1. dy1:the y coordinate within the first cell。
    2. dx2:the x coordinate within the second cell。
    3. dy2:the y coordinate within the second cell。
    4. col1:the column (0 based) of the first cell。
    5. row1:the row (0 based) of the first cell。
    6. col2:the column (0 based) of the second cell。
    7.  row2:the row (0 based) of the second cell。
      

      这里dx1、dy1定义了该图片在开始cell的起始位置,dx2、dy2定义了在终cell的结束位置。col1、row1定义了开始cell、col2、row2定义了结束cell。

    下面是有两个不同的构造函数所创建的,从这幅图中我们可以清晰看到上面八个参数的含义和不同之处。

上面是插入一张图片,那么实现插入多张图片呢?其实很简单,构造多个不同的HSSFClientAnchor对象,控制好那八个参数,如下:

?

123456

HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 1, (short)5, 8); HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 9, (short)5, 16); //插入图片 patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

  其余代码一样,得到如下结果:

public class ExportErrorExcel { public static void main(String[] args) { //第一步创建workbook HSSFWorkbook wb = new HSSFWorkbook(); //第二步创建sheet HSSFSheet sheet = wb.createSheet("身份证错误信息"); //第三步创建行row:添加表头0行 HSSFRow row = sheet.createRow(0); HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //居中 //第四步创建单元格 HSSFCell cell = row.createCell(0); //第一个单元格 cell.setCellValue("姓名"); //设定值 cell.setCellStyle(style); //内容居中 cell = row.createCell(1); //第二个单元格 cell.setCellValue("身份证"); cell.setCellStyle(style); cell = row.createCell(2); //第三个单元格 cell.setCellValue("错误状态"); cell.setCellStyle(style); cell = row.createCell(3); //第四个单元格 cell.setCellValue("错误信息"); cell.setCellStyle(style); //第五步插入数据 List list = ExportErrorExcel.getErrorCondition(); for (int i = 0; i < list.size(); i++) { ErrorCondition errorCondition = list.get(i); //创建行 row = sheet.createRow(i+1); //创建单元格并且添加数据 row.createCell(0).setCellValue(errorCondition.getName()); row.createCell(1).setCellValue(errorCondition.getIdCard()); row.createCell(2).setCellValue(errorCondition.getStatus()); row.createCell(3).setCellValue(errorCondition.getMessage()); } //第六步将生成excel文件保存到指定路径下 try { FileOutputStream fout = new FileOutputStream("D:\errorCondition.xls"); wb.write(fout); fout.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Excel文件生成成功..."); } public static List getErrorCondition(){ List list = new ArrayList(); ErrorCondition r1 = new ErrorCondition("张三", "4306821989021611", "L", "长度错误"); ErrorCondition r2 = new ErrorCondition("李四", "430682198902191112","X", "校验错误"); ErrorCondition r3 = new ErrorCondition("王五", "", "N", "身份证信息为空"); list.add(r1); list.add(r2); list.add(r3); return list; } }

用 pageoffice 给excel中的单元格插入文字或者图片吧