本文實(shí)例為大家分享了java使用poi導(dǎo)出圖片到excel的具體代碼,供大家參考,具體內(nèi)容如下
代碼實(shí)現(xiàn)
controller
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
37
38
39
40
41
|
/** * 導(dǎo)出志愿者/人才數(shù)據(jù) * @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 { //設(shè)置發(fā)送到客戶端的響應(yīng)內(nèi)容類型 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); // 循環(huán)取出流中的數(shù)據(jù) 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( "導(dǎo)出志愿者/人才數(shù)據(jù)成功!" ); } catch (exception e) { e.printstacktrace(); logger.error( "導(dǎo)出志愿者/人才數(shù)據(jù)異常!" ); } } |
service
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
public void exportdata(list<map<string, object>> volunteermaplist, string filepath) throws exception { string[] alias = { "頭像" , "名稱" , "個(gè)人/團(tuán)體" , "志愿者/人才" , "性別" , "生日" , "手機(jī)號(hào)" , "身份證" , "省份" , "市" , "區(qū)/縣" , "詳細(xì)地址" , "郵箱" , "政治面貌" , "學(xué)歷" , "民族" , "職業(yè)" , "團(tuán)隊(duì)人數(shù)" , "藝術(shù)特長(zhǎng)" , "介紹" }; 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); //創(chuàng)建標(biāo)題 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); // 寫(xiě)入各條記錄,每條記錄對(duì)應(yīng)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); } } } // 設(shè)置列寬 for ( int i = 1 ; i < alias.length; i++) sheet.autosizecolumn(i); // 處理中文不能自動(dòng)調(diào)整列寬的問(wèn)題 this .setsizecolumn(sheet, alias.length); } fileoutput.flush(); workbook.write(fileoutput); fileoutput.close(); } // 自適應(yīng)寬度(中文支持) 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; //當(dāng)前行未被使用過(guò) 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導(dǎo)出圖片到excel示例代碼解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://blog.csdn.net/wkh___/article/details/87431066