java后臺(tái)利用Apache poi 生成excel文檔提供前臺(tái)下載示例
之前在項(xiàng)目中會(huì)用到在Java在后臺(tái)把數(shù)據(jù)填入Word文檔的模板來(lái)提供前臺(tái)下載,為了自己能隨時(shí)查看當(dāng)時(shí)的實(shí)現(xiàn)方案及方便他人學(xué)習(xí)我寫了這篇博客,訪問量已經(jīng)是我寫的博客里第一了。于是乎我在學(xué)會(huì)用Java在后臺(tái)利用Apache poi 生成excel文檔提供前臺(tái)下載之后就想著來(lái)寫一篇姊妹篇啦。
在生成Excel文檔的時(shí)候我采用了和生成Word時(shí)的不同方法,Apache poi。它是用Java編寫的免費(fèi)開源的跨平臺(tái)的 Java API,提供API給Java程式對(duì)Microsoft Office格式檔案讀和寫的功能。想要實(shí)現(xiàn)這個(gè)功能,就按照下面的步驟來(lái)做吧,為了方便起見,我直接拿項(xiàng)目中遇到的實(shí)例來(lái)舉例說明,是的,我在寫這篇博客的時(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去
比方說,這是我項(xiàng)目的前臺(tái),看到那個(gè)表面質(zhì)量按鈕嗎,來(lái)看一下當(dāng)它被點(diǎn)擊的時(shí)候調(diào)用的函數(shù)

function exportBatch() {
//get請(qǐng)求,可以傳遞參數(shù),比方說我這里就傳了一堆卷號(hào),我只生成傳過去的這堆卷號(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)求,我反正是沒搞出來(lái),挺麻煩的,網(wǎng)上找的相關(guān)解決方案也都比較~,因此不傳什么復(fù)雜的敏感的參數(shù),就這么寫就可以。
step3:后臺(tái)處理
首先你當(dāng)然要把Apache poi那一套東西引入你的項(xiàng)目啦,我的項(xiàng)目是Maven項(xiàng)目,添加依賴很容易
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency> 然后,為了方便導(dǎo)出Excel,在項(xiàng)目中建了一個(gè)ExcelUtils工具類,后面給出源碼,這么一來(lái)導(dǎo)出Excel會(huì)變得更簡(jiǎn)單。ExcelUtils里面除了一些既定的方法外,還有就是你具體怎么去操作模板的方法了。當(dāng)然你用的少的話可以不用我這工具類,而是在你需要的時(shí)候import相關(guān)的類,然后在你處理的時(shí)候就把操作模板的邏輯寫進(jìn)去也可以。但我這個(gè)項(xiàng)目很多次用到導(dǎo)出Excel,所以抽象出一個(gè)工具類是很有必要的,符合設(shè)計(jì)模式。
我的項(xiàng)目是基于SpringMVC的,來(lái)看看我后臺(tái)接收到請(qǐng)求以后做了些什么吧
Controller:
/***
* 批量導(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)不展示
//把要填寫的數(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車臺(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è)方法是自定義的,它定義的是怎樣去操作模板
ExcelUtils:
//這里得有你自己的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ù)名,寫你的操作模板函數(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)出了。。。有圖為證

嗯嗯,文章寫到這里就結(jié)束啦,Apache poi還提供了很多API在本例中為得到展示,比如能夠指定樣式等等。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)抽獎(jiǎng)算法的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何通過SpringBoot實(shí)現(xiàn)抽獎(jiǎng)算法,文中的示例代碼簡(jiǎn)潔易懂,具有一定的參考價(jià)值,感興趣的小伙伴可以了解一下2023-06-06
詳解SpringBoot+Dubbo集成ELK實(shí)戰(zhàn)
這篇文章主要介紹了詳解SpringBoot+Dubbo集成ELK實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
詳解Java編程中統(tǒng)一資源定位符URL的相關(guān)使用
這篇文章主要介紹了Java編程中統(tǒng)一資源定位符URL的相關(guān)使用,是Java網(wǎng)絡(luò)編程中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10
Java實(shí)現(xiàn)word轉(zhuǎn)pdf并在關(guān)鍵字位置插入圖片
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)word轉(zhuǎn)pdf,并在word中關(guān)鍵字位置插入圖片,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
macOS上使用gperftools定位Java內(nèi)存泄漏問題及解決方案
這篇文章主要介紹了macOS上使用gperftools定位Java內(nèi)存泄漏問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
springboot 實(shí)現(xiàn)長(zhǎng)鏈接轉(zhuǎn)短鏈接的示例代碼
短鏈接服務(wù)通過將長(zhǎng)URL轉(zhuǎn)換成6位短碼,并存儲(chǔ)長(zhǎng)短鏈接對(duì)應(yīng)關(guān)系到數(shù)據(jù)庫(kù)中,用戶訪問短鏈接時(shí),系統(tǒng)通過查詢數(shù)據(jù)庫(kù)并重定向到原始URL,實(shí)現(xiàn)快速訪問,本文就來(lái)介紹一下如何使用,感興趣的可以了解一下2024-09-09

