今天工作中遇到了excel表格導出的操作,還好有寫好的模板,不然我也是焦頭爛額,下面記錄一下excel表格導出的操作步驟,同時用來給正在學習jfinal的小伙伴一些參考和學習。
首先我們需要把表格查詢出並且顯示在頁面上,下面是頁面跳轉時經過的Controller:(這里包含條件查詢,所以獲取信息比較復雜,全部查詢只需要將獲取到的pageUtils 傳到前台即可,不需要添加多余的查詢條件,在這里傳給前台的變量名我叫他page)
public void index(){ PageUtils pageUtils = getPageParameter(); String where = " where 1=1 "; //獲取當前時間 String nowDate =DateUtils.getDate(); if(pageUtils.getMap().get("orderNo")!=null&&!pageUtils.getMap().get("orderNo").toString().trim().equals("")){ where = where + " and order_no like '%"+pageUtils.getMap().get("orderNo")+ "%' "; } if(pageUtils.getMap().get("customerName")!=null&&!pageUtils.getMap().get("customerName").toString().trim().equals("")){ where = where + " and customer_name like '%"+pageUtils.getMap().get("customerName")+"%'"; } if(pageUtils.getMap().get("shipmentDate")!=null&&!pageUtils.getMap().get("shipmentDate").toString().trim().equals("")){ where = where + " and shipment_date = '"+pageUtils.getMap().get("shipmentDate")+"'"; } if(pageUtils.getMap().get("isship")!=null&&!pageUtils.getMap().get("isship").toString().trim().equals("")){ if(pageUtils.getMap().get("isship").equals("1")){ where = where + " and Tknum is not null "; }else if (pageUtils.getMap().get("isship").equals("3")){ where = where + " and ship_type!=5 and arrive_date<'"+nowDate+"' "; }else{ where = where + " and Tknum is null "; } } if(pageUtils.getMap().get("shipType")!=null&&!pageUtils.getMap().get("shipType").toString().trim().equals("")){ if(pageUtils.getMap().get("shipType").equals("1")){ where = where + " and ship_type =5 "; }else{ where = where + " and ship_type !=5 "; } } if(pageUtils.getMap().get("ishd")!=null&&!pageUtils.getMap().get("ishd").toString().trim().equals("")){ if(pageUtils.getMap().get("ishd").equals("1")){ where = where + " and img is not null "; }else{ where = where + " and img is null "; } } where += " order by arrive_date desc "; pageUtils.initialize(TMSOrderList.dao.paginate(pageUtils.getPageNo(), pageUtils.getPageSize(), "select * ", " from TMS_Order_List"+where+ pageUtils.getOrderBySql())); if(pageUtils.getList().size()==0&&pageUtils.getPageNo()!=1){ pageUtils.setPageNo(1); pageUtils.initialize(TMSOrderList.dao.paginate(pageUtils.getPageNo(), pageUtils.getPageSize(), "select * ", " from TMS_Order_List "+where+pageUtils.getOrderBySql())); } setAttr("page", pageUtils); setAttr("nowDate", nowDate); render("reportList.jsp"); }
在上面我們獲取到了表格信息(實體類和model我就不用寫了吧),這里傳到前台兩個變量,和一個需要返回的頁面,這里當前時間你們可能不需要,之后就是在頁面接收這個值:(這里因為連表查詢所以將page這個值拆開了兩次,如果不
需要鏈表,直接<c:foreach>循環輸出就好了,不需要<c:set>)
<input id="btnSubmit" class="btn btn-primary" type="button" value="導出" onclick="exportExcel()"/>
<table id="contentTable" class="table table-striped table-bordered table-condensed"> <thead> <tr> <th style="text-align: center;"><input id="checkAll" type="checkbox"/>序號</th> <th style="text-align: center;">發運客戶</th> <th style="text-align: center;">訂單號</th> <th style="text-align: center;">裝貨日期</th> <th style="text-align: center;">預計送達日期</th> <th style="text-align: center;">運輸類型</th> <th style="text-align: center;">裝貨地址</th> <th style="text-align: center;">裝貨聯系人</th> <th style="text-align: center;">裝貨聯系方式</th> <th style="text-align: center;">物料描述</th> <th style="text-align: center;">裝貨數量</th> <th style="text-align: center;">裝貨重量(KG)</th> <th style="text-align: center;">裝貨體積(M3)</th> <th style="text-align: center;">包裝規格</th> <th style="text-align: center;">到達省份</th> <th style="text-align: center;">到達城市</th> <th style="text-align: center;">收貨客戶</th> <th style="text-align: center;">收貨地址</th> <th style="text-align: center;">收貨聯系人</th> <th style="text-align: center;">收貨聯系方式</th> <th style="text-align: center;">單價</th> <th style="text-align: center;">應收運費</th> <th style="text-align: center;">附加費用</th> <th style="text-align: center;">合計運費</th> <th style="text-align: center;">承運商</th> <th style="text-align: center;">承運商聯系方式</th> <th style="text-align: center;">承運車輛</th> <th style="text-align: center;">駕駛員</th> <th style="text-align: center;">駕駛員聯系方式</th> <th style="text-align: center;">應付運費</th> <th style="text-align: center;">預付運費</th> <th style="text-align: center;">回付運費</th> <th style="text-align: center;">附加費用</th> <th style="text-align: center;">附加費單號</th> <th style="text-align: center;">合同單號</th> <th style="text-align: center;">備注</th> </tr> </thead> <tbody id="pBody"> <c:forEach items="${page.list}" var="item" varStatus="index"> <c:set value="${item.client}" var="client"></c:set> <c:set value="${item.vttkManual}" var="vttkManual"></c:set> <tr> <td> <input id="checkbox${item.id}" name="checkbox" type="checkbox" value="${item.id}" onchange="selected(this.value)"/> ${index.index+1}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.customername}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.orderno}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentdate}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.arrivedate}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shiptype}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentaddress}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentlinkman}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmenttelephone}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.materialdescription}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentqty}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentweight}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentbulk}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.packagesize}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.province}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.city}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${client.clientName}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${client.clientAddress}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${client.linkman}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${client.telphone}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.unitprice}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.receivablecharge}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.additionalcharge}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.totalcharge}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.name1}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"></td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.signi}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"></td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"></td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.receivablefreight}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.advancefreight}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.backfreight}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.additionalfreight}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.additionalnumber}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.contract}</td> <td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.bezei}</td> </tr> </c:forEach> </tbody> </table>
之后就是重點了,excel表格導出,我們可以看到這個頁面的最頂端有個導出按鈕,導出按鈕監聽了點擊事件onclick="exportExcel()",之后我們可以在頁面的上方寫JavaScript寫一個方法:
function exportExcel (){ $("#searchForm").attr("action","${ctx}/tms/report/exportExcel").submit(); }
這里寫的這個方法是跳轉到tms下面report這個controller下的exportExcel方法,這個方法是這樣寫的:
/** * 導出excel */ public void exportExcel(){ // List<VttkManual> list=VttkManual.dao.find("select * from L2C_VTTK"); List<TMSOrderList> list = TMSOrderList.dao.find("select * from TMS_Order_List"); //Excel生成工具 ExcelExport excelExport = new ExcelExport(); String[] listName = {"發運客戶"," 訂單編號 "," 裝貨日期","預計送達日期 ","運輸類型 ","裝貨地址","裝貨聯系人", "裝貨聯系方式","物料描述 ","裝貨數量","裝貨重量 (KG)","裝貨體積(m3) ","包裝規格", "到達省份","到達城市 ","收貨客戶","收貨地址","收貨聯系人 ","收貨聯系方式", "單價","應收運費","附加費用","合計運費","承運商","承運商聯系方式 ","承運車輛", "駕駛員 ","駕駛員聯系方式 ","應付運費","預付運費","回付運費 ","附加費用","附加費單號 ", "合同單號","備注"}; Map<Integer, Object> excelMap = excelExport.createTable("列表", listName); HSSFCellStyle style = ((HSSFWorkbook) excelMap.get(3)).createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFRow row = null; HSSFCell cells = null; for (int i = 0; i < list.size(); i++){ row = ((HSSFSheet)excelMap.get(2)).createRow(i + 1); // VttkManual vm = list.get(i); // TMSOrderList order=TMSOrderList.dao.findFirst("select * from TMS_Order_List " + // "where order_no='"+vm.getVbeln()+"'"); TMSOrderList order = list.get(i); VttkManual vm = VttkManual.dao.findFirst("select * from L2C_VTTK where Vbeln='"+order.getOrderno()+"'"); cells = row.createCell(0); cells.setCellValue(order.getCustomername()); cells.setCellStyle(style); cells = row.createCell(1); cells.setCellValue(order.getOrderno()); cells.setCellStyle(style); cells = row.createCell(2); cells.setCellValue(order.getShipmentdate()); cells.setCellStyle(style); cells = row.createCell(3); cells.setCellValue(order.getArrivedate()); cells.setCellStyle(style); cells = row.createCell(4); cells.setCellValue(order.getShipmenttype()); cells.setCellStyle(style); cells = row.createCell(5); cells.setCellValue(order.getShipmentaddress()); cells.setCellStyle(style); cells = row.createCell(6); cells.setCellValue(order.getShipmentlinkman()); cells.setCellStyle(style); cells = row.createCell(7); cells.setCellValue(order.getShipmenttelephone()); cells.setCellStyle(style); cells = row.createCell(8); cells.setCellValue(order.getMaterialdescription()); cells.setCellStyle(style); cells = row.createCell(9); cells.setCellValue(order.getShipmentqty()==null?"":order.getShipmentqty().toString()); cells.setCellStyle(style); cells = row.createCell(10); cells.setCellValue(order.getShipmentweight()==null?"":order.getShipmentweight().toString()); cells.setCellStyle(style); cells = row.createCell(11); cells.setCellValue(order.getShipmentbulk()==null?"":order.getShipmentbulk().toString()); cells.setCellStyle(style); cells = row.createCell(12); cells.setCellValue(order.getPackagesize()); cells.setCellStyle(style); cells = row.createCell(13); cells.setCellValue(order.getProvince()); cells.setCellStyle(style); cells = row.createCell(14); cells.setCellValue(order.getCity()); cells.setCellStyle(style); if(order.getClient()!=null){ cells = row.createCell(15); cells.setCellValue(order.getClient().getClientName()); cells.setCellStyle(style); cells = row.createCell(16); cells.setCellValue(order.getClient().getClientAddress()); cells.setCellStyle(style); cells = row.createCell(17); cells.setCellValue(order.getClient().getLinkman()); cells.setCellStyle(style); cells = row.createCell(18); cells.setCellValue(order.getClient().getTelphone()); cells.setCellStyle(style); } cells = row.createCell(19); cells.setCellValue(order.getUnitprice()==null?"":order.getUnitprice().toString()); // cells.setCellValue(""); cells.setCellStyle(style); cells = row.createCell(20); cells.setCellValue(order.getReceivablecharge()==null?"":order.getReceivablecharge().toString()); // cells.setCellValue(""); cells.setCellStyle(style); cells = row.createCell(21); cells.setCellValue(order.getAdditionalcharge()==null?"":order.getAdditionalcharge().toString()); // cells.setCellValue(""); cells.setCellStyle(style); cells = row.createCell(22); cells.setCellValue(order.getTotalcharge()==null?"":order.getTotalcharge().toString()); // cells.setCellValue(""); cells.setCellStyle(style); cells = row.createCell(23); cells.setCellValue(vm.getName1()); cells.setCellStyle(style); cells = row.createCell(24); cells.setCellValue(vm.getTelephone1()); cells.setCellStyle(style); cells = row.createCell(25); cells.setCellValue(vm.getSigni()); cells.setCellStyle(style); cells = row.createCell(26); cells.setCellValue(vm.getDriver()); cells.setCellStyle(style); cells = row.createCell(27); cells.setCellValue(vm.getTelephone2()); cells.setCellStyle(style); cells = row.createCell(28); cells.setCellValue(vm.getReceivablefreight()); cells.setCellStyle(style); cells = row.createCell(29); cells.setCellValue(vm.getAdvancefreight()); cells.setCellStyle(style); cells = row.createCell(30); cells.setCellValue(vm.getBackfreight()); cells.setCellStyle(style); cells = row.createCell(31); cells.setCellValue(vm.getAdditionalfreight()); cells.setCellStyle(style); cells = row.createCell(32); cells.setCellValue(vm.getAdditionalnumber()); cells.setCellStyle(style); cells = row.createCell(33); cells.setCellValue(vm.getContract()); cells.setCellStyle(style); cells = row.createCell(34); cells.setCellValue(vm.getBezei()); cells.setCellStyle(style); } // 第六步,將文件存到指定位置 try{ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss"); String code = sdf.format(new Date()); String fileName = "訂單全部明細"+code+".xls"; String path = JFinal.me().getConstants().getFileRenderPath();//獲取項目配置下載路徑 path = path.replace("\\","/"); FileOutputStream fout = new FileOutputStream(path+"/"+fileName); ((HSSFWorkbook)excelMap.get(3)).write(fout); fout.close(); addMessageSuccess("導出成功"); renderFile(fileName); }catch (Exception e){ e.printStackTrace(); addMessageError("導出失敗"); redirect("/tms/report"); } }
之后大概就都懂了把,最上面String[] listName 是每列的字段名,下面
cells = row.createCell(0);代表的是當前列數;
cells.setCellValue(order.getCustomername());代表的是當前列插入的內容;
cells.setCellStyle(style);代表當前單元格內的樣式
下面配置好路徑就可以道出了,導出成功之后頁面會提示你保存的路徑,保存之后就可以查看了,是不是很簡單呢