java 生成excel表傳給前端下載
阿新 • • 發佈:2019-01-03
js部分:
java端:var jsonstr=getSearchSel(os); var str = "";//傳引數 for(var i in jsonstr){ if(jsonstr[i]!=""){ str += i+"="+jsonstr[i]+"&"; } } str = str.substr(0, str.length - 1); var url = "/QAdevices/ExcelServlet?" + str; window.open(url);
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { StringBuilder jsonstr = new StringBuilder();//轉化成json jsonstr.append("{"); String reqstr = request.getQueryString();//獲取請求引數 if(!"".equals(reqstr) &&reqstr !=null){ String [] stringArr= reqstr.split("&"); for(int i = 0;i<stringArr.length;i++){ String [] strArr = stringArr[i].split("="); jsonstr.append("\""+strArr[0]+"\":"+"\""+strArr[1]+"\""+",") ; } jsonstr.deleteCharAt(jsonstr.length()-1); } jsonstr.append("}"); String filename =getExcelPath() ; List<Map<String, Object>> searchlist = ServerInit.managerAction.searchDeviceInfoByConditions3(jsonstr.toString()); if(searchlist!=null){ HSSFWorkbook wb = createExcel(searchlist); try{ //寫入瀏覽器 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); OutputStream outputStream=response.getOutputStream(); wb.write(outputStream); outputStream.flush(); outputStream.close(); }catch(Exception e){ logger.error("Exception",e); } } }
/** * 返回excel表生成的路徑 * 引數 * os,devicestatue,peopleId * @return excel表的伺服器儲存地址 * */ public String getExcelPath() { //String str = this.getServletContext().getRealPath("/WEB-INF"); List<Map<String, Object>> searchlist = null; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設定日期格式 String timestr = df.format(new Date()).toString().replace(" ", "-"); timestr = timestr.replace(":",""); String filename = "裝置一覽表-"+ timestr+".xls"; logger.info("下載:"+filename); return filename; } /** * 建立excel表 * 引數 * 資料庫查詢資料list,儲存路徑 * @return boolean true:生成成功 false: 生成失敗 * @throws * */ public HSSFWorkbook createExcel(List<Map<String, Object>> list) { boolean bool = true; // 第一步,建立一個webbook,對應一個Excel檔案 //XSSFWorkbook wb = new XSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中新增一個sheet,對應Excel檔案中的sheet HSSFSheet sheet = wb.createSheet("裝置資訊"); // 第三步,在sheet中新增表頭第0行,注意老版本poi對Excel的行數列數有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,建立單元格,並設定值表頭 設定表頭居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個居中格式 ArrayList<String> title = new ArrayList<>(Arrays.asList("編號","裝置編號","品牌名稱","品牌","系統","系統版本","cpu","cpu型號","記憶體大小","螢幕尺寸","解析度高","解析度寬","imei1","imei2","sn","是否越獄","顏色","前置攝像頭","後置攝像頭","資料線","充電器","耳機","電池","手機殼","入庫時間","裝置管理員","借用人","狀態","備註")); // System.out.println(title.size()); HSSFCell cell; for(int i = 0 ;i<title.size();i++){ cell = row.createCell((short) i); cell.setCellValue(title.get(i)); } HSSFRow row1 = sheet.createRow((int) 1); ArrayList<String> title2 = new ArrayList<>(Arrays.asList("必填","必填","必填","必填","android/ios","必填","廠商","廠商+型號","","","","","必填","","","0=否,1=是","","單位萬","單位萬","0=否,1=是","0=否,1=是","0=否,1=是","0=否,1=是","0=否,1=是","","","","狀態(0完好 1破損 2老舊 3待修復 4已壞)","")); // System.out.println(title2.size()); for(int i = 0 ;i<title2.size();i++){ cell = row1.createCell((short) i); cell.setCellValue(title2.get(i)); } HSSFRow row2; HSSFCell cell2; int i = 0; int j = 0; for (Map<String, Object> map : list) {//填充資料 row2 = sheet.createRow((int) i+2); List<String> datalist = getData(map); for( j = 0 ;j<datalist.size();j++){ cell2 = row2.createCell((short) j); cell2.setCellValue(datalist.get(j)); } i++; } return wb; } /** * 處理資料庫查詢出的資料,按照excel 列的順序 * 引數 * Map<String , Object>一行資料 * @return datalist * */ public List<String> getData(Map<String, Object> map){ List<String> stringList = new ArrayList<String>(); for(Map.Entry<String,Object> entry : map.entrySet()){ if(entry.getValue() == null){ entry.setValue(""); } if(entry.getValue() instanceof Boolean){//false:0 true:1 if((Boolean)entry.getValue()){ entry.setValue(1); } else { entry.setValue(0); } } } stringList.add(map.get("deviceid").toString()); stringList.add(map.get("tag").toString()); stringList.add(map.get("model").toString()); stringList.add(map.get("brand").toString()); stringList.add(map.get("os").toString()); stringList.add(map.get("osversion").toString()); stringList.add(map.get("cpu").toString()); stringList.add(map.get("cpumodel").toString()); stringList.add(map.get("mem").toString()); stringList.add(map.get("screensize").toString()); stringList.add(map.get("resolution_high").toString()); stringList.add(map.get("resolution_width").toString()); stringList.add(map.get("imei1").toString()); stringList.add(map.get("imei2").toString()); stringList.add(map.get("sn").toString()); stringList.add(map.get("root").toString()); stringList.add(map.get("color").toString()); stringList.add(map.get("frontcam").toString()); stringList.add(map.get("backcam").toString()); stringList.add(map.get("usbcable").toString()); stringList.add(map.get("charger").toString()); stringList.add(map.get("headset").toString()); stringList.add(map.get("battery").toString()); stringList.add(map.get("phoneshell").toString()); stringList.add(map.get("adddate").toString()); stringList.add(map.get("managername").toString()); stringList.add(map.get("name").toString()); stringList.add(map.get("devicestatus").toString()); stringList.add(map.get("devicenotes").toString()); return stringList; } }