poi写入excel2007

从数据库中读取数据并导出excel文件(要求excel2007版本)

xssfworkbook用于excel2007版本

 

 

service.java

	public Object invoke(Object inData) throws TonicareException {
		// TODO Auto-generated method stub
		WarehouseWasteRecordListVo vo=(WarehouseWasteRecordListVo) inData;
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String path = "";
	
		if(StringUtils.isNotEmpty(vo.getOpType())){
			if(Const.OPERATE_VIEW.equals(vo.getOpType())){
				
				String opid[]=vo.getOpId().split(",");
				vo.setOpId(opid[0]);//传值过来的id可能会将id重复 变成“31,31”这种形式
				DeliverDao dao=(DeliverDao) BeanFactory.getDao(Const.DAO_MGMT_DELIVER);
				WhWareHouse house=dao.getWarehouseById(Integer.parseInt(vo.getOpId()));
				
				String name =house.getName()+"—仓库废品处理记录"+System.currentTimeMillis();
				vo.setFileName(name);

 				path = this.WriteExcel(vo,conn,ps,rs);
				if (StringUtils.isNotEmpty(path))
					vo.setCsv(path);
			}				
		}
		return null;
	}
	

 

 

 

	public String WriteExcel(WarehouseWasteRecordListVo vo,Connection conn ,PreparedStatement ps,ResultSet rs ){
		String result=StringUtils.EMPTY;
		Date tmp=null;
		try {
			conn=DbHelper.getConnection();
			if(id>0){
				String strSql = "select  a.*,b.name,b.PRICE_QUOTE,b.BASE_ID,b.BASE_AMOUNT,b.TYPE_ID  from wh_order_waste a ,wh_product b  where 1=1  and a.PRODUCT_ID=b.CODE and a.WAREHOUSE_ID="+id;
				
				ps=conn.prepareStatement(strSql);
				rs=ps.executeQuery();
				while(rs.next()){
					WarehouseWasteRecordEditVo row=new WarehouseWasteRecordEditVo();
					row.setId(rs.getInt("id"));
					row.setWarehouseId(rs.getInt("warehouse_id"));
					row.setName(rs.getString("name"));
					row.setProductId(rs.getLong("product_id"));
					row.setAmount(rs.getInt("amount"));
					row.setFlag(rs.getInt("flag"));
					
					Integer typeid=rs.getInt("TYPE_ID");
					if(typeid!=null && typeid!=0){
						StProductTypeDao daotype= (StProductTypeDao) BeanFactory.getDao(Const.DAO_SELL_STPRODUCTTYPE);
						StProductType productType=daotype.getProductTypeById(typeid);
						row.setTypeName(productType.getName());
					}else{
						row.setTypeName("");
					}
					
					Long baseid=rs.getLong("BASE_ID");
					if(baseid!=null && baseid!=0){
						DeliverDao dao=(DeliverDao) BeanFactory.getDao(Const.DAO_MGMT_DELIVER);
						WhProduct pro=dao.getProductByCode(baseid).get(0);
						row.setSumPrice(row.getAmount()*pro.getPriceQuote());
					}else{
						row.setSumPrice(0.0);
					}
					
					tmp = new Date(rs.getTimestamp("UPDATE_TIME").getTime());
					row.setUpdateTime(tmp);
					vo.getDatas().add(row);
				}
			}
			
			result=Config.APP_PATH+File.separator+"report"+File.separator+vo.getFileName()+".xlsx";
			FileOutputStream fos=new FileOutputStream(result);
			XSSFWorkbook xfwb=new XSSFWorkbook();// 创建工作薄
			
			XSSFSheet sheet=xfwb.createSheet("sheet1");// 在工作薄中创建一工作表
			XSSFRow row=sheet.createRow(0);// 在指定的索引处创建一行
			row.createCell(0).setCellValue(vo.getFileName());
			
			XSSFRow row1=sheet.createRow(1);
			row1.createCell(0).setCellValue("序号");
			row1.createCell(1).setCellValue("产品条码");
			row1.createCell(2).setCellValue("产品名称");
			row1.createCell(3).setCellValue("分类");
			row1.createCell(4).setCellValue("废品处理数量(瓶)");
			row1.createCell(5).setCellValue("金额");
			row1.createCell(6).setCellValue("责任方");
			row1.createCell(7).setCellValue("时间");
			
			if(vo.getDatas()!=null && vo.getDatas().size()>0){
				for(int i = 0; i < vo.getDatas().size(); i++){
					WarehouseWasteRecordEditVo  bean=vo.getDatas().get(i);
					XSSFRow dataRow=sheet.createRow(i+2);
					
					dataRow.createCell(0).setCellValue((i+1));
					dataRow.createCell(1).setCellValue(String.valueOf(bean.getProductId()));//将产品id强制转换成字符串类型,避免excel报表出现不科学计算
					dataRow.createCell(2).setCellValue(bean.getName());
					dataRow.createCell(3).setCellValue(bean.getTypeName());
					dataRow.createCell(4).setCellValue(bean.getAmount());
					dataRow.createCell(5).setCellValue(bean.getSumPrice());
					
					String responsible=null;
					if(bean.getFlag()!=null){
						//1:公司废弃;2:供应商;3:物流赔付;
						if(bean.getFlag()==1){
							responsible="公司废弃";
						}else if(bean.getFlag()==2){
							responsible="供应商";
						}else if(bean.getFlag()==3){
							responsible="物流赔付";
						}
					}
					dataRow.createCell(6).setCellValue(responsible);
					
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					dataRow.createCell(7).setCellValue(sdf.format(bean.getUpdateTime()));
                                         //从数据库查询得到的时间已经转换为data形式  excel报表需要“yyyy-MM-dd hh:mm:ss”形式的数据 
				}
			}
			xfwb.write(fos);
			fos.flush();
			fos.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LogHelper.error(e);
		}finally{
			DbHelper.closedAll(rs, ps, conn);
		}
		
		return result;
	}

 

 action.java

 

		if (StringUtils.isNotEmpty(vo.getCsv())) {
			try {
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setContentType("application/force-download");
				response.setCharacterEncoding(Const.DEFAULT_CHARSET);
				response.setHeader("Content-Disposition", "attachment;filename=" +java.net.URLEncoder.encode(vo.getFileName(), "UTF-8") + ".xlsx");
				
				OutputStream os = null;
				FileInputStream fis = null;
				
				os = response.getOutputStream();
				fis = new FileInputStream(vo.getCsv());
				byte[] b = new byte[1024];
				int i = 0;
				while((i=fis.read(b)) > 0)
					os.write(b, 0, i);
				os.flush();
				os.close();
				fis.close();
				fis = null;
			}
			catch (Exception e) {
				LogHelper.info("");
			}
			return null;
		}

 

 

页面代码

 

<div>
		<ul class="toolBar">
			<li><a class="icon" href="deliver/warehouseWasteRecordList.do?opId=${opId}&opType=VIEW"  target="dwzExport"  targetType="navTab" id="exportProduct" postType="string" title="确定要导出这些记录吗?" ><span>导出废品处理记录</span></a></li>
		</ul>
	</div>

 

 

你可能感兴趣的:(xssfworkbook,poi写入excel)