簡單介紹一下java中的excel文件導出功能(基于httpservletresponse實現(xiàn)下載)
首先,引入需要依賴的jar包:
1
2
3
4
5
6
7
8
9
10
|
<dependency> <groupid>org.apache.poi</groupid> <artifactid>poi</artifactid> <version> 3.14 </version> </dependency> <dependency> <groupid>org.apache.poi</groupid> <artifactid>poi-ooxml</artifactid> <version> 3.14 </version> </dependency> |
編寫一個工具類:
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
|
package exceloutput; import org.apache.commons.lang3.stringutils; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.usermodel.workbook; import org.apache.poi.xssf.streaming.sxssfsheet; import org.apache.poi.xssf.streaming.sxssfworkbook; import javax.servlet.http.httpservletresponse; import java.io.ioexception; import java.io.unsupportedencodingexception; import java.util.list; import java.util.uuid; /** * @author haozz * @date 2018/6/6 9:57 * @description excel導出抽象工具類 **/ public abstract class exportabstractutil { public void write(httpservletresponse response, workbook workbook){ string filename = uuid.randomuuid().tostring()+ ".xls" ; pwrite(response,workbook,filename); } public void write(httpservletresponse response,workbook workbook,string filename){ if (stringutils.isempty(filename)){ filename = uuid.randomuuid().tostring()+ ".xls" ; } pwrite(response,workbook,filename); } public void write(httpservletresponse response, list<list<string>> lists,string filename){ if (stringutils.isempty(filename)){ filename = uuid.randomuuid().tostring()+ ".xls" ; } sxssfworkbook workbook = new sxssfworkbook(lists.size()); sxssfsheet sheet = workbook.createsheet(filename.substring( 0 ,filename.indexof( ".xls" ))); integer rowindex = 0 ; row row = null ; cell cell = null ; for (list<string> rowdata: lists ){ integer columnindex = 0 ; row = sheet.createrow(rowindex++); for (string columnval:rowdata){ cell = row.createcell(columnindex++); cell.setcellvalue(columnval); } } pwrite(response,workbook,filename); } private void pwrite(httpservletresponse response,workbook workbook,string filename){ response.setcharacterencoding( "utf-8" ); response.setcontenttype( "application/vnd.ms-excel;charset=utf-8" ); try { response.addheader( "content-disposition" , "attachment; filename=" + new string(filename.getbytes( "utf-8" ), "iso8859-1" )); } catch (unsupportedencodingexception e) { e.printstacktrace(); filename= uuid.randomuuid().tostring()+ ".xls" ; response.addheader( "content-disposition" , "attachment; filename=" +filename); } try { workbook.write(response.getoutputstream()); } catch (ioexception e) { e.printstacktrace(); } } } |
有了這個工具類就可以實現(xiàn)excel導出了,代碼不難,這里就不多解釋了。
在springboot項目中編寫一個導出excel的controller,并繼承上面的exportabstractutil,給出一個接口用作測試:
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
|
package com.csdn.myboot.controller; import com.csdn.myboot.utils.exportabstractutil; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responsebody; import javax.servlet.http.httpservletresponse; import java.util.arraylist; import java.util.arrays; import java.util.list; /** * @author haozz * @date 2018/6/6 10:14 * @description **/ @controller @requestmapping (value = "/index" ) public class helloctrl extends exportabstractutil{ @requestmapping (value = "/testexceloutput" ) @responsebody public void testexceloutput(httpservletresponse response){ //拼接數(shù)據(jù)start list<list<string>> lists = new arraylist<list<string>>(); string rows[] = { "year" , "month" , "day" }; list<string> rowstitle = arrays.aslist(rows); lists.add(rowstitle); for ( int i = 0 ; i<= 9 ;i++){ string [] rowss = { "1" , "2" , "3" }; list<string> rowsslist = arrays.aslist(rowss); lists.add(rowsslist); } //拼接數(shù)據(jù)end write(response,lists, "導出excel.xls" ); } } |
瀏覽器輸入鏈接:
http://localhost:8099/index/testexceloutput
即可自動下載測試數(shù)據(jù)組成的excel:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/hz_940611/article/details/80590488