`
dengzhangtao
  • 浏览: 668035 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

jxl操作复杂excel表格

阅读更多
上一篇,我写了jxl的理论的东西比较多,  是为了让我自己学习,更好让我去完成项目中的一個模块, 就是在ext的gridpanel显示出的数据显示出來,  因为导出的数据是两个gridpanel的数据所以这就算比较复杂的导出了, 单单使用ext自带的excel导出,就难以做到啦。

  简单的说明之后, 我还是把代码贴吧。一整个controller方法
	@SuppressWarnings("unchecked")
	@RequestMapping("/pages/deliveryPoint/deliveryPointSearch/exportExcel.page")
    public String exportExcel(HttpServletRequest request, HttpServletResponse response){
		 Map condition = new HashMap();
		
		 String regieOrgCode = request.getParameter("regieOrgCode_export");
		 String regieDeptCode = request.getParameter("regieDeptCode_export");
		 
		 condition.put("custName", request.getParameter("custName_export"));
		 condition.put("regieOrgCode", regieOrgCode);
		 condition.put("regieDeptCode", regieDeptCode);
		 condition.put("grantDate1", request.getParameter("grantDate1_export"));
		 condition.put("grantDate2", request.getParameter("grantDate2_export"));
		 
		 
		 String regieOrgName = "       烟草专卖局"; 
		 if(StringUtils.isNotBlank(regieOrgCode)){
			 RmRegieOrg   obj = rmRegieOrgService.getRmRegieOrg(regieOrgCode);
			 regieOrgName = obj.getRegieOrgName();
		 }
		 
		 String regieDeptName = "       专管所"; 
		 if(StringUtils.isNotBlank(regieDeptCode)){
			 RmRegieDept   obj = rmRegieDeptService.getRmRegieDept(regieDeptCode);
			 regieDeptName = obj.getRegieDeptName();
		 }
		 
		 
		 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		
         List<Map> custMap = rmDeliveryPointApplyDetailService.findCustMapForExport(condition);
        
		 
		 response.setHeader("Content-disposition", "attachment;filename=text.xls");
		 response.setContentType("application/msexcel");
		  try {
		   OutputStream os = response.getOutputStream();
		   jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
		   
		   jxl.write.WritableSheet ws = wwb.createSheet("定点取货点寄货户实时更新汇总表", 0);
		   
		   WritableFont titleFont = new WritableFont(WritableFont.createFont("宋体"), 15,WritableFont.BOLD);
		   WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
		   titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
		   titleFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
		   titleFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.NONE);
		   
		   WritableFont titleFont1 = new WritableFont(WritableFont.createFont("宋体"), 11,WritableFont.NO_BOLD);
		   WritableCellFormat titltFormat1 = new WritableCellFormat(titleFont1);
		   titltFormat1.setAlignment(jxl.format.Alignment.LEFT);
		   titltFormat1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
		   titltFormat1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.NONE);
		   
		   WritableFont cellFont = new WritableFont(WritableFont.createFont("宋体"), 9,WritableFont.NO_BOLD);
		   WritableCellFormat cellFormat = new WritableCellFormat(cellFont);
		   cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
		   cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
		   cellFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
		   cellFormat.setWrap(true);
		   
		   WritableFont headFont = new WritableFont(WritableFont.createFont("宋体"), 9,WritableFont.BOLD);
		   WritableCellFormat headFormat = new WritableCellFormat(headFont);
		   headFormat.setAlignment(jxl.format.Alignment.CENTRE);
		   headFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
		   headFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
		   headFormat.setWrap(true);
		   
		   
		 
		   
		   int rowIndex = 0 ; //Excel 行索引
		   jxl.write.Label labelC = new jxl.write.Label(0, 0, "定点取货点寄货户实时更新汇总表",titleFormat);
		   ws.addCell(labelC);
		   ws.mergeCells(0, rowIndex, 8, rowIndex);
		   ws.setRowView(rowIndex, 800);
		   
		    rowIndex = rowIndex + 1; // 下一行
			labelC = new jxl.write.Label(0, rowIndex, regieOrgName+"  "+regieDeptName, titltFormat1);
			ws.addCell(labelC);
			ws.mergeCells(0, rowIndex, 8, rowIndex);
			ws.setRowView(rowIndex, 600);
			
      	
		   for(int i=0;i<custMap.size();i++){
			     
			   rowIndex = rowIndex + 1; // 下一行
				labelC = new jxl.write.Label(0, rowIndex, "取货点姓名", headFormat);
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(1, rowIndex, "许可证号码", headFormat);
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(2, rowIndex, "经营地址", headFormat);
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(3, rowIndex, "序号", headFormat);
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(4, rowIndex, "寄货户姓名", headFormat);
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(5, rowIndex, "许可证号码", headFormat);
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(6, rowIndex, "经营地址", headFormat);
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(7, rowIndex, "设立时间", headFormat);
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(8, rowIndex, "取消时间", headFormat);
				ws.addCell(labelC);
				
				ws.setColumnView(0, 12);
				ws.setColumnView(1, 8);
				ws.setColumnView(2, 12);
				ws.setColumnView(3, 3);
				ws.setColumnView(4, 12);
				ws.setColumnView(5, 8);
				ws.setColumnView(6, 12);
				ws.setColumnView(7, 10);
				ws.setColumnView(8, 10);
				
				ws.setRowView(rowIndex, 800);
				rowIndex = rowIndex + 1; // 下一行
			   
	        	 Map map = custMap.get(i);
	        	 String custName = map.get("custName").toString();
	        	 String custLicenceCode= map.get("custLicenceCode").toString();
	        	 String custAddress= map.get("custAddress").toString();
	        	 
	        	 labelC = new jxl.write.Label(0, rowIndex, custName, cellFormat);
	 			 ws.addCell(labelC);
	 			 
	 			 labelC = new jxl.write.Label(1, rowIndex, custLicenceCode, cellFormat);
	 			 ws.addCell(labelC);
	 			 
	 			 labelC = new jxl.write.Label(2, rowIndex, custAddress, cellFormat);
	 			 ws.addCell(labelC);
	  		     
	        	 String custCode = map.containsKey("custCode")? map.get("custCode").toString():"";
	        	 List<Map> list = deliveryPointSearchService.getDeliveryPointAnchoredCustList(custCode);
	        	 
	        	 for(int j=0;j<list.size();j++){
	        		 
	        		 Map mapTemp = list.get(j);
	        		 labelC = new jxl.write.Label(3, rowIndex+j, String.valueOf(j+1), cellFormat);
		 			 ws.addCell(labelC);
		 			 
		 			 String anchoredCustName = mapTemp.get("anchoredCustName").toString();
		 			 String anchoredLicenceCode = mapTemp.get("anchoredLicenceCode").toString();
		 			 String anchoredCustAddress = mapTemp.get("anchoredCustAddress").toString();
		 			 Date setTime = (Date)mapTemp.get("setTime");
		 			 
		 			String setTimeString = "";
					if (setTime != null) {
						setTimeString = df.format(setTime);
					}
		 			 Date cancelTime = mapTemp.containsKey("cancelTime")?(Date)mapTemp.get("cancelTime"):null;
		 			String cancelTimeString = "";
					if (cancelTime != null) {
						cancelTimeString = df.format(cancelTime);
					}
		 			 labelC = new jxl.write.Label(4, rowIndex+j, anchoredCustName, cellFormat);
		 			 ws.addCell(labelC);
		 			 
		 			labelC = new jxl.write.Label(5, rowIndex+j, anchoredLicenceCode, cellFormat);
		 			 ws.addCell(labelC);
		 			 
		 			labelC = new jxl.write.Label(6, rowIndex+j, anchoredCustAddress, cellFormat);
		 			 ws.addCell(labelC);
		 			 
		 			labelC = new jxl.write.Label(7, rowIndex+j, setTimeString, cellFormat);
		 			 ws.addCell(labelC);
		 			 
		 			labelC = new jxl.write.Label(8, rowIndex+j, cancelTimeString, cellFormat);
		 			 ws.addCell(labelC);
		 			 
		 			 ws.setRowView(rowIndex+j, 800);
	        	 }
	        	 
	        	 if(list.size()>0){
	        		 int temp = list.size()-1;
		        	 ws.mergeCells(0, rowIndex, 0, rowIndex+temp);
		        	 ws.mergeCells(1, rowIndex, 1, rowIndex+temp);
		        	 ws.mergeCells(2, rowIndex, 2, rowIndex+temp);
		        	 rowIndex = rowIndex+temp;
	        	 }
	        	 
	        }
		    
		    wwb.write();
			 //关闭Excel工作薄对象
			wwb.close();
			os.close();
		  
		  }catch (WriteException e) {
		   e.printStackTrace();
		  } catch (IOException e) {
		   e.printStackTrace();
		  }
	     return null;
	}



效果图:

  • 大小: 113.2 KB
1
0
分享到:
评论

相关推荐

    java用jxl包导出excel表格

    java用jxl包导出excel表格 x

    JXL操作EXCEL(详)

    JXL操作EXCEL公式,颜色,表格,百分比,小数点等等等等

    通过java操作excel表格的工具类库jxl

     通过java操作excel表格的工具类库  支持Excel 95-2000的所有版本  生成Excel 2000标准格式  支持字体、数字、日期操作  能够修饰单元格属性  支持图像和图表  应该说以上功能已经能够大致满足我们的需要。最...

    jxl包-用于java读取excel表格

    Jxl是一个开源的Java Excel API项目,通过Jxl,Java可以很方便的操作微软的Excel文档。除了Jxl之外,还有Apache的一个POI项目,也可以操作Excel,两者相比之下:Jxl使用方便,但功能相对POI比较弱。POI使用复杂,...

    Excel数据操纵-jar包-jxl.jar

    通过java操作excel表格的工具类库  支持Excel 95-2000的所有版本  生成Excel 2000标准格式  支持字体、数字、日期操作  能够修饰单元格属性  支持图像和图表  应该说以上功能已经能够大致满足我们的需要...

    POI读写海量Excel(详细解读)

    至于Excel的写操作,对较高版本的Excel2007,POI提供了很好的支持,主要流程是第一步构建工作薄和电子表格对象,第二步在一个流中构建文本文件,第三步使用流中产生的数据替换模板中的电子表格。这种方式也可以处理...

    js导出excel

    通过js和jxl导出jsp页面的table表格到excel,兼容所有浏览器及复杂的table布局

    Java_Web开发实战1200例第1卷.part2

    21.1 应用JXL组件操作Excel 776 21.2 应用POI组件操作Excel 807 第22章 报表与打印 829 22.1 Web打印 830 22.2 利用Word打印报表 833 22.3 利用Excel打印报表 838 22.4 应用WebBrowser+CSS套打邮寄 22.5 打印库存...

    Java_Web开发实战1200例第1卷.part3

    21.1 应用JXL组件操作Excel 776 21.2 应用POI组件操作Excel 807 第22章 报表与打印 829 22.1 Web打印 830 22.2 利用Word打印报表 833 22.3 利用Excel打印报表 838 22.4 应用WebBrowser+CSS套打邮寄 22.5 打印库存...

    soaoffice示例代码

    需要把html页面中表格导出到excel的; 5. 需要在html页面中套打票据、报表的; 6. 需要在html页面中显示复杂报表,并要求精确打印预览及打印的; 7. 需要动态生成中国式复杂格式报表的; 8. 需要在html页面中输入...

    SOAOFFICE - 微软 OFFICE 中间件

    4. 需要把html页面中表格导出到excel的; 5. 需要在html页面中套打票据、报表的; 6. 需要在html页面中显示复杂报表,并要求精确打印预览及打印的; 7. 需要动态生成中国式复杂格式报表的; 8. 需要在html页面中输入...

Global site tag (gtag.js) - Google Analytics