java后臺利用Apache poi 生成excel文檔提供前臺下載示例
之前在項(xiàng)目中會用到在Java在后臺把數(shù)據(jù)填入Word文檔的模板來提供前臺下載,為了自己能隨時查看當(dāng)時的實(shí)現(xiàn)方案及方便他人學(xué)習(xí)我寫了這篇博客,訪問量已經(jīng)是我寫的博客里第一了。于是乎我在學(xué)會用Java在后臺利用Apache poi 生成excel文檔提供前臺下載之后就想著來寫一篇姊妹篇啦。
在生成Excel文檔的時候我采用了和生成Word時的不同方法,Apache poi。它是用Java編寫的免費(fèi)開源的跨平臺的 Java API,提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。想要實(shí)現(xiàn)這個功能,就按照下面的步驟來做吧,為了方便起見,我直接拿項(xiàng)目中遇到的實(shí)例來舉例說明,是的,我在寫這篇博客的時候同時也在完成手上的項(xiàng)目。
step1:創(chuàng)建xls格式的模板
表頭含有我的甲方信息就打碼了,可以看到我搞了一個空的模板文件,現(xiàn)在有很多東西需要在后臺填入
step2:前臺觸發(fā)事件
搞一個按鈕,用戶點(diǎn)擊的時候用JavaScript的window.location.href將頁面重定向到你處理下載的URL去
比方說,這是我項(xiàng)目的前臺,看到那個表面質(zhì)量按鈕嗎,來看一下當(dāng)它被點(diǎn)擊的時候調(diào)用的函數(shù)
function exportBatch() { //get請求,可以傳遞參數(shù),比方說我這里就傳了一堆卷號,我只生成傳過去的這堆卷號的檢驗(yàn)記錄 //參數(shù)rollNumbers的細(xì)節(jié)就不展示了,業(yè)務(wù)相關(guān) window.location.href = '../ir/exportSurface?rollNumberList=' + rollNumbers; }
有朋友可能想用什么Ajax來發(fā)送請求,我反正是沒搞出來,挺麻煩的,網(wǎng)上找的相關(guān)解決方案也都比較~,因此不傳什么復(fù)雜的敏感的參數(shù),就這么寫就可以。
step3:后臺處理
首先你當(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)目中建了一個ExcelUtils工具類,后面給出源碼,這么一來導(dǎo)出Excel會變得更簡單。ExcelUtils里面除了一些既定的方法外,還有就是你具體怎么去操作模板的方法了。當(dāng)然你用的少的話可以不用我這工具類,而是在你需要的時候import相關(guān)的類,然后在你處理的時候就把操作模板的邏輯寫進(jìn)去也可以。但我這個項(xiàng)目很多次用到導(dǎo)出Excel,所以抽象出一個工具類是很有必要的,符合設(shè)計模式。
我的項(xiàng)目是基于SpringMVC的,來看看我后臺接收到請求以后做了些什么吧
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ù)放在一個map里 Map<String, Object> map = new HashMap<String, Object>(); map.put("sequence", "0001");//mock編號 map.put("date", DateUtils.toDateStr(new Date(), DateUtils.DEFAULT_DATE_PATTERN_CHINESE)); map.put("chetaihao", "1#");//mock車臺號 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)出方法,這個方法是自定義的,它定義的是怎樣去操作模板
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)上文里有,改個函數(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("文件路徑錯誤"); e.printStackTrace(); } catch (IOException e) { System.out.println("文件輸入流錯誤"); 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("輸出流錯誤"); 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:啟動項(xiàng)目,然后測試一下,看!完美的導(dǎo)出了。。。有圖為證
嗯嗯,文章寫到這里就結(jié)束啦,Apache poi還提供了很多API在本例中為得到展示,比如能夠指定樣式等等。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)抽獎算法的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何通過SpringBoot實(shí)現(xiàn)抽獎算法,文中的示例代碼簡潔易懂,具有一定的參考價值,感興趣的小伙伴可以了解一下2023-06-06詳解SpringBoot+Dubbo集成ELK實(shí)戰(zhàn)
這篇文章主要介紹了詳解SpringBoot+Dubbo集成ELK實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10詳解Java編程中統(tǒng)一資源定位符URL的相關(guān)使用
這篇文章主要介紹了Java編程中統(tǒng)一資源定位符URL的相關(guān)使用,是Java網(wǎng)絡(luò)編程中的基礎(chǔ)知識,需要的朋友可以參考下2015-10-10Java實(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-11macOS上使用gperftools定位Java內(nèi)存泄漏問題及解決方案
這篇文章主要介紹了macOS上使用gperftools定位Java內(nèi)存泄漏問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07springboot 實(shí)現(xiàn)長鏈接轉(zhuǎn)短鏈接的示例代碼
短鏈接服務(wù)通過將長URL轉(zhuǎn)換成6位短碼,并存儲長短鏈接對應(yīng)關(guān)系到數(shù)據(jù)庫中,用戶訪問短鏈接時,系統(tǒng)通過查詢數(shù)據(jù)庫并重定向到原始URL,實(shí)現(xiàn)快速訪問,本文就來介紹一下如何使用,感興趣的可以了解一下2024-09-09