java poi导出图片到excel示例代码
程序员文章站
2024-02-13 20:20:52
本文实例为大家分享了java使用poi导出图片到excel的具体代码,供大家参考,具体内容如下
代码实现
controller
/**
* 导出志愿者...
本文实例为大家分享了java使用poi导出图片到excel的具体代码,供大家参考,具体内容如下
代码实现
controller
/** * 导出志愿者/人才数据 * @param talent_type * @return */ @requestmapping("/exportdata") public void exportdata(integer talent_type, httpservletresponse response) { string fileid = uuid.randomuuid().tostring().replace("-", ""); map<string, object> param = new hashmap<>() ; param.put("talent_type", talent_type) ; try { list<map<string, object>> volunteermaplist = volunteerservice.getexportdata(param) ; string rootpath = sysconfigmanager.getinstance().gettext("/config/sys/rootpath"); string filepath = rootpath + "/" + fileid + ".xlsx" ; volunteerservice.exportdata(volunteermaplist, filepath) ; // 下载 fileinputstream inputstream = null; try{ //设置发送到客户端的响应内容类型 response.reset(); response.setcontentlength((int) new file(filepath).length()); response.setcontenttype("application/octet-stream"); response.addheader("content-disposition", "attachment; filename=\"" + urlencoder.encode("文件名.xlsx", "utf-8")+ "\""); //读取本地图片输入流 inputstream = new fileinputstream(filepath); // 循环取出流中的数据 byte[] b = new byte[1024]; int len; while ((len = inputstream.read(b)) > 0) response.getoutputstream().write(b, 0, len); } finally{ if(inputstream != null){ inputstream.close(); } } logger.debug("导出志愿者/人才数据成功!"); } catch (exception e) { e.printstacktrace(); logger.error("导出志愿者/人才数据异常!"); } }
service
public void exportdata(list<map<string, object>> volunteermaplist, string filepath) throws exception { string[] alias = {"头像", "名称", "个人/团体", "志愿者/人才", "性别", "生日", "手机号", "身份证", "省份", "市", "区/县", "详细地址", "邮箱", "政治面貌", "学历", "民族", "职业", "团队人数", "艺术特长", "介绍"}; string[] keys = {"photo", "name", "type", "talent_type", "sex", "birth_day", "mobile", "idcard", "province", "city", "county", "address", "email", "political", "education", "nation", "profession", "member_count", "art_spetiality", "content"}; file file = new file(filepath); if (!file.exists()) file.createnewfile(); fileoutputstream fileoutput = new fileoutputstream(file); xssfworkbook workbook = new xssfworkbook(); int sheetsize = volunteermaplist.size() + 50; double sheetno = math.ceil(volunteermaplist.size() / sheetsize); string photoimgpath = sysconfigmanager.getinstance().gettext("/config/sys/rootpath") ; for (int index = 0; index <= sheetno; index++) { xssfsheet sheet = workbook.createsheet(); workbook.setsheetname(index, "人才、志愿者" + index); xssfrow row = sheet.createrow(0); sheet.setcolumnwidth(0, 2048); xssfcell cell; xssfcellstyle cellstyle = workbook.createcellstyle(); xssffont font = workbook.createfont(); font.setboldweight(xssffont.boldweight_bold); // 居中 cellstyle.setalignment(xssfcellstyle.align_center); // 加粗 cellstyle.setfont(font); //创建标题 for (int i = 0; i < alias.length; i++) { cell = row.createcell(i); cell.setcellvalue(alias[i]); cell.setcellstyle(cellstyle); } int startno = index * sheetsize; int endno = math.min(startno + sheetsize, volunteermaplist.size()); cellstyle = workbook.createcellstyle(); // 居中 cellstyle.setalignment(xssfcellstyle.align_center); cellstyle.setverticalalignment(xssfcellstyle.vertical_center); // 写入各条记录,每条记录对应excel表中的一行 for (int i = startno; i < endno; i++) { int rownum = i + 1 - startno ; row = sheet.createrow(rownum); map<string, object> map = (map<string, object>) volunteermaplist.get(i); for (int j = 0; j < keys.length; j++) { cell = row.createcell(j); string key = keys[j] ; if (key.equals("photo")){ sheet.addmergedregion(new cellrangeaddress(i + 1,i + 1,i + 1,i + 1)) ; // 头像 file photofile = new file(photoimgpath + map.get(key)) ; if (photofile.exists()){ bufferedimage bufferedimage = imageio.read(photofile) ; bytearrayoutputstream bytearrayout = new bytearrayoutputstream(); imageio.write(bufferedimage, "jpg", bytearrayout); byte[] data = bytearrayout.tobytearray(); xssfdrawing drawingpatriarch = sheet.createdrawingpatriarch(); xssfclientanchor anchor = new xssfclientanchor(480, 30, 700, 250, (short)0, i + 1, (short) 1, i + 2); drawingpatriarch.createpicture(anchor, workbook.addpicture(data, xssfworkbook.picture_type_jpeg)); sheet.setcolumnwidth((short)500, (short)500); row.setheight((short)500); } else { cell.setcelltype(xssfcell.cell_type_string); cell.setcellvalue(""); } } else { cell.setcelltype(xssfcell.cell_type_string); object value = map.get(key); cell.setcellvalue(value == null ? "" : value.tostring()); cell.setcellstyle(cellstyle); } } } // 设置列宽 for (int i = 1; i < alias.length; i++) sheet.autosizecolumn(i); // 处理中文不能自动调整列宽的问题 this.setsizecolumn(sheet, alias.length); } fileoutput.flush(); workbook.write(fileoutput); fileoutput.close(); } // 自适应宽度(中文支持) private void setsizecolumn(xssfsheet sheet, int size) { for (int columnnum = 0; columnnum < size; columnnum++) { int columnwidth = sheet.getcolumnwidth(columnnum) / 256; for (int rownum = 0; rownum <= sheet.getlastrownum(); rownum++) { xssfrow currentrow; //当前行未被使用过 if (sheet.getrow(rownum) == null) { currentrow = sheet.createrow(rownum); } else { currentrow = sheet.getrow(rownum); } if (currentrow.getcell(columnnum) != null) { xssfcell currentcell = currentrow.getcell(columnnum); if (currentcell.getcelltype() == xssfcell.cell_type_string) { int length = currentcell.getstringcellvalue().getbytes().length; if (columnwidth < length) columnwidth = length; } } } columnwidth = columnwidth * 256 ; sheet.setcolumnwidth(columnnum, columnwidth >= 65280 ? 6000 : columnwidth); } }
以上所述是小编给大家介绍java poi导出图片到excel示例代码解整合,希望对大家有所帮助
上一篇: web.config使用方法指南
下一篇: JSP运行原理和九大隐式对象说明