论坛首页 入门技术论坛

[讨论]poi jxl 操作excel出报表

浏览 6613 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-04-19  
最近学习java 通过poi jxl 等API 来操作Excel 出报表
有几点感想,在基本的功能方面(数据填充,基本格式绘制)感觉很简单
一下是一个绘制简单报表的方法:
	public static void toExcel(EpmEqRmaBO bo,File fileAddress)
	{ 
		String temp="";
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		I18NInfoService i18NInfoService = new I18NInfoService();
		try
		{
			//构建Workbook对象, 只读Workbook对象
			jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(fileAddress);
			jxl.write.WritableSheet ws = wwb.createSheet("Sheet 1", 0); 
			  //设置列宽
			  ws.setColumnView(0,6);
			  ws.setColumnView(1,12);
			  ws.setColumnView(2,12);
			  ws.setColumnView(3,12);
			  ws.setColumnView(4,12);
			  ws.setColumnView(5,20); 
			  ws.setColumnView(6,12);
			  ws.setColumnView(7,12);
			  ws.setColumnView(8,16);
			  ws.setColumnView(9,12);
			  //设置行高
			  ws.setRowView(0,600);
			  ws.setRowView(1,400);
			  ws.setRowView(2,300); 
			  ws.setRowView(4,300);
			  ws.setRowView(5,300);
			  ws.setRowView(6,300);

			//大标题的格式
			WritableFont font1 =  new  WritableFont(WritableFont.TIMES, 16 ,WritableFont.BOLD);
			WritableCellFormat title1 = new  WritableCellFormat(font1); 
			title1.setAlignment(jxl.format.Alignment.CENTRE); //水平对齐
			//小标题的格式
			WritableFont font2 =  new  WritableFont(WritableFont.TIMES, 14 ,WritableFont.BOLD);
			WritableCellFormat title2 = new  WritableCellFormat(font2); 
			title2.setAlignment(jxl.format.Alignment.CENTRE); //水平对齐			
			//次标题的格式
			WritableFont font3 =  new  WritableFont(WritableFont.TIMES, 10 ,WritableFont.NO_BOLD);
			WritableCellFormat title3 = new  WritableCellFormat(font3); 
			title3.setAlignment(jxl.format.Alignment.LEFT); //水平对齐			
			//表格栏目的格式
			WritableFont font4 =  new  WritableFont(WritableFont.TIMES, 10 ,WritableFont.NO_BOLD);
			WritableCellFormat title4 = new  WritableCellFormat(font4); 
			title4.setAlignment(jxl.format.Alignment.CENTRE); //水平对齐
			title4.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直对齐
			title4.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); //设置边框
			
			//设置数据类的格式
			WritableFont font5=  new  WritableFont(WritableFont.TIMES, 12 ,WritableFont.NO_BOLD);
			WritableCellFormat cell = new  WritableCellFormat(font5); 
			cell.setAlignment(jxl.format.Alignment.CENTRE); //水平对齐
			cell.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直对齐
			cell.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); //设置边框

			jxl.write.Label labelC = new jxl.write.Label(0, 0, "故障设备送修单",title1);
			ws.addCell(labelC); 
			ws.mergeCells(0,0,9,0); 
			labelC =new jxl.write.Label(0,1 , "Return Material Authorization Request (RMA)",title2);
			ws.addCell(labelC);
			ws.mergeCells(0,1,9,1);   
           
			if(bo.getEpmEqRmaVO().getCreatedTime()!=null && !"".equals(bo.getEpmEqRmaVO().getCreatedTime()) && bo.getEpmEqRmaVO().getCreatedTime().length()>10)
			{
				labelC = new jxl.write.Label(0,2 , "联系人:"+bo.getCustPersonName()+"             填单日期:"+bo.getEpmEqRmaVO().getCreatedTime().substring(0,10),title3);
			}
			else
			{
				labelC = new jxl.write.Label(0,2 , "联系人:"+bo.getCustPersonName()+"             填单日期:"+bo.getEpmEqRmaVO().getCreatedTime(),title3);
			} 
			ws.addCell(labelC);  
			ws.mergeCells(0,2,9,2);   
			
			//电话:                   传真:                   EMAIL:    此栏由xx填写:                           

			labelC = new jxl.write.Label(0,3 , "电话:" + bo.getOfficePhone()+"       传真:" + bo.getFax()+"       EMAIL:" + bo.getEmail(),title3);
			ws.addCell(labelC);
			ws.mergeCells(0,3,9,3);   
	 
			
			//用户单位:       邮编:        RMA确认号:       
			labelC = new jxl.write.Label(0,4 , "用户单位:" + bo.getCustName()+"       邮编:" + bo.getPostCode(),title3);
			ws.addCell(labelC);
			ws.mergeCells(0,4,6,4);
			//用户单位:       邮编:        RMA确认号:       
			labelC = new jxl.write.Label(7,4 , "RMA确认号:" + bo.getEpmEqRmaVO().getRmaNo(),title3);
			ws.addCell(labelC);
			ws.mergeCells(7,4,9,4);				
			//收货地址:        确认日期:           年       月            
			
			//联系人的地址改为收货地址
			temp =  
				i18NInfoService.getInfo(
						" ECC_CUST_PERSON_INFO p",
						" p.communication_address",
						" rownum=1 and  p.id=" + bo.getEpmEqRmaVO().getCustPersonId());
			
			//labelC = new jxl.write.Label(0,5 , "收货地址:" + temp); 
			//ws.addCell(labelC);
			if(bo.getEpmEqRmaVO().getEvaDate()!=null && !"".equals(bo.getEpmEqRmaVO().getEvaDate()) && bo.getEpmEqRmaVO().getEvaDate().length()>10)
			{
				labelC = new jxl.write.Label(0,5 , "收货地址:" + temp,title3);
				ws.addCell(labelC); 
				ws.mergeCells(0,5,6,5);
				labelC = new jxl.write.Label(7,5 , "确认日期:" + bo.getEpmEqRmaVO().getEvaDate().substring(0,10),title3);
			}
			else
			{
				labelC = new jxl.write.Label(0,5 , "收货地址:" + temp,title3);
				ws.addCell(labelC); 
				ws.mergeCells(0,5,6,5);
				labelC = new jxl.write.Label(7,5 , "确认日期:" + bo.getEpmEqRmaVO().getEvaDate(),title3); 
			}
			ws.addCell(labelC); 
			ws.mergeCells(7,5,9,5);
			
			//以下栏目由用户填写
			labelC = new jxl.write.Label(0,6 , "以下栏目由用户填写",title4); 
			ws.addCell(labelC); 
			ws.mergeCells(0,6,7,6); 
			labelC = new jxl.write.Label(8,6 , "",title4); 
			ws.addCell(labelC); 
			ws.mergeCells(8,6,9,6);
			//序号	故障日期	产品型号	设备名称	硬件版本	序列号 	设备服务号	故障描述	受理意见	备注说明
			labelC = new jxl.write.Label(0,7 , "序号",title4); 
			ws.addCell(labelC);
			labelC = new jxl.write.Label(1,7 , "故障日期",title4); 
			ws.addCell(labelC);
			labelC = new jxl.write.Label(2,7 , "产品型号",title4); 
			ws.addCell(labelC);
			labelC = new jxl.write.Label(3,7 , "设备名称",title4); 
			ws.addCell(labelC);
			labelC = new jxl.write.Label(4,7 , "硬件版本",title4); 
			ws.addCell(labelC);
			labelC = new jxl.write.Label(5,7 , "序列号",title4); 
			ws.addCell(labelC);
			labelC = new jxl.write.Label(6,7 , "设备服务号",title4); 
			ws.addCell(labelC);
			labelC = new jxl.write.Label(7,7 , "故障描述",title4); 
			ws.addCell(labelC);
			labelC = new jxl.write.Label(8,7 , "受理意见",title4); 
			ws.addCell(labelC);
			labelC = new jxl.write.Label(9,7 , "备注说明",title4); 
			ws.addCell(labelC);				
			int y=8;
			//setRamBoardList  明细列表迭带
			for(int i=0;i<bo.getRamBoardList().size();i++)
			{ 
				ReturnFormItemBO rbo = new ReturnFormItemBO();
				
				rbo = (ReturnFormItemBO)bo.getRamBoardList().get(i);
				
				ReturnFormItemVO rvo = rbo.getReturnFormItemVO(); 
				
                //序号	故障日期  	产品型号	设备名称	硬件版本	序列号 	设备服务号	故障描述	受理意见	备注说明
				labelC = new jxl.write.Label(0,y , String.valueOf(i+1),title4); 
				ws.addCell(labelC);

				if(rvo.getRmaErrorDate()!=null && !"".equals(rvo.getRmaErrorDate()) && rvo.getRmaErrorDate().length()>0)
				{
					labelC = new jxl.write.Label(1,y , rvo.getRmaErrorDate(),title4); 
				}
				else
				{
					labelC = new jxl.write.Label(1,y , "",title4); 
				} 
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(2,y , rvo.getProductmodel(),title4); 
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(3,y , rvo.getBoardname(),title4); 
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(4,y , rvo.getBoardedition(),title4); 
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(5,y , rvo.getBoardid(),title4); 
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(6,y , rvo.getFacilityServiceNO(),title4); 
				ws.addCell(labelC);
				
				labelC = new jxl.write.Label(7,y , rvo.getError(),title4); 
				ws.addCell(labelC);
				
				if(rvo.getRmaEvaFlag()!=null && "Y".equals(rvo.getRmaEvaFlag()))
				{ 
					labelC = new jxl.write.Label(8,y , "√",title4); 
					ws.addCell(labelC);
				}
				else
				{
					labelC = new jxl.write.Label(8,y , "×",title4); 
					ws.addCell(labelC);
				}
				
				labelC = new jxl.write.Label(9,y , rvo.getRmaEvaDevice(),title4); 
				ws.addCell(labelC);	
				y++;
			}
			//注意:1. 请将此表附在送修的故障硬件包装箱内。
			//2. 请不要把不受理的故障硬件送修。 
			//3. 请将此表附在送修的故障硬件包装箱内。

			//返修中心
			labelC = new jxl.write.Label(0,y , ""); 
			ws.addCell(labelC);
			ws.mergeCells(0,y,9,y);
			y++;
			
			//返修中心
			labelC = new jxl.write.Label(0,y , "注意:",title3);
			ws.addCell(labelC); 
			labelC = new jxl.write.Label(1,y , "1. 请将此表附在送修的故障硬件包装箱内。 ",title3);
			ws.addCell(labelC);
			ws.mergeCells(1,y,9,y);
			y++;
			
			//返修中心
			labelC = new jxl.write.Label(1,y , "2. 请不要把不受理的故障硬件送修。  ",title3);
			ws.addCell(labelC);
			ws.mergeCells(1,y,9,y);
			y++;
			
			//返修中心  
			labelC = new jxl.write.Label(1,y , "3. 请将此表附在送修的故障硬件包装箱内。 ",title3);       
			ws.addCell(labelC);
			ws.mergeCells(1,y,9,y);
			y++;
			

			//返修中心
			labelC = new jxl.write.Label(0,y , "/返修中心 ",title3);
			ws.addCell(labelC);
			ws.mergeCells(0,y,9,y);
			y++;

			labelC = new jxl.write.Label(0,y , "地址: 高新技术产业园科技南路通讯维修/返修中心B2座6楼      邮政编码: 518057 ",title3);
			ws.addCell(labelC);
			ws.mergeCells(0,y,9,y);
			y++;

			//返修中心
			labelC = new jxl.write.Label(0,y , "电话: 0755-26771776                                              传真: 0755-26770177 ",title3);
			ws.addCell(labelC);
			ws.mergeCells(0,y,9,y);
			y++;

			//返修中心
			labelC = new jxl.write.Label(0, y ,   "Email: RRCN@zzz.com.cn ",title3);
			ws.addCell(labelC);
			ws.mergeCells(0,y,9,y);

			wwb.write();
			wwb.close(); 

		}
		catch (Exception e)
		{
			e.printStackTrace();
		} 
	
	} 


但要绘制下面的模板感觉却很难:
要求: 整个版面事固定的,要求需设置边框样式,内部线格式,
      要求设置页边距,整个列表的长宽.

      要求数据内容的填充自动适应单元格大小,尽量排版美观(单元格固定大小)
   发表时间:2007-04-19  
大家有检验的介绍下.
0 请登录后投票
   发表时间:2007-04-20  
在workbook中可以设置样式的模板.比用代码设置要方便多了.
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics