当前位置: 移动技术网 > IT编程>开发语言>Java > Windows中使用Java生成Excel文件并插入图片的方法

Windows中使用Java生成Excel文件并插入图片的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

生成简单的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。
     对于上面的四个名词我们可以在下图理解

201622584521421.jpg (820×422)

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

  •      hssfworkbook:excel 的文档对象
  •      hssfsheet:excel 的表单
  •      hssfrow :excel 的行
  •      hssfcell:excel 的格子单元

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

        1、生成文档对象hsshworkbook。
        2、通过hssfworkbook生成表单hssfsheet。
        3、通过hssfsheet生成行hssfrow
        4、通过hssfrow生成单元格hssfcell。
     下面是展示代码:
     身份证错误bean(errorcondition.java)

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)

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<errorcondition> 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<errorcondition> geterrorcondition(){ 
    list<errorcondition> list = new arraylist<errorcondition>(); 
     
    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方法即可。实现倒是非常容易实现,如果想把它做好还是有点儿难度的。这里我们先插入一张图片:

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对象,控制好那八个参数,如下:

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)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网