我們?cè)谧鲞@個(gè)項(xiàng)目的時(shí)候,需要導(dǎo)出一個(gè)excel文件,所以我就想到了使用poi導(dǎo)出數(shù)據(jù)
下面把代碼貼出來(lái),供大家參考,親測(cè)有效,希望小伙伴不要走彎路。
首先把poi? jar包導(dǎo)入項(xiàng)目中,我把我導(dǎo)入的節(jié)點(diǎn)提供給大家
<dependency>? ?
?<groupId>org.apache.poi</groupId>
? ? <artifactId>poi</artifactId>
? ? <version>3.9</version>
</dependency>
第一步,先從controller開(kāi)始
@RequestMapping("/poi/load")
public String poi(Integer soulId, HttpServletResponse response, HttpSession session){
logger.info("進(jìn)入poi/poi接口");
? ? HSSFWorkbook hssfWorkbook =poiService.selectBySoulId(soulId, response);
? ? session.setAttribute("hssfWorkbook",hssfWorkbook);
? ? return "test/test";
}
我們的業(yè)務(wù)需求是前臺(tái)傳來(lái)soulId的值來(lái)下載一個(gè)excel文件
第二步,進(jìn)入service層
@Override
? ? public HSSFWorkbook? selectBySoulId(Integer soulId, HttpServletResponse response) {
? ? //從數(shù)據(jù)庫(kù)獲取數(shù)據(jù)
? ? ? ? List>list =sysConfigMapper.selectBySoulId(soulId);
? ? ? ? //excel標(biāo)題
? ? ? ? String[]title ={"商品編號(hào)","商品名稱","商品規(guī)格","商品價(jià)格","貨架名稱"};
? ? ? ? //excel文件名
? ? ? ? String fileName ="臺(tái)賬詳情表" +System.currentTimeMillis() +".xls";
? ? ? ? //內(nèi)容列表 行、列
? ? ? ? int size =list.size();
? ? ? ? String [] []content =new String [size][title.length];
? ? ? ? //sheet名
? ? ? ? String sheetName ="全家臺(tái)賬詳情表";
? ? ? ? if(size>0){
? ? ? ? ? ?for (int i =0; i<size; i++){
? ? ? ? ? ? ? ? ? ? Map?map =list.get(i);
? ? ? ? ? ? ? ? try{
? ? ? ? ? ? ? ? ? ? ? ? ? ?content[i][0] =map.get("ABIARTICLEID").toString(); //商品編號(hào)
? ? ? ? ? ? ? ? ? ? ? ? ? ?content[i][1] =map.get("ABIARTICLENAME").toString(); //商品名稱
? ? ? ? ? ? ? ? ? ? ? ? ? ?content[i][2] =map.get("ABISPEC").toString();//商品規(guī)格
? ? ? ? ? ? ? ? ? ? ? ? ? ? content[i][3] =map.get("ASPISTANDARDSALESPRICE").toString();//商品價(jià)格TITLE
? ? ? ? ? ? ? ? ? ? ? ? ? ? content[i][4] =map.get("TITLE").toString();//貨架名稱
? ? ? ? ? ? ? ? ? ?}catch (Exception e){
? ? ? ? ? ? ? ? }
? ? ? ? }
}
// 創(chuàng)建HSSFWorkbook
? ? ? ? HSSFWorkbook wb =ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
? ? ? ? try {
? ? ? ? ? ? ? ? ? ?ExportUtil.exportExcel(response, fileName, wb);
? ? ? ? ? ? }catch (Exception e) {
? ? ? ? ? ? }
? ? ? ? ? ? ? ? return? wb;
? ? }
service的接口
/**
* 查詢poi下載所需要的數(shù)據(jù)
*/
public HSSFWorkbook? selectBySoulId(Integer soulId, HttpServletResponse response);
第三步? ?mapper層
<select id="selectBySoulId" parameterType="Integer" resultType="java.util.HashMap">
這里是你自己的業(yè)務(wù),你所需要下載的數(shù)據(jù),返回的是一個(gè)List<Map<String,Object>>格式
</select>
mapper層接口
/**
* 查詢poi下載所需要的數(shù)據(jù)
*/
public?List<Map<String,Object>>?selectBySoulId (Integer soulId)
第四步? ?分別是??ExcelUtil類? ?和? ?ExportUtil類??
public class ExcelUtil {
/**
* 導(dǎo)出Excel
*
? ? * @param sheetName? ?sheet名稱
? ? * @param title? ? 標(biāo)題
? ? * @param content 內(nèi)容
? ? * @param wb? HSSFWorkbook對(duì)象
? ? */
? ? public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[]title, String[][]content, HSSFWorkbook wb){
? ? ? ?//第一步 創(chuàng)建一個(gè)HSSFWorkbook, 對(duì)應(yīng)一個(gè)Excel文件
? ? ? ? ? ? ? ?if (wb ==null){
? ? ? ? ? ? ? ? ? ? ? ? wb =new HSSFWorkbook();
? ? ? ? ? ? ? ? ?}
? ? ? ?//第二步, 在workbook中添加一個(gè)sheet,對(duì)應(yīng)excel文件中的sheet
? ? ? ? HSSFSheet sheet = wb.createSheet(sheetName);
? ? ? ? //第三步,在sheet中添加表頭第0行,注意老版本poi對(duì)Excel的行數(shù)列數(shù)有限制
? ? ? ? HSSFRow row =sheet.createRow(0);
? ? ? ? //第四步,創(chuàng)建單元格,并設(shè)置表頭 設(shè)置表頭居中
? ? ? ? HSSFCellStyle style = wb.createCellStyle();
? ? ? ? //創(chuàng)建一個(gè)居中格式
? ? ? ? style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
? ? ? ? //設(shè)置字體
? ? ? ? HSSFFont font = wb.createFont();
? ? ? ? //設(shè)置為楷體
? ? ? ? font.setFontName("楷體");
? ? ? ? //創(chuàng)建邊框?qū)ο?/p>
? ? ? ? HSSFCellStyle setBorder = wb.createCellStyle();
? ? ? ? //設(shè)置自動(dòng)換行
? ? ? ? setBorder.setWrapText(true);
? ? ? ? //聲明列對(duì)象
? ? ? ? HSSFCell cell;
? ? ? ? //創(chuàng)建標(biāo)題
? ? ? ? ? for(int i =0; i<title.length; i++){
? ? ? ? ? ? ? ? ?cell = row.createCell(i);
? ? ? ? ? ? ? ? ?cell.setCellValue(title[i]);
? ? ? ? ? ? ? ? cell.setCellStyle(style);
? ? ? ? ? }
? ? ? ?//創(chuàng)建內(nèi)容
? ? ? ? for(int i =0; i<content.length; i++){
? ? ? ? ? ? ? ? String str =",";
? ? ? ? ? ? ? ? System.out.println(content[i]);
? ? ? ? ? ? ? ? row =sheet.createRow(i +1);
? ? ? ? ? ? ? ?for (int j =0; j<content[i].length; i++){
? ? ? ? ? ? ? ? ? ? ?//將內(nèi)容按照順序賦給對(duì)應(yīng)的列對(duì)象
? ? ? ? ? ? ? ? ? ? ? row.createCell(j).setCellValue(String.valueOf(content[i][j]));
? ? ? ? ? ? ? }
}
? ? ? ? ? ? ? ? ?return wb;
? ? }
}
public class ExportUtil {
public static void exportExcel(HttpServletResponse response, String fileName, HSSFWorkbook wb)throws? Exception{
fileName =new String(fileName.getBytes(),"ISO-8859-1");
? ? ? ? response.setContentType("application/octet-stream;charset=ISO-8859-1");
? ? ? ? response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
? ? ? ? response.addHeader("Pargam", "no-cache");
? ? ? ? response.addHeader("Cache-Control", "no-cache");
? ? ? ? OutputStream os =response.getOutputStream();
? ? ? ? wb.write(os);
? ? ? ? os.flush();
? ? ? ? os.close();
? ? }
}
第五步
也就是前端請(qǐng)求
<button id="js-export" type="button" class="btn btn-primary">導(dǎo)出Excel
? ? $('#js-export').click(function(){
window.location.href="${pageContext.request.contextPath}/poi/load.do";
? ? });
</script>
至此,大工告成了,希望能幫助到更多的小伙伴,給個(gè)支持再走把!
不懂的還可以給我留言,或者留下聯(lián)系方式,我來(lái)幫大家。