java中poi进行execl导出
首先贴出最终导出的execl截图吧:
前台界面如下:
点击导出时,为其按钮的onclick事件添加exportDate()函数:
function exportDate(){ var begin_Date=$('#dateid').datebox('getValue'); document.getElementById("exportForm").action="../mobileManage/export.do?begin_Date="+begin_Date; document.getElementById("exportForm").submit(); }
在后台export.do的代码如下:
@RequestMapping("export.do") public void exportExecl(HttpServletRequest request, HttpServletResponse response) { String beginDate = request.getParameter("dateid")+"-01"; int maxDay=getCurrMonthDays(beginDate); List<MobileManage> list = getDataList(beginDate); String title =beginDate.substring(0, 4)+"年"+beginDate.subSequence(5, 7)+"月"+ "员工工作详情表"; String[] headers1 = new String[33];// 表头1数组 日期,1,2,3... String[] headers2 = new String[31];// 表头2数组 星期,一二三... headers1[0]="类型"; headers1[1]="姓名"; for (int i = 1; i <=maxDay; i++) { String date=beginDate.substring(0,8)+(i<10?"0"+i:i+""); String week=getWeek(date); headers1[i+1]=i+""; headers2[i-1]=week; } HSSFWorkbook workbook = new HSSFWorkbook();// 生成一个工作簿 HSSFSheet sheet = workbook.createSheet(title);//创建一个表 HSSFCellStyle style = workbook.createCellStyle();//创建一个居中格式 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直 sheet.setDefaultColumnWidth(5);//设置默认列宽 sheet.setColumnWidth(0, 10* 256);//设置第一、第二列列宽 sheet.setColumnWidth(1, 10 * 256); sheet.setDefaultRowHeight((short)300);//设置默认行高 HSSFRow row = sheet.createRow(0);//创建表头一 sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0)); //设置跨行 sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1)); HSSFRow rowWeek=sheet.createRow(1);//创建表头二 HSSFCell cell = null; //给表头填充数据 for (int i = 0; i < headers1.length; i++) { cell = row.createCell(i); cell.setCellValue(headers1[i]); cell.setCellStyle(style); } for (int i = 0; i < headers2.length; i++) { cell=rowWeek.createCell(i+2); cell.setCellValue(headers2[i]); cell.setCellStyle(style); } // 遍历集合数据,产生数据行 MobileManage manage = null; int index = 1; // 循环写入行 for (int i = 0; i < list.size(); i++) { index++; row = sheet.createRow(index);//从第二行开始填充数据(第零和一行是表头) manage = list.get(i); cell = row.createCell(0);// 类型 cell.setCellValue(manage.getType()); cell = row.createCell(1);//姓名 cell.setCellValue(manage.getName()); for (int j = 0; j < headers2.length; j++) {//项目编码 cell=row.createCell(j+2); cell.setCellValue(manage.getDateCodes()[j]==null?"":manage.getDateCodes()[j].getCode()); } } response.reset(); try { title=new String(title.getBytes("gb2312"),"iso8859-1");//设置表格名时中文乱码,进行转码 } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xls"); try { OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } return; }
我在网上看到很多人说这里面涉及到设置的一般都要加个short强转,但是我这样写在火狐谷歌导出并没有什么问题,当然也许不同浏览器等或许会出现问题,以后要是遇到了再说吧.
生来奔走万山中,踏尽崎岖路自通