excel输出

来源:互联网 发布:电脑网络连接出现红叉 编辑:程序博客网 时间:2024/05/01 03:02

参考:

http://boonya.iteye.com/blog/1768376

http://www.iteye.com/problems/64614


jsp页面-此form只用于excel输出:

 <form id="form1" name="form1" method="post"  accept-charset="GBK" onsubmit="if(!!window.ActiveXObject){document.charset='GBK';}"  ></form> var url='/mzjgl/exportExcel.action?rygs='+gs+'&ssjd='+ssjd+'&rybh='+number+'&name='+name; $("#form1").attr("action",url); $("#form1").submit(); 



或者:

  jQuery("form")//选择form     .first()//选择第一个 第二个用eq(1) 最后一个 last()     .attr("action",'/mzjgl/exportExcel.action?rygs='+gs+'&ssjd='+ssjd+'&rybh='+number+'&name='+name)//更改属性     .submit();//提交 


或者:

 form1.action='/mzjgl/exportExcel.action?rygs='+gs+'&ssjd='+ssjd+'&rybh='+number+'&name='+name, form1.submit();


action后台代码:

response.setContentType("applicationnd.ms-excel;");//response.setHeader("Content-Disposition", "attachment;filename=" + templateName + ".xls");



详细:
// 弹出式导出Excel文件public void exportExcel() {HttpServletResponse response = this.getResponse();//response.setContentType("applicationnd.ms-excel; charset=UTF-8");response.setContentType("applicationnd.ms-excel;");SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");Date date = new Date();String fileName = sdf.format(date);response.setHeader("Content-Disposition", "attachment;filename="+ fileName + ".xls");// ******************************************集合String sort = this.getRequest().getParameter("sort");String order = this.getRequest().getParameter("order");String rygs = this.getRequest().getParameter("rygs");String ssjd = this.getRequest().getParameter("ssjd");String rybh = this.getRequest().getParameter("rybh");String name = this.getRequest().getParameter("name");// 查询条件StringBuffer contions = new StringBuffer();if (null != rygs && !"".equals(rygs)) {contions.append(" and ").append(" CZ_RYGS='").append(rygs).append("' ");}if (null != ssjd && !"".equals(ssjd)) {contions.append(" and ").append(" CZ_SSJD='").append(ssjd).append("' ");}if (null != rybh && !"".equals(rybh)) {contions.append(" and ").append(" CZ_NUMBER = '").append(rybh).append("' ");}if (null != name && !"".equals(name)) {contions.append(" and ").append(" CZ_NAME like '%").append(name).append("%' ");}Map map = new HashMap();if (sort == null || order == null) {map.put("sort", "rygs");map.put("order", "asc");} else {map.put("sort", sort);map.put("order", order);}map.put("contions", contions.toString());CaptialService capitalService = new CaptialService();long time1 = new Date().getTime();System.out.println("开始时间:"+new Date());//写入Excel的数据listList<CzzjInfo> list = capitalService.findAll(map);//List<CzzjInfo> list=new ArrayList<CzzjInfo>();System.out.println("结束时间:"+new Date());//数据列名String[] headers = { "序号", "人员归属", "所属街道", "编号", "姓名", "商保(元)","商保补差(元)", "区补(元)", "生补(元)", "开户银行", "卡号" };//写入Exceltry {HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("sheet_1");sheet.setDefaultColumnWidth((short) 15);//设置格式int iFontSize = 12;String sFontname = "宋体";HSSFFont Headfont = wb.createFont();Headfont.setFontHeightInPoints((short) iFontSize); // 表头字体大小Headfont.setFontName(sFontname); // 表头字体名称Headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 表头字体加粗HSSFCellStyle HeadStyle = wb.createCellStyle(); // 表头格式HeadStyle.setFont(Headfont); // 表头字体HeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 表头位置HSSFFont bodyfont = wb.createFont();bodyfont.setFontHeightInPoints((short) 10); // 字体大小bodyfont.setFontName(sFontname); // 字体名称bodyfont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 字体加粗HSSFCellStyle bodyStyle = wb.createCellStyle(); // 格式bodyStyle.setFont(bodyfont); // 字体bodyStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 位置int i = 0;while (i <= list.size()) {HSSFRow row = sheet.createRow(i);if (i == 0) {for (int j = 0; j < headers.length; j++) {HSSFCell cell = row.createCell((short) j);cell.setCellStyle(HeadStyle);cell.setCellValue(headers[j]);}} else {CzzjInfo cz = list.get(i-1);HSSFCell cell = row.createCell((short) 0);cell.setCellStyle(bodyStyle);cell.setCellValue(i);cell = row.createCell((short) 1);cell.setCellValue(cz.getRygs());cell = row.createCell((short) 2);cell.setCellValue(cz.getSsjd());cell = row.createCell((short) 3);cell.setCellValue(cz.getBh());cell = row.createCell((short) 4);cell.setCellValue(cz.getXm());cell = row.createCell((short) 5);cell.setCellValue(cz.getSbs());cell = row.createCell((short) 6);cell.setCellValue(cz.getSbbcs());cell = row.createCell((short) 7);cell.setCellValue(cz.getQbs());cell = row.createCell((short) 8);cell.setCellValue(cz.getSbs());cell = row.createCell((short) 9);cell.setCellValue(cz.getKhyh());cell = row.createCell((short) 10);cell.setCellValue(cz.getKh());}i++;}java.io.OutputStream out = response.getOutputStream();wb.write(out);out.close();} catch (FileNotFoundException e1) {e1.printStackTrace();} catch (IOException e) {e.printStackTrace();}}







0 0
原创粉丝点击