利用POI進行Excel的導出
阿新 • • 發佈:2017-11-12
alert height 編碼 標題樣式 div for cor option data-
需求:將用戶的違約金信息導出為excel表格格式
步驟
1. 數據庫中增加按鈕的值(註意上級編號要和頁面隱藏域中的相等)
DZ內容(頁面加載時根據SJBH查詢數據庫內容,讀取DZ字段信息並加載樣式及方法)
<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:‘icon-remove‘" onclick="getWyj();">違約金查詢</a>
頁面加載後菜單欄出現“違約金查詢”按鈕
2. 創建js方法
sf_wyjcx.js
function getWyj(){ var userNode = frames[‘threeModel‘].$("#topUserList").datagrid("getSelected"); if (!userNode) { pubAlertMessage({ "msg" : "請選擇用戶信息!", "type" : BDXC_ALERT_TYPE.INFO }); return; } var yhbh = userNode.YHBH;var postData = { ‘jmjs.yhbh‘ : yhbh }; var paramJson = { "winDivId" :"getWyjWin", "title" :"違約金查詢", "width" :920,//300, // 790 "height" :400, // 520 "iconCls" :"icon-edit", "inDivId" :"getWyj","loadUrl" :"UserPayAction!getWyj.action", "postData" :postData, "watingTime":100, "defineFun":"exportExcel("+yhbh+")", "initFunc":"" }; //pubLoadWinPage(paramJson); pubLoadWyj(paramJson); } function pubLoadWyj(paramJson) { var watingStartJson = { "time" : paramJson.watingTime }; pubWatingStart(watingStartJson); $.ajax({ url : paramJson.loadUrl, type : ‘post‘, data : paramJson.postData, dataType : ‘html‘, error : function (e) { pubWatingEnd(); pubAlertMessage({ "msg" : "加載頁面失敗!", "type" : BDXC_ALERT_TYPE.ERROR }); }, success : function (data) { pubWatingEnd(); var winDiv = $(‘#‘ + paramJson.winDivId); if (winDiv.length == 0) { $(‘<div id="‘ + paramJson.winDivId + ‘"></div>‘).appendTo(document.body); } var canelFun = "pubWinClose(‘" + paramJson.winDivId + "‘,‘‘);"; if (paramJson.canelFun) { canelFun = "pubWinClose(‘" + paramJson.winDivId + "‘,‘" + paramJson.canelFun + "‘);"; } var inDivId = paramJson.inDivId || paramJson.winDivId + ‘Content‘; paramJson.inDivId = inDivId; paramJson.winHtml = ‘<div class="easyui-layout" fit="true">‘ + ‘<div id="‘ + paramJson.inDivId + ‘" region="center" border="false" class="winDataContent">‘ + data + ‘</div>‘; if (!paramJson.isNormal) { paramJson.winHtml += ‘<div region="south" border="false" class="winOpLine" style="float:right">‘ + ‘<a id="btnCancel" class="easyui-linkbutton" icon="icon-cancel" href="javascript:‘ + canelFun + ‘">關閉</a>‘ + ‘<a id="btnEp" class="easyui-linkbutton" icon="icon-ok" href="javascript:‘ + paramJson.defineFun + ‘" >導出</a>‘ + ‘</div>‘; } paramJson.winHtml += ‘</div>‘; pubInitWindow(paramJson); } }); } //導出表格 function exportExcel(yhbh){ var url = "UserPayAction!getWyjExcel.action?yhbh="+yhbh; $.messager.confirm("操作提示","是否導出違約金信息?",function(data){ if(data){ location.href = url; } }); }
3. 點擊菜單欄的“違約金查詢”首先加載用戶所有的違約金信息
後臺方法如下:
UserPayAction.java
// 查詢違約金 public String getWyj(){ jmjs.setYhbh(jmjs.getYhbh()); List list=userPayService.getWyjlist(jmjs); jmjs.setList(list); return "addwyj"; }
UserPayDaoImpl.java
public List getWyjlist(SfJmjsT vo){ List list = new ArrayList(); StringBuilder sql = new StringBuilder(); List result = null; sql.append("select a.*,b.*,c.* from (select cnq,sfmj,dj,ysje,sfje,qfje,decode(sfhjwyj,‘否‘,SF_ZNJ_F_CFD(‘居民‘,yhbh,?,cnq),znjys)znj," + "SF_ZNJ_F_TS(yhbh,?,cnq)ts from sf_jmjs_t where zf=0 and qfje>0 and yhbh=? )a," + "(select sum(qfje) zqf,sum(decode(sfhjwyj,‘否‘,SF_ZNJ_F_CFD(‘居民‘,yhbh,?,cnq),znjys))zznj from sf_jmjs_t " + "where zf=0 and qfje>0 and yhbh=?)b,(select yhbm,yhkh,yhmc,lxdh,fgs,qy,rlz,xq,dz from sf_jmyh_t where zf=0 and yhbh=?)c"); Date da=new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(da); DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); Date date=null; try { date = format1.parse(dateString); } catch (ParseException e) { e.printStackTrace(); } list.add(date); list.add(date); list.add(vo.getYhbh()); list.add(date); list.add(vo.getYhbh()); list.add(vo.getYhbh()); try { result = this.executeSqlList(sql.toString(), list.toArray(), SfJmjsT.class); } catch (BDXCException e) { e.printStackTrace(); logger.error(BDXCUtil.getExceptionString(e)); } return result; }
返回的數據在頁面進行遍歷
sf_wyjcx.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <%@ taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core"%> <script type="text/javascript" src="js/common/validate.js"></script> <script type="text/javascript"> </script> <form id="getWyjForm" method="post" action=""> <s:token></s:token> <div> <table> <tbody> <fieldset class="fieldsetCss" style="width:95%;"> <legend class="legendCss" >違約金信息</legend> <table id="getWyj" border="1"> <tr> <td align="center">采暖期</td> <td align="center">用戶編碼</td> <td align="center">用戶名稱</td> <td align="center">聯系電話</td> <td align="center">地址</td> <td align="center">收費面積</td> <td align="center">單價</td> <td align="center">應收金額</td> <td align="center">實收金額</td> <td align="center">欠費金額</td> <td align="center">違約金</td> <td align="center">違約金天數</td> </tr>
<!--中間欄對含違約金結算信息進行遍歷--> <s:iterator value="jmjs.list" var="b"> <tr> <td class="contentCss" align="center">${b.cnq }</td> <td class="contentCss" align="center">${b.yhbm }</td> <td class="contentCss" align="center">${b.yhmc }</td> <td class="contentCss" align="center">${b.lxdh }</td> <td class="contentCss" align="center">${b.dz }</td> <td class="contentCss" align="center">${b.sfmj }</td> <td class="contentCss" align="center">${b.dj }</td> <td class="contentCss" align="center">${b.ysje }</td> <td class="contentCss" align="center">${b.sfje }</td> <td class="contentCss" align="center">${b.qfje }</td> <td class="contentCss" align="center">${b.znj }</td> <td class="contentCss" align="center">${b.ts }</td> </tr> </s:iterator>
<tr> <td class="contentCss" align="center">合計:</td> <td class="contentCss" align="center"></td> <td class="contentCss" align="center"></td> <td class="contentCss" align="center"></td> <td class="contentCss" align="center"></td> <td class="contentCss" align="center"></td> <td class="contentCss" align="center"></td> <td class="contentCss" align="center"></td> <td class="contentCss" align="center"></td> <td class="contentCss" align="center">${b.zqf }</td> <td class="contentCss" align="center">${b.zznj }</td> <td class="contentCss" align="center"></td> </tr> </table> </fieldset> </tbody> </table> </div> </form>
頁面顯示效果
4. 導出
UserPayAction.java
/** * 導出違約金信息 */ public String getWyjExcel(){ String yhbh = this.getRequest().getParameter("yhbh"); wyjExcelIs = userPayService.getWyjExcelIs(context,yhbh); return "wyjExcel"; } /** * 違約金導出表格名稱*/ public String getWyjExcelName() { String wyjExcelName = null; try { wyjExcelName = new String("違約金信息.xls".getBytes(), "ISO-8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); logger.error(BDXCUtil.getExceptionString(e)); } return wyjExcelName; } public InputStream getWyjExcelIs() { return wyjExcelIs; } public void setWyjExcelIs(InputStream wyjExcelIs) { this.wyjExcelIs = wyjExcelIs; }
struts.xml
<result name="wyjExcel" type="stream"> <param name="contentType">application/vnd.ms-excel</param> <param name="inputName">wyjExcelIs</param> <param name="contentDisposition"> attachment;fileName = "${wyjExcelName}" </param> <param name="bufferSize">1024</param> </result>
UserPayServiceImpl.java
/** * 導出違約金信息 * * @return 違約金信息文件的輸入流 */ public InputStream getWyjExcelIs(BDXCPageContext context, String yhbh) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { HSSFWorkbook workbook = new HSSFWorkbook(); //頭標題樣式 HSSFCellStyle style1 = workbook.createCellStyle(); // 樣式對象 style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直 style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平 HSSFFont f1 = workbook.createFont(); f1.setFontName("宋體"); f1.setFontHeightInPoints((short) 20);//字號 f1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗 style1.setFont(f1); //表頭樣式 HSSFCellStyle style2 = workbook.createCellStyle(); // 樣式對象 style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直 style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平 style2.setWrapText(true); style2.setBorderBottom((short) 1);//下邊框 style2.setBorderLeft((short) 1);//左邊框 style2.setBorderRight((short) 1);//右邊框 style2.setBorderTop((short) 1);//上邊框 HSSFFont f2 = workbook.createFont(); f2.setFontName("宋體"); f2.setFontHeightInPoints((short) 12);//字號 f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗 style2.setFont(f2); //表體樣式 HSSFCellStyle style3 = workbook.createCellStyle(); // 樣式對象 style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直 style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平 style3.setWrapText(true); style3.setBorderBottom((short) 1);//下邊框 style3.setBorderLeft((short) 1);//左邊框 style3.setBorderRight((short) 1);//右邊框 style3.setBorderTop((short) 1);//上邊框 HSSFFont f3 = workbook.createFont(); f3.setFontName("宋體"); f3.setFontHeightInPoints((short) 10);//字號 style3.setFont(f3); //添加工作表 HSSFSheet sheet = workbook.createSheet("違約金信息"); //設置默認行高 sheet.setDefaultRowHeightInPoints(25); //設置工作表列寬 for (short j = 0; j < 12; j++) { if(j==4){ sheet.setColumnWidth(j, (short) (20 * 256)); }else if(j==11){ sheet.setColumnWidth(j, (short) (16 * 256)); }else{ sheet.setColumnWidth(j, (short) (12 * 256)); } } //添加表頭信息 List<String> headList = new ArrayList<String>(); headList.add("采暖期"); headList.add("用戶編碼"); headList.add("用戶名稱"); headList.add("聯系電話"); headList.add("地址"); headList.add("收費面積"); headList.add("單價"); headList.add("應收金額"); headList.add("實收金額"); headList.add("欠費金額"); headList.add("違約金"); headList.add("違約金天數"); //第一行 頭標題 HSSFRow nRow1 =sheet.createRow(0); nRow1.setHeight((short) (30 * 20)); HSSFCell ncell1 = nRow1.createCell((short) 0); ncell1.setCellValue("違約金信息"); ncell1.setCellStyle(style1); ncell1.setCellType(HSSFCell.CELL_TYPE_STRING); sheet.addMergedRegion(new Region(0,(short)0,0,(short)11)); //第二行 表頭 HSSFRow nRow2 =sheet.createRow(1); nRow2.setHeight((short) (25 * 20)); for (short k = 0; k < 12; k++) { HSSFCell cell = nRow2.createCell(k); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellStyle(style2); cell.setCellValue(headList.get(k)); } //表體 SfJmjsT jmjs = new SfJmjsT(); jmjs.setYhbh(Long.parseLong(yhbh)); List list=getWyjlist(jmjs); for (int i = 2; i < list.size()+2; i++) { SfJmjsT js = (SfJmjsT) list.get(i-2); HSSFRow Row =sheet.createRow(i); Row.setHeight((short) (25 * 20)); //采暖期 HSSFCell xcell = Row.createCell((short)0); xcell.setCellType(HSSFCell.CELL_TYPE_STRING); xcell.setCellStyle(style3); xcell.setCellValue(js.getCnq()); //用戶編碼 HSSFCell xcel2 = Row.createCell((short)1); xcel2.setCellType(HSSFCell.CELL_TYPE_STRING); xcel2.setCellStyle(style3); xcel2.setCellValue(js.getYhbm()); //用戶名稱 HSSFCell xcel3 = Row.createCell((short)2); xcel3.setCellType(HSSFCell.CELL_TYPE_STRING); xcel3.setCellStyle(style3); xcel3.setCellValue(js.getYhmc()); //聯系電話 HSSFCell xcel4 = Row.createCell((short)3); xcel4.setCellType(HSSFCell.CELL_TYPE_STRING); xcel4.setCellStyle(style3); xcel4.setCellValue(js.getLxdh()); //地址 HSSFCell xcel5 = Row.createCell((short)4); xcel5.setCellType(HSSFCell.CELL_TYPE_STRING); xcel5.setCellStyle(style3); xcel5.setCellValue(js.getDz()); //收費面積 HSSFCell xcel6 = Row.createCell((short)5); xcel6.setCellType(HSSFCell.CELL_TYPE_NUMERIC); xcel6.setCellStyle(style3); xcel6.setCellValue(js.getSfmj()); //單價 HSSFCell xcel7 = Row.createCell((short)6); xcel7.setCellType(HSSFCell.CELL_TYPE_NUMERIC); xcel7.setCellStyle(style3); xcel7.setCellValue(js.getDj()); //應收金額 HSSFCell xcel8 = Row.createCell((short)7); xcel8.setCellType(HSSFCell.CELL_TYPE_NUMERIC); xcel8.setCellStyle(style3); xcel8.setCellValue(js.getYsje()); //實收金額 HSSFCell xcel9 = Row.createCell((short)8); xcel9.setCellType(HSSFCell.CELL_TYPE_NUMERIC); xcel9.setCellStyle(style3); xcel9.setCellValue(js.getSfje()); //欠費金額 HSSFCell xcel10 = Row.createCell((short)9); xcel10.setCellType(HSSFCell.CELL_TYPE_NUMERIC); xcel10.setCellStyle(style3); xcel10.setCellValue(js.getQfje()); //違約金 HSSFCell xcel11 = Row.createCell((short)10); xcel11.setCellType(HSSFCell.CELL_TYPE_NUMERIC); xcel11.setCellStyle(style3); xcel11.setCellValue(js.getZnj()); //違約金天數 HSSFCell xcel12 = Row.createCell((short)11); xcel12.setCellType(HSSFCell.CELL_TYPE_STRING); xcel12.setCellStyle(style3); xcel12.setCellValue(js.getTs()); } //表尾 合計 SfJmjsT hjJs = (SfJmjsT) list.get(0); HSSFRow lastRow =sheet.createRow(list.size()+2); lastRow.setHeight((short) (25 * 20)); //采暖期 HSSFCell xcell = lastRow.createCell((short)0); xcell.setCellType(HSSFCell.CELL_TYPE_STRING); xcell.setCellStyle(style2); xcell.setCellValue("合計:"); //用戶編碼 HSSFCell xcel2 = lastRow.createCell((short)1); xcel2.setCellType(HSSFCell.CELL_TYPE_STRING); xcel2.setCellStyle(style2); xcel2.setCellValue(""); //用戶名稱 HSSFCell xcel3 = lastRow.createCell((short)2); xcel3.setCellType(HSSFCell.CELL_TYPE_STRING); xcel3.setCellStyle(style2); xcel3.setCellValue(""); //聯系電話 HSSFCell xcel4 = lastRow.createCell((short)3); xcel4.setCellType(HSSFCell.CELL_TYPE_STRING); xcel4.setCellStyle(style2); xcel4.setCellValue(""); //地址 HSSFCell xcel5 = lastRow.createCell((short)4); xcel5.setCellType(HSSFCell.CELL_TYPE_STRING); xcel5.setCellStyle(style2); xcel5.setCellValue(""); //收費面積 HSSFCell xcel6 = lastRow.createCell((short)5); xcel6.setCellType(HSSFCell.CELL_TYPE_STRING); xcel6.setCellStyle(style2); xcel6.setCellValue(""); //單價 HSSFCell xcel7 = lastRow.createCell((short)6); xcel7.setCellType(HSSFCell.CELL_TYPE_STRING); xcel7.setCellStyle(style2); xcel7.setCellValue(""); //應收金額 HSSFCell xcel8 = lastRow.createCell((short)7); xcel8.setCellType(HSSFCell.CELL_TYPE_STRING); xcel8.setCellStyle(style2); xcel8.setCellValue(""); //實收金額 HSSFCell xcel9 = lastRow.createCell((short)8); xcel9.setCellType(HSSFCell.CELL_TYPE_STRING); xcel9.setCellStyle(style2); xcel9.setCellValue(""); //欠費金額 HSSFCell xcel10 = lastRow.createCell((short)9); xcel10.setCellType(HSSFCell.CELL_TYPE_NUMERIC); xcel10.setCellStyle(style2); xcel10.setCellValue(hjJs.getZqf()); //違約金 HSSFCell xcel11 = lastRow.createCell((short)10); xcel11.setCellType(HSSFCell.CELL_TYPE_NUMERIC); xcel11.setCellStyle(style2); xcel11.setCellValue(hjJs.getZznj()); //違約金天數 HSSFCell xcel12 = lastRow.createCell((short)11); xcel12.setCellType(HSSFCell.CELL_TYPE_STRING); xcel12.setCellStyle(style2); xcel12.setCellValue(""); workbook.write(out); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return new ByteArrayInputStream(out.toByteArray()); }
效果(有些版本的谷歌瀏覽器導出時可能不跳出彈窗,直接下載,避免這種情況可以使用火狐瀏覽器)
excel表格
利用POI進行Excel的導出