java的导出Excel表格
2017-10-09 11:05
405 查看
导出Excel表格
public void export(HttpServletRequest request, HttpServletResponse response){ try { Integer totalRow1 = 0,totalRow2 = 0,totalRow3 = 0,totalRow4= 0; List<String> deptName_list= new ArrayList<String>(); List<String> ylist=new ArrayList<String>(); List<String> tlist=new ArrayList<String>(); List<String> zlist=new ArrayList<String>(); List<String> yblist=new ArrayList<String>(); System.out.println(list.size()); for(Map<String,String> map:list){ String deptName=map.get("deptname"); deptNamelist.add(deptName); String total=map.get("total"); ylist.add(total); totalRow1 += Integer.parseInt(map.get("total")); String tg=map.get("tg"); tlist.add(tg); totalRow2 += Integer.parseInt(map.get("tg")); String zbl=map.get("zbl"); zlist.add(zbl); totalRow3 += Integer.parseInt(map.get("zbl")); String ybl=map.get("ybj"); yblist.add(ybl); totalRow4 += Integer.parseInt(map.get("ybj")); } deptName_list.add("总计"); yc_list.add(totalRow1.toString()); tg_list.add(totalRow2.toString()); zbl_list.add(totalRow3.toString()); ybj_list.add(totalRow4.toString()); String startTime=request.getParameter("startTime"); String endTime=request.getParameter("endTime"); String start_time=""; if(startTime!=null&&startTime.contains("-")){ start_time=startTime.replace("-", ""); } String end_time=""; if(endTime!=null&&endTime.contains("-")){ end_time=endTime.replace("-", ""); } if(deptName_list!=null&&!deptName_list.isEmpty()){ response.setContentType("application/ms-excel;charset=GB2312"); response.setHeader("Content-Disposition", "attachment; filename="+start_time+"-"+end_time+new String("理详情".getBytes("gb2312"), "ISO-8859-1")+".xls"); ServletOutputStream os=response.getOutputStream(); String[] titleContent={"部门名称","常","过","理","结"}; //创建工作薄 WritableWorkbook workbook = Workbook.createWorkbook(os); //创建新的一页 WritableSheet sheet = workbook.createSheet("First Sheet", 0); //构造标题 sheet.mergeCells(0, 0, titleContent.length-1, 0);//添加合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行 WritableFont bold = new WritableFont(WritableFont.ARIAL,16,WritableFont.BOLD);//设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示 WritableCellFormat titleFormate = new WritableCellFormat(bold);//生成一个单元格样式控制对象 titleFormate.setAlignment(jxl.format.Alignment.CENTRE);//单元格中的内容水平方向居中 titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//单元格的内容垂直方向居中BLACK titleFormate.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.RED); Label title = new Label(0,0,"督查处理详情",titleFormate); sheet.setRowView(0, 800, false);//设置第一行的高度 sheet.addCell(title); //构造表头 for (int i = 0; i < titleContent.length; i++) { WritableFont titleContentBlod = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD);//设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示 WritableCellFormat titleContentFormate = new WritableCellFormat(titleContentBlod);//生成一个单元格样式控制对象 titleContentFormate.setAlignment(jxl.format.Alignment.CENTRE);//单元格中的内容水平方向居中 titleContentFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//单元格的内容垂直方向居中 titleContentFormate.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.RED); Label lable = new Label(i,1,titleContent[i],titleContentFormate); sheet.addCell(lable); } WritableFont content = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD);//设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示 WritableCellFormat contentFormate = new WritableCellFormat(content); contentFormate.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);//添加边框 for(int i=0;i<deptName_list.size();i++){ Label deptNameLable = new Label(0,i+2,deptName_list.get(i),contentFormate); sheet.addCell(deptNameLable); Label ycNumLable = new Label(1,i+2,yc_list.get(i),contentFormate); sheet.addCell(ycNumLable); Label tgNumLable = new Label(2,i+2,tg_list.get(i),contentFormate); sheet.addCell(tgNumLable); Label zblNumLable = new Label(3,i+2,zbl_list.get(i),contentFormate); sheet.addCell(zblNumLable); Label ybjNumLable = new Label(4,i+2,ybj_list.get(i),contentFormate); sheet.addCell(ybjNumLable); } workbook.write(); workbook.close(); os.close(); } }catch(Exception e){ e.printStackTrace(); } }
相关文章推荐
- 用java将Excel表格导入导出数据库
- java导出数据库中Excel表格数据的方法
- Java web查出的数据导出为Excel的表格
- JAVA利用FreeMarker生成(导出)Excel表格
- java中使用poi导出excel表格数据并且可以手动修改导出路径
- java如何将网页表格导出为excel
- Java表格数据导出EXCEL 并从服务器下载实例
- JAVA 导出 excel 表格
- java 将 利用poi 数据库 内容 导出到excel 表格
- Java中Excel表格数据的导入和导出步骤和方法
- java POI实现excel实现表格导出
- java poi导出excel 表格
- Java读取并导出Word中的表格(Excel),导出文件为Excel
- [ExtJS5学习笔记]第三十四节 sencha extjs 5 grid表格之java后台导出excel
- 导出excel表格到本地服务器, 远程下载 Java
- java 导出一行多列的excel表格
- [ExtJS5学习笔记]第三十四节 sencha extjs 5 grid表格之java后台导出excel
- Java 代码 datagrid导出表格为excel的路径为绝对路径(本地电脑)
- [ExtJS5学习笔记]第三十四节 sencha extjs 5 grid表格之java后台导出excel
- JAVA生成并导入导出Excel表格文件