java 生成excel表传给前端下载


js部分:

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);

java端:
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> 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 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 list) {boolean bool = true; // 第一步,创建一个webbook,对应一个Excel文件 //XSSFWorkbookwb = 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 title = new ArrayList<>(Arrays.asList("编号","设备编号","品牌名称","品牌","系统","系统版本","cpu","cpu型号","内存大小","屏幕尺寸","分辨率高","分辨率宽","imei1","imei2","sn","是否越狱","颜色","前置摄像头","后置摄像头","数据线","充电器","耳机","电池","手机壳","入库时间","设备管理员","借用人","状态","备注")); //System.out.println(title.size()); HSSFCell cell; for(int i = 0 ; i(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


【java 生成excel表传给前端下载】






    推荐阅读