之前在項(xiàng)目中會(huì)用到在java在后臺(tái)把數(shù)據(jù)填入word文檔的模板來(lái)提供前臺(tái)下載,為了自己能隨時(shí)查看當(dāng)時(shí)的實(shí)現(xiàn)方案及方便他人學(xué)習(xí)我寫(xiě)了這篇博客,訪問(wèn)量已經(jīng)是我寫(xiě)的博客里第一了。于是乎我在學(xué)會(huì)用java在后臺(tái)利用apache poi 生成excel文檔提供前臺(tái)下載之后就想著來(lái)寫(xiě)一篇姊妹篇啦。
在生成excel文檔的時(shí)候我采用了和生成word時(shí)的不同方法,apache poi。它是用java編寫(xiě)的免費(fèi)開(kāi)源的跨平臺(tái)的 java api,提供api給java程式對(duì)microsoft office格式檔案讀和寫(xiě)的功能。想要實(shí)現(xiàn)這個(gè)功能,就按照下面的步驟來(lái)做吧,為了方便起見(jiàn),我直接拿項(xiàng)目中遇到的實(shí)例來(lái)舉例說(shuō)明,是的,我在寫(xiě)這篇博客的時(shí)候同時(shí)也在完成手上的項(xiàng)目。
step1:創(chuàng)建xls格式的模板
表頭含有我的甲方信息就打碼了,可以看到我搞了一個(gè)空的模板文件,現(xiàn)在有很多東西需要在后臺(tái)填入
step2:前臺(tái)觸發(fā)事件
搞一個(gè)按鈕,用戶點(diǎn)擊的時(shí)候用javascript的window.location.href將頁(yè)面重定向到你處理下載的url去
比方說(shuō),這是我項(xiàng)目的前臺(tái),看到那個(gè)表面質(zhì)量按鈕嗎,來(lái)看一下當(dāng)它被點(diǎn)擊的時(shí)候調(diào)用的函數(shù)
1
2
3
4
5
|
function exportbatch() { //get請(qǐng)求,可以傳遞參數(shù),比方說(shuō)我這里就傳了一堆卷號(hào),我只生成傳過(guò)去的這堆卷號(hào)的檢驗(yàn)記錄 //參數(shù)rollnumbers的細(xì)節(jié)就不展示了,業(yè)務(wù)相關(guān) window.location.href = '../ir/exportsurface?rollnumberlist=' + rollnumbers; } |
有朋友可能想用什么ajax來(lái)發(fā)送請(qǐng)求,我反正是沒(méi)搞出來(lái),挺麻煩的,網(wǎng)上找的相關(guān)解決方案也都比較蛋疼,因此不傳什么復(fù)雜的敏感的參數(shù),就這么寫(xiě)就可以。
step3:后臺(tái)處理
首先你當(dāng)然要把a(bǔ)pache poi那一套東西引入你的項(xiàng)目啦,我的項(xiàng)目是maven項(xiàng)目,添加依賴(lài)很容易
1
2
3
4
5
|
<dependency> <groupid>org.apache.poi</groupid> <artifactid>poi</artifactid> <version> 3.14 </version> </dependency> |
然后,為了方便導(dǎo)出excel,在項(xiàng)目中建了一個(gè)excelutils工具類(lèi),后面給出源碼,這么一來(lái)導(dǎo)出excel會(huì)變得更簡(jiǎn)單。excelutils里面除了一些既定的方法外,還有就是你具體怎么去操作模板的方法了。當(dāng)然你用的少的話可以不用我這工具類(lèi),而是在你需要的時(shí)候import相關(guān)的類(lèi),然后在你處理的時(shí)候就把操作模板的邏輯寫(xiě)進(jìn)去也可以。但我這個(gè)項(xiàng)目很多次用到導(dǎo)出excel,所以抽象出一個(gè)工具類(lèi)是很有必要的,符合設(shè)計(jì)模式。
我的項(xiàng)目是基于springmvc的,來(lái)看看我后臺(tái)接收到請(qǐng)求以后做了些什么吧
controller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/*** * 批量導(dǎo)出表面質(zhì)量檢驗(yàn)記錄 * * @return * @throws exception */ @requestmapping (value = "exportsurface" , method = requestmethod.get) @responsebody public void exportsurface(httpservletrequest request, httpservletresponse response) throws exception { //參數(shù)獲取及處理,業(yè)務(wù)相關(guān)不展示 //把要填寫(xiě)的數(shù)據(jù)放在一個(gè)map里 map<string, object> map = new hashmap<string, object>(); map.put( "sequence" , "0001" ); //mock編號(hào) map.put( "date" , dateutils.todatestr( new date(), dateutils.default_date_pattern_chinese)); map.put( "chetaihao" , "1#" ); //mock車(chē)臺(tái)號(hào) map.put( "productname" , "預(yù)應(yīng)力鋼絞線" ); //mock品名 map.put( "specification" , "規(guī)格" ); //mock規(guī)格 map.put( "memo" , "備注" ); //mock備注 map.put( "inspectrecordbizlist" , inspectrecodebizlist); excelutils.exportinspectionrecordsurface(request, response, map); } |
最后調(diào)用excelutils里的相關(guān)導(dǎo)出方法,這個(gè)方法是自定義的,它定義的是怎樣去操作模板
自定義的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public static void exportinspectionrecordsurface(httpservletrequest request, httpservletresponse response, map map) throws ioexception { //模板的路徑,這個(gè)在自己的項(xiàng)目中很容易弄錯(cuò),相對(duì)位置一定要寫(xiě)對(duì)啊 string psth = request.getrealpath( "/" ) + inspectionrecord_surface_templet_path; workbook webbook = readexcel(psth); createcellstyle(webbook); sheet sheet = webbook.getsheetat( 0 ); //開(kāi)始操作模板,找到某行某列(某個(gè)cell),需要注意的是這里有個(gè)坑,行和列的計(jì)數(shù)都是從0開(kāi)始的 //一次數(shù)據(jù)插入的位置不對(duì),別灰心,多試幾次就好啦,你要是能看懂我下面的代碼,數(shù)據(jù)插在了什么位置,你就明白了 int rows = 1 ; row row = sheet.getrow(rows); row.createcell( 1 ).setcellvalue((string) map.get( "sequence" )); row.createcell( 3 ).setcellvalue((string) map.get( "date" )); row.createcell( 9 ).setcellvalue((string) map.get( "chetaihao" )); rows = 2 ; row = sheet.getrow(rows); row.createcell( 1 ).setcellvalue((string) map.get( "productname" )); row.createcell( 3 ).setcellvalue((string) map.get( "specification" )); row.createcell( 9 ).setcellvalue((string) map.get( "memo" )); //檢驗(yàn)記錄的插入業(yè)務(wù)相關(guān),不展示,其實(shí)就是for循環(huán)在合適的行合適的列插入一個(gè)個(gè)對(duì)象的屬性即可,你這么聰明,沒(méi)問(wèn)題的 writeexcel(response, webbook, "表面質(zhì)量檢驗(yàn)記錄" ); } |
excelutils:
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
|
//這里得有你自己的package名 import org.apache.poi.hssf.usermodel.hssfcellstyle; import org.apache.poi.hssf.usermodel.hssffont; import org.apache.poi.hssf.usermodel.hssfrichtextstring; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.ss.usermodel.*; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.*; import java.net.urlencoder; import java.util.arraylist; import java.util.list; import java.util.map; /** * created by bwju on 2016/12/06. */ public class excelutils { private static final string inspectionrecord_surface_templet_path = "/asserts/templete/inspectionrecordsurface.xls" ; private static hssfcellstyle cellstyle = null ; public static void exportinspectionrecordsurface(httpservletrequest request, httpservletresponse response, map map) throws ioexception { //實(shí)現(xiàn)上文里有,改個(gè)函數(shù)名,寫(xiě)你的操作模板函數(shù)吧! } private static workbook readexcel(string filepath) { inputstream in = null ; workbook work = null ; try { in = new fileinputstream(filepath); work = new hssfworkbook(in); } catch (filenotfoundexception e) { system.out.println( "文件路徑錯(cuò)誤" ); e.printstacktrace(); } catch (ioexception e) { system.out.println( "文件輸入流錯(cuò)誤" ); e.printstacktrace(); } return work; } private static void writeexcel(httpservletresponse response, workbook work, string filename) throws ioexception { outputstream out = null ; try { out = response.getoutputstream(); response.setcontenttype( "application/ms-excel;charset=utf-8" ); response.setheader( "content-disposition" , "attachment;filename=" .concat(string.valueof(urlencoder.encode(filename + ".xls" , "utf-8" )))); work.write(out); } catch (ioexception e) { system.out.println( "輸出流錯(cuò)誤" ); e.printstacktrace(); } finally { out.close(); } } private static cell setcellstylewithstyleandvalue(cellstyle style, cell cell, string value) { cell.setcellstyle(style); cell.setcellvalue(value); return cell; } private static cell setcellstylewithvalue(cell cell, string value) { cell.setcellstyle(cellstyle); cell.setcellvalue(value); return cell; } private static cell setcellstylewithstyleandvalue(cellstyle style, cell cell, richtextstring value) { cell.setcellstyle(style); cell.setcellvalue(value); return cell; } private static cell setcellstylewithvalue(cell cell, int value) { cell.setcellstyle(cellstyle); cell.setcellvalue(value); return cell; } private static cell setcellstylewithvalue(cell cell, double value) { cell.setcellstyle(cellstyle); cell.setcellvalue(value); return cell; } private static hssfcellstyle createcellstyle(workbook wb) { cellstyle = (hssfcellstyle) wb.createcellstyle(); cellstyle.setalignment(hssfcellstyle.align_center); cellstyle.setborderbottom(hssfcellstyle.border_thin); cellstyle.setborderleft(hssfcellstyle.border_thin); cellstyle.setborderright(hssfcellstyle.border_thin); cellstyle.setbordertop(hssfcellstyle.border_thin); cellstyle.setverticalalignment(hssfcellstyle.vertical_center); return cellstyle; } } |
step4:啟動(dòng)項(xiàng)目,然后測(cè)試一下,看!完美的導(dǎo)出了。。。有圖為證
嗯嗯,文章寫(xiě)到這里就結(jié)束啦,apache poi還提供了很多api在本例中為得到展示,比如能夠指定樣式等等。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/u010251278/article/details/53491258