java poi excel导出,excel图片导出,每列前面加入图片导出 。

 

上图看效果 导出每列加上不同图片

java poi excel导出,excel图片导出,每列前面加入图片导出 。_第1张图片

 

1.导包

	
			org.apache.poi
			poi
			3.16
		
		
			org.apache.poi
			poi-ooxml
			3.16
		
		
			org.apache.poi
			poi-ooxml-schemas
			3.16
		

 

2。工具类 ExportExcelUtils   

ExportExcelUtils 网上原本的普通导出    后来我改进 加入图片列  这是普通的导出 无图片

/**
 * poi导出
 * @author Administrator
 *
 */
public class ExportExcelUtils {
	private String title; // 导出表格的表名  
    
    private String[] rowName;// 导出表格的列名  
      
    private List  dataList = new ArrayList(); // 对象数组的List集合  
      
    private HttpServletResponse  response;  
    
    // 传入要导入的数据  
    public ExportExcelUtils(String title,String[] rowName,List  dataList,HttpServletResponse  response){  
        this.title=title;  
        this.rowName=rowName;  
        this.dataList=dataList;  
        this.response = response;  
    }  
	// 导出数据  
    public void exportData(){  
        try {  
            HSSFWorkbook workbook =new HSSFWorkbook(); // 创建一个excel对象  
            HSSFSheet sheet =workbook.createSheet(title); // 创建表格  
            // 产生表格标题行  
            HSSFRow rowm  =sheet.createRow(0);  // 行  
            HSSFCell cellTiltle =rowm.createCell(0);  // 单元格  
              
            // sheet样式定义  
            HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook); // 头样式  
            HSSFCellStyle style = this.getStyle(workbook);  // 单元格样式  
            /**  
             * 参数说明  
             * 从0开始   第一行 第一列 都是从角标0开始  
             * 行 列 行列    (0,0,0,5)  合并第一行 第一列  到第一行 第六列  
             * 起始行,起始列,结束行,结束列  
             *   
             * new Region()  这个方法使过时的  
             */  
            // 合并第一行的所有列  
            sheet.addMergedRegion(new CellRangeAddress(0, (short) 0, 0, (short) (rowName.length-1)));  
            cellTiltle.setCellStyle(columnTopStyle);  
            cellTiltle.setCellValue(title);   
              
            int columnNum = rowName.length;  // 表格列的长度  
            HSSFRow rowRowName = sheet.createRow(1);  // 在第二行创建行  
            HSSFCellStyle cells =workbook.createCellStyle();  
            cells.setBottomBorderColor(HSSFColor.BLACK.index);    
            rowRowName.setRowStyle(cells);  
              
            // 循环 将列名放进去  
            for (int i = 0; i < columnNum; i++) {  
                HSSFCell  cellRowName = rowRowName.createCell((int)i);  
                cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 单元格类型  
                  
                HSSFRichTextString text = new HSSFRichTextString(rowName[i]);  // 得到列的值  
                cellRowName.setCellValue(text); // 设置列的值  
                cellRowName.setCellStyle(columnTopStyle); // 样式  
            }  
              
            // 将查询到的数据设置到对应的单元格中  
            for (int i = 0; i < dataList.size(); i++) {  
                Object[] obj = dataList.get(i);//遍历每个对象  
                HSSFRow row = sheet.createRow(i+2);//创建所需的行数  
                for (int j = 0; j < obj.length; j++) {  
                     HSSFCell  cell = null;   //设置单元格的数据类型   
                     if(j==0){  
                         // 第一列设置为序号  
                         cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);  
                         cell.setCellValue(i+1);  
                     }else{  
                         cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);  
                         if(!"".equals(obj[j]) && obj[j] != null){    
                                cell.setCellValue(obj[j].toString());                       //设置单元格的值    
                            }else{  
                                cell.setCellValue("  ");  
                            }    
                     }  
                     cell.setCellStyle(style); // 样式  
                }  
            }  
            //  让列宽随着导出的列长自动适应  
             sheet.autoSizeColumn((short)0); //调整第一列宽度  
             sheet.autoSizeColumn((short)1); //调整第二列宽度  
             sheet.autoSizeColumn((short)2); //调整第三列宽度  
             sheet.autoSizeColumn((short)3); //调整第四列宽度  
             sheet.autoSizeColumn((short)4); //调整第五列宽度  
             sheet.autoSizeColumn((short)5); //调整第六列宽度  
               
             if(workbook !=null){    
                    try    
                    {    
                        // excel 表文件名  
                        String fileName = title + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";    
                        String fileName11 = URLEncoder.encode(fileName,"UTF-8");  
                        String fileName12 = java.net.URLDecoder.decode(fileName11, "UTF-8");
                        String headStr = "attachment; filename=\"" + fileName12 + "\"";    
                        System.out.println(headStr);
                        response.setContentType("APPLICATION/OCTET-STREAM");    
                        // response.setHeader("Content-Disposition", headStr);    
                        response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName12.getBytes("gb2312"), "ISO8859-1" ));
                        OutputStream out = response.getOutputStream();    
                        workbook.write(out);  
                        out.flush();  
                        out.close();  
                    }    
                    catch (IOException e)    
                    {    
                        e.printStackTrace();    
                    }   
                      
                }    
        
            }catch(Exception e){    
                e.printStackTrace();    
            }    
                
        }    
              
    public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {    
          
        // 设置字体    
        HSSFFont font = workbook.createFont();    
        //设置字体大小    
        font.setFontHeightInPoints((short)11);    
        //字体加粗    
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);    
        //设置字体名字     
        font.setFontName("Courier New");    
        //设置样式;     
        HSSFCellStyle style = workbook.createCellStyle();    
        //设置底边框;     
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);    
        //设置底边框颜色;      
        style.setBottomBorderColor(HSSFColor.BLACK.index);    
        //设置左边框;       
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);    
        //设置左边框颜色;     
        style.setLeftBorderColor(HSSFColor.BLACK.index);    
        //设置右边框;     
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);    
        //设置右边框颜色;     
        style.setRightBorderColor(HSSFColor.BLACK.index);    
        //设置顶边框;     
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);    
        //设置顶边框颜色;      
        style.setTopBorderColor(HSSFColor.BLACK.index);    
        //在样式用应用设置的字体;      
        style.setFont(font);    
        //设置自动换行;     
        style.setWrapText(false);    
        //设置水平对齐的样式为居中对齐;      
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);    
        //设置垂直对齐的样式为居中对齐;     
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);    
            
        return style;    
            
  }    
      
    public HSSFCellStyle getStyle(HSSFWorkbook workbook) {    
        // 设置字体    
        HSSFFont font = workbook.createFont();    
        //设置字体大小    
        //font.setFontHeightInPoints((short)10);    
        //字体加粗    
        //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);    
        //设置字体名字     
        font.setFontName("Courier New");    
        //设置样式;     
        HSSFCellStyle style = workbook.createCellStyle();    
        //设置底边框;     
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);    
        //设置底边框颜色;      
        style.setBottomBorderColor(HSSFColor.BLACK.index);    
        //设置左边框;       
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);    
        //设置左边框颜色;     
        style.setLeftBorderColor(HSSFColor.BLACK.index);    
        //设置右边框;     
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);    
        //设置右边框颜色;     
        style.setRightBorderColor(HSSFColor.BLACK.index);    
        //设置顶边框;     
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);    
        //设置顶边框颜色;      
        style.setTopBorderColor(HSSFColor.BLACK.index);    
        //在样式用应用设置的字体;      
        style.setFont(font);    
        //设置自动换行;     
        style.setWrapText(false);    
        //设置水平对齐的样式为居中对齐;      
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);    
        //设置垂直对齐的样式为居中对齐;     
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);    
           
        return style;    
  }    
}    
    

 

这是普通导出方法  无图片   

@RequestMapping("/exportExcel")
	  public ModelAndView exportExcel(HttpServletRequest request,HttpServletResponse response) {
		//PageDto pagedto = new PageDto(request.getParameter("pageNumber"),request.getRequestURL());
		//vipuserservice.loadPageDto(pagedto);
		List findUser = vipuserservice.findUser();
		
		try { 
			 String title ="会员信息表";  
	         String[] rowsName=new String[]{"序号","ID","姓名","关系","身份证","充值金额","电话","扣费"};  
	         List  dataList = new ArrayList();
	         
	         Object[] objs = null;  
	         for (int i = 0; i < findUser.size(); i++) {  
	        	 ActivityUser po = findUser.get(i); 
	        	 
	             objs = new Object[rowsName.length];  
	             objs[0] = i;  
	             objs[1] = po.getId(); 
	             objs[2] = po.getName();
	             objs[3] = po.getRelation();  
	             objs[4] = po.getUserno();
	             
//	             if(po.getSex()==1){
//	            	 objs[5] = '男';
//	             }else{
//	            	 objs[5] = '女';
//	             } 
	             objs[5] = po.getMoney();  
	             objs[6] = po.getPhone();
	             objs[7] = po.getCharging();
	             dataList.add(objs);  
	         }  
	         ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response);
	         ex.exportData();  
			
			 } catch (Exception e) {  
		            e.printStackTrace();  
		        }
		return null;  
			
	  }

 

 

3.有需求导出需求前面列加入图片  改进后的代码

/**
 * poi导出
 * @author yangjaibin
 *
 */
public class ExportExcelUtils
{
    private String title; // 导出表格的表名

    private String[] rowName;// 导出表格的列名

    private List dataList = new ArrayList(); // 对象数组的List集合

    private HttpServletResponse response;

    // 传入要导入的数据
    public ExportExcelUtils(String title, String[] rowName, List dataList, HttpServletResponse response)
    {
        this.title = title;
        this.rowName = rowName;
        this.dataList = dataList;
        this.response = response;
    }

    // 导出数据
    public void exportData()
    {
        try
        {
            HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个excel对象
            HSSFSheet sheet = workbook.createSheet(title); // 创建表格
            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
            // 产生表格标题行
            HSSFRow rowm = sheet.createRow(0);  // 行
            HSSFCell cellTiltle = rowm.createCell(0);  // 单元格
            // sheet样式定义
            HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook); // 头样式
            HSSFCellStyle style = this.getStyle(workbook);  // 单元格样式
            /**
             * 参数说明
             * 从0开始   第一行 第一列 都是从角标0开始
             * 行 列 行列    (0,0,0,5)  合并第一行 第一列  到第一行 第六列
             * 起始行,起始列,结束行,结束列
             *
             * new Region()  这个方法使过时的
             */
            // 合并第一行的所有列
            sheet.addMergedRegion(new CellRangeAddress(0, (short) 0, 0, (short) (rowName.length - 1)));
            cellTiltle.setCellStyle(columnTopStyle);
            cellTiltle.setCellValue(title);

            int columnNum = rowName.length;  // 表格列的长度
            HSSFRow rowRowName = sheet.createRow(1);  // 在第二行创建行
            HSSFCellStyle cells = workbook.createCellStyle();
            cells.setBottomBorderColor(HSSFColor.BLACK.index);
            rowRowName.setRowStyle(cells);

            // 循环 将列名放进去
            for (int i = 0; i < columnNum; i++)
            {
                HSSFCell cellRowName = rowRowName.createCell((int) i);
                cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 单元格类型

                HSSFRichTextString text = new HSSFRichTextString(rowName[i]);  // 得到列的值
                cellRowName.setCellValue(text); // 设置列的值
                cellRowName.setCellStyle(columnTopStyle); // 样式
            }

            // 将查询到的数据设置到对应的单元格中
            for (int i = 0; i < dataList.size(); i++)
            {
                Object[] obj = dataList.get(i);//遍历每个对象
                HSSFRow row = sheet.createRow(i + 2);//创建所需的行数
                row.setHeightInPoints(40);

                for (int j = 0; j < obj.length; j++)
                {
                    HSSFCell cell = null;   //设置单元格的数据类型
                    if (j == 0)
                    {
                        // 第一列设置为序号
                        cell = row.createCell(j, HSSFCell.CELL_TYPE_NUMERIC);
                        cell.setCellValue(i + 1);
                    }
                    else
                    {
                        cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING);

                        if (!"".equals(obj[j]) && obj[j] != null)
                        {
                            if (obj[j].toString().length() > 30)
                            {
                                cell.setCellValue("  ");
                                drawPictureInfoExcel(workbook, patriarch, obj[j].toString(), i + 2);//i+2代表当前的行
                            }
                            else
                            {
                                cell.setCellValue(obj[j].toString());
                            }
                            //设置单元格的值
                        }
                        else
                        {
                            cell.setCellValue("  ");
                        }

                    }
                    cell.setCellStyle(style); // 样式
                }
            }
            //  让列宽随着导出的列长自动适应

            sheet.setColumnWidth(0, 8 * 256);  //调整第一列宽度
            sheet.setColumnWidth(1, 8 * 256);
            sheet.setColumnWidth(2, 20 * 256);  ///调整第二列宽度
            sheet.setColumnWidth(3, 20 * 256);
            sheet.setColumnWidth(4, 20 * 256);
            sheet.setColumnWidth(5, 21 * 256);
            sheet.setColumnWidth(6, 20 * 256);

            if (workbook != null)
            {
                try
                {
                    // excel 表文件名
                    String fileName = title + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
                    String fileName11 = URLEncoder.encode(fileName, "UTF-8");
                    String fileName12 = java.net.URLDecoder.decode(fileName11, "UTF-8");
                    String headStr = "attachment; filename=\"" + fileName12 + "\"";
                    response.setContentType("APPLICATION/OCTET-STREAM");
                    // response.setHeader("Content-Disposition", headStr);
                    response.setHeader("Content-Disposition",
                            "attachment;filename=" + new String(fileName12.getBytes("gb2312"), "ISO8859-1"));
                    OutputStream out = response.getOutputStream();
                    workbook.write(out);
                    out.flush();
                    out.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    private void drawPictureInfoExcel(HSSFWorkbook wb, HSSFPatriarch patriarch, String pictureUrl, int rowIndex)
    {
        //rowIndex代表当前行
        try
        {
            if (pictureUrl != null)
            {
                URL url = new URL(pictureUrl);//获取人员照片的地址
                //打开链接
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                //设置请求方式为"GET"
                conn.setRequestMethod("GET");
                //超时响应时间为5秒
                conn.setConnectTimeout(5 * 1000);
                //通过输入流获取图片数据
                InputStream inStream = conn.getInputStream();
                //得到图片的二进制数据,以二进制封装得到数据,具有通用性
                byte[] data = readInputStream(inStream);
                //anchor主要用于设置图片的属性
                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 1, rowIndex, (short) 1,
                        rowIndex);
                //Sets the anchor type (图片在单元格的位置)
                //0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
                anchor.setAnchorType(0);
                patriarch.createPicture(anchor, wb.addPicture(data, HSSFWorkbook.PICTURE_TYPE_JPEG));
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private static byte[] readInputStream(InputStream inStream) throws Exception
    {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度,如果为-1,代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while ((len = inStream.read(buffer)) != -1)
        {
            //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }

    public HSSFCellStyle getStyle(HSSFWorkbook workbook)
    {
        // 设置字体
        HSSFFont font = workbook.createFont();
        //设置字体大小
        font.setFontHeightInPoints((short) 9);
        //字体加粗
        //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //设置字体名字
        font.setFontName("Courier New");
        //设置样式;
        HSSFCellStyle style = workbook.createCellStyle();
        //设置底边框;
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //设置底边框颜色;
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        //设置左边框;
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        //设置左边框颜色;
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        //设置右边框;
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //设置右边框颜色;
        style.setRightBorderColor(HSSFColor.BLACK.index);
        //设置顶边框;
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //设置顶边框颜色;
        style.setTopBorderColor(HSSFColor.BLACK.index);
        //在样式用应用设置的字体;
        style.setFont(font);
        //设置自动换行;
        style.setWrapText(false);
        //设置水平对齐的样式为居中对齐;
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        return style;
    }

    public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook)
    {

        // 设置字体
        HSSFFont font = workbook.createFont();
        //设置字体大小
        font.setFontHeightInPoints((short) 10);
        //字体加粗
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //设置字体名字
        font.setFontName("Courier New");
        //设置样式;
        HSSFCellStyle style = workbook.createCellStyle();
        //设置底边框;
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //设置底边框颜色;
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        //设置左边框;
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        //设置左边框颜色;
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        //设置右边框;
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //设置右边框颜色;
        style.setRightBorderColor(HSSFColor.BLACK.index);
        //设置顶边框;
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //设置顶边框颜色;
        style.setTopBorderColor(HSSFColor.BLACK.index);
        //在样式用应用设置的字体;
        style.setFont(font);
        //设置自动换行;
        style.setWrapText(false);
        //设置水平对齐的样式为居中对齐;
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        return style;

    }

}

 

方法调用

   @RequestMapping(value = "/exportHandConf", method = { RequestMethod.POST, RequestMethod.GET })
    public @ResponseBody JSONObject exportHandConf(Long metId, HttpServletRequest request,
            HttpServletResponse response)
    {
        JSONObject result = new JSONObject();

      //查询需要导出的数据
        List> maps = 
                             InfoService.getId(metId);
        if (maps.size() != 0)
        {
            try
            {
                String title = "人员";
                String[] rowsName = new String[] { "序号", "头像", "昵称", "姓名", "号码", "企业"};
                List dataList = new ArrayList();
                Object[] objs = null;
                for (int i = 0; i < maps.size(); i++)
                {
                    objs = new Object[rowsName.length];
                    objs[0] = i;
                    objs[1] = maps.get(i).get("headUrl");   //头像url
                    objs[2] = maps.get(i).get("nickname");
                    objs[3] = maps.get(i).get("realName");
                    objs[4] = maps.get(i).get("signPhone");
                    objs[5] = maps.get(i).get("signunit");
                 
                    dataList.add(objs);
                }
                ExportExcelUtils ex = new ExportExcelUtils(title, rowsName, dataList, response);
                ex.exportData();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                
            }

        }
        result.put("flag", "true");
        return result;

    }

 

 

简单说明一下

在原先工具类基础上  加入  drawPictureInfoExcel   readInputStream 图片写入  方法 

 

在写入表格时 因为我图片url 长度 很长,所以 我判断url图片的 长度 然后调用写入方法 写入图片

java poi excel导出,excel图片导出,每列前面加入图片导出 。_第2张图片

 

调整图片的位置 在哪个单元格

java poi excel导出,excel图片导出,每列前面加入图片导出 。_第3张图片

 

每一列的间距

 

 

java poi excel导出,excel图片导出,每列前面加入图片导出 。_第4张图片

 

 

 

 

你可能感兴趣的:(ssm,ssh,java基础,srpingmvc,springboot)