利用POI导出excel

利用servlet输出流导出excel,需要有poi包个人封装了一个excel类仅供参考

public class Excle implements BaseExcle
{
	public Excle(){
	}
	
	public Excle(HttpServletResponse response,String savedName){
		this.setupDefaultHeader(response, savedName);
	}

	/**
	 * 版本:EAS7.0
	 * 作者(修改人):xu chang
	 * 修改时间:2014-9-17
	 *		<p>
	 * 描 述:覆盖父类方法 #excle基本导出功能
	 * @throws IOException 
	 * @throws WriteException 
	 * @see com.kingdee.eas.custom.common.excle.BaseExcle#export(javax.servlet.ServletOutputStream,
	 *  com.kingdee.eas.framework.CoreBaseCollection, java.lang.String[])
	 */
	public void export(ServletOutputStream servletOutputStream, CoreBaseCollection col, String[] colName) throws EASBizException, BOSException, IOException, WriteException
	{
		WritableWorkbook workbook=Workbook.createWorkbook(servletOutputStream);
		//定义表格title和内容的字体以及格式
		WritableFont font = new WritableFont(WritableFont.TAHOMA, 11,WritableFont.BOLD);// 定义字体
		
		// 字体颜色
		font.setColour(Colour.BLACK);
		WritableCellFormat headFormat = new WritableCellFormat(font);
		headFormat.setAlignment(Alignment.CENTRE); // 设置标题居中对其
		headFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置边框线
		headFormat.setBackground(jxl.format.Colour.YELLOW); // 设置单元格的背景颜色
		
		WritableCellFormat lineFormat = new WritableCellFormat();
		lineFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置边框线
		
		WritableCellFormat amountFormat = new WritableCellFormat();
		amountFormat.setAlignment(Alignment.RIGHT); // 设置标题居中对其
		amountFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 设置边框线
		
		WritableSheet sheet=workbook.createSheet("单据导出", 0);
		sheet.setRowView(0, 280); // 设置第一行的行高
		for (int i = 0; i < colName.length; i++) {
			if (i == 0) {
				sheet.setColumnView(i, 35);// 设置列宽
			}else if(i==colName.length-1){
				sheet.setColumnView(i, 15);// 设置列宽
			}else {
				sheet.setColumnView(i, 25);// 设置列宽
			}
			sheet.addCell(new Label(i, 0, colName[i], headFormat));
		}
		
		CoreBaseInfo baseInfo=col.get(0);
		if(baseInfo instanceof ProjectBillInfo){
			//立项 --11个字段
			for(int i=0;i<col.size();i++){
				ProjectBillInfo info=(ProjectBillInfo) col.get(i);
				sheet.addCell(new Label(0, i + 1,info.getLxnumber(),lineFormat));
				sheet.addCell(new Label(1, i + 1,info.getPmname(),lineFormat));
				sheet.addCell(new Label(2, i + 1,getObjName(info.getBuname()),lineFormat));
				sheet.addCell(new Label(3, i + 1,getObjName(info.getBuorz()),lineFormat));
				sheet.addCell(new Label(4, i + 1,getObjName(info.getCname()),lineFormat));
				sheet.addCell(new Label(5, i + 1,info.getOrzmag(),lineFormat));//区域负责人
				sheet.addCell(new Label(6, i + 1,info.getOrzsales(),lineFormat));//销售负责人
				sheet.addCell(new Label(7, i + 1,DateUtil(info.getSigndate()),lineFormat));//签订日期
				sheet.addCell(new Label(8, i + 1,formatObj(info.getProfitamt()),lineFormat));//评估金额
				sheet.addCell(new Label(9, i + 1,formatObj(info.getProfitpre()),lineFormat));//评估利润
				sheet.addCell(new Label(10, i + 1,formatObj(info.getState()),lineFormat));//单据状态
			}
			
		}
		
		workbook.write();
		workbook.close();
		servletOutputStream.flush();
		
		System.out.println("导出Excel成功!!!");
		
	}
	
	
	
	public void setupDefaultHeader(HttpServletResponse httpRes,String savedName) {
		String contentType = null;
		contentType = "application/octet-stream";
		httpRes.setContentType(contentType);
	
		StringBuffer contentDisposition = new StringBuffer();
		contentDisposition.append("attachment;");
		try {
			savedName = new String(savedName.getBytes("gb2312"), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			throw new IllegalArgumentException("savedName is invalid!");
		}
		contentDisposition.append("filename=\"").append(
				savedName.replace("\n", ""));
		contentDisposition.append("\"");
		httpRes.setHeader("Content-Disposition", contentDisposition.toString());
	}
	
	
	private String formatObj(Object obj)
	{
		if(obj instanceof Date){
			return obj != null ? new SimpleDateFormat("yyyy-MM-dd").format(obj)
				: null;
		}else if(obj instanceof CoreBaseInfo){
			String returnVal = "";
		    if(obj != null){		
		    	try{//特殊处理下,有些数据存在问题
		    		returnVal = ((CoreBaseInfo)obj).get("name").toString() ;
		    	}catch(Exception e){
		    		
		    	}
		    }
		    return returnVal;
		}else if(obj instanceof BigDecimal){
			NumberFormat formater = new DecimalFormat("#,###,##0.00");
			return obj == null ? formater.format(BigDecimal.ZERO) : formater
					.format(obj);
		}
		return obj==null ? "":obj.toString();
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是利用POI导出Excel表格的步骤: 1.导入POI的jar包。 在Java工程中,需要将POI的jar包添加到工程中。 2.创建工作簿(Workbook)和工作表(Sheet)对象。 使用POI创建Excel表格的第一步是创建工作簿和工作表对象。 3.创建表头。 在Excel表格中,表头通常是第一行,我们需要将表头信息写入到该行中。 4.写入数据利用Java代码将需要导出数据写入到Excel表格中。 5.设置单元格样式。 可以通过设置单元格样式来美化Excel表格。 6.保存Excel表格。 最后,将Excel表格保存到指定的位置。 下面是一个示例代码: ``` // 创建工作簿 Workbook wb = new HSSFWorkbook(); // 创建工作表 Sheet sheet = wb.createSheet("Sheet1"); // 创建表头 Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("姓名"); cell = row.createCell(1); cell.setCellValue("年龄"); // 写入数据 List<User> userList = getUserList(); for(int i = 0; i < userList.size(); i++){ User user = userList.get(i); row = sheet.createRow(i + 1); cell = row.createCell(0); cell.setCellValue(user.getName()); cell = row.createCell(1); cell.setCellValue(user.getAge()); } // 设置单元格样式 CellStyle style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); cell.setCellStyle(style); // 保存Excel表格 FileOutputStream fos = new FileOutputStream("user.xlsx"); wb.write(fos); fos.close(); ``` 以上是一个简单的POI导出Excel表格的示例代码,你可以根据具体需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值