国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

SpringMVC的Excel導(dǎo)出(彈框提示下載)

2019-11-10 19:17:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

重要的事情說(shuō)三遍?。。。?/p>

jQuery的Ajax函數(shù)、及ajaxSubmit等函數(shù)的返回類型(dataType)只有xml、text、json、html等類型,沒(méi)有“流”類型.

jQuery的ajax函數(shù)、及ajaxSubmit等函數(shù)的返回類型(dataType)只有xml、text、json、html等類型,沒(méi)有“流”類型.

jQuery的ajax函數(shù)、及ajaxSubmit等函數(shù)的返回類型(dataType)只有xml、text、json、html等類型,沒(méi)有“流”類型.

所以想要彈框下載,就不能用Ajax來(lái)做?。。?!

jsp

<span class="btn" onclick="Excel_export()">EXCEL導(dǎo)出</span> <div style="display: none;"> <form action= "login/excel_download.action" method="get"> <input type="text" id="startime" name="startime"> <input type="text" id="stoptime" name="stoptime"> <input type="text" id="mintemp" name="mintemp"> <input type="text" id="maxtemp" name="maxtemp"> <input type="text" id="winds" name="winds"> <input type="text" id="id" name="id"> <input type="submit" id="sub" value="Submit" />         </form></div>

JS

function excel_export(){ $("#startime").val(begaintime); $("#stoptime").val(endtime); $("#mintemp").val(wind_sp); $("#maxtemp").val(min_temp); $("#winds").val(max_temp); $("#id").val(a1); $("#sub").click();}

后臺(tái):

@RequestMapping(value = "excel_download") @ResponseBody public void  excel_download(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("utf-8"); resp.setHeader("Cache-Control", "no-cache"); String fileName = "excel文件"; String startime = req.getParameter("startime"); String stoptime = req.getParameter("stoptime"); String mintemp = req.getParameter("mintemp"); String maxtemp = req.getParameter("maxtemp"); String winds = req.getParameter("winds"); String id = req.getParameter("id"); System.out.PRintln(startime+","+stoptime+","+mintemp+","+maxtemp+"<"+winds+","+id); Map<String, Object> param=new HashMap<String, Object>();  param.put("id", id.substring(1, id.length())); param.put("startime", startime); param.put("stoptime", stoptime); param.put("mintemp", mintemp); param.put("maxtemp", maxtemp); param.put("winds", winds); List<RecordInfo> r = recordService.GetRecordByCondition_excel(param); System.out.println(r); // PoiExpExcel.main(r); String[] title = {"序號(hào)","風(fēng)速","記錄時(shí)間","溫度"}; HSSFWorkbook workbook = new HSSFWorkbook(); //創(chuàng)建一個(gè)工作表sheet HSSFSheet sheet = workbook.createSheet(); //創(chuàng)建第一行 HSSFRow row = sheet.createRow(0); HSSFCell cell = null; //插入第一行數(shù)據(jù)title for(int i= 0; i < title.length; i++){ cell = row.createCell(i); cell.setCellValue(title[i]); } //追加數(shù)據(jù) for(int i= 1; i <= r.size(); i++){ HSSFRow nextrow = sheet.createRow(i); HSSFCell cell2 = nextrow.createCell(0); cell2.setCellValue(i); cell2 = nextrow.createCell(1); cell2.setCellValue(r.get(i-1).getRecord_wind_speed()); cell2 = nextrow.createCell(2); cell2.setCellValue(r.get(i-1).getRecord_time()); cell2 = nextrow.createCell(3); cell2.setCellValue(r.get(i-1).getRecord_temp()); } //創(chuàng)建一個(gè)文件 /*File file = new File("C:/Users/Administrator/Desktop/poi_test.xls"); try { file.createNewFile(); //將EXCEL內(nèi)容存盤(pán) FileOutputStream stream = FileUtils.openOutputStream(file); workbook.write(stream); stream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ ByteArrayOutputStream os = new ByteArrayOutputStream();        try {         workbook.write(os);        } catch (IOException e) {            e.printStackTrace();        }        byte[] content = os.toByteArray();        InputStream is = new ByteArrayInputStream(content);        // 設(shè)置response參數(shù),可以打開(kāi)下載頁(yè)面        resp.reset();        resp.setContentType("application/vnd.ms-excel;charset=utf-8");        resp.setHeader("Content-Disposition", "attachment; filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));        ServletOutputStream out = resp.getOutputStream();        BufferedInputStream bis = null;        BufferedOutputStream bos = null;        try {            bis = new BufferedInputStream(is);            bos = new BufferedOutputStream(out);            byte[] buff = new byte[2048];            int bytesRead;            // Simple read/write loop.            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {                bos.write(buff, 0, bytesRead);            }        } catch (final IOException e) {            throw e;        } finally {            if (bis != null)                bis.close();            if (bos != null)                bos.close();        } }
上一篇:HashMap原理詳講

下一篇:Golang API 類型 Get

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 咸丰县| 凤阳县| 巴东县| 石狮市| 肇东市| 漳州市| 隆尧县| 屯门区| 柘荣县| 佛冈县| 日土县| 区。| 法库县| 濮阳县| 余姚市| 九龙城区| 龙川县| 公主岭市| 怀来县| 盐津县| 柳林县| 隆安县| 五峰| 博野县| 侯马市| 防城港市| 苗栗市| 株洲县| 长汀县| 五河县| 大埔县| 黄平县| 长白| 左贡县| 高唐县| 庆元县| 开江县| 大兴区| 常熟市| 新乡县| 永安市|