当前位置:首页 > 开发 > Web前端 > 前端 > 正文

j2ee 导出excel ,poi + jxl

发表于: 2014-09-22   作者:BlueSkator   来源:转载   浏览次数:
摘要: 1、POI 导出 Excel public static void exportUserExcel(String[] headers, List<Muser> userList, OutputStream out) { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("

1、POI 导出 Excel

public static void exportUserExcel(String[] headers, List<Muser> userList, OutputStream out) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("会员表");
		HSSFRow row = sheet.createRow(0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(headers[i]);
			cell.setCellValue(text);
		}
		for (int i = 0; i < userList.size(); i++) {
			Muser user = userList.get(i);
			row = sheet.createRow(i + 1);
			row.createCell(0).setCellValue(user.getId());
			row.createCell(1).setCellValue(user.getName());
			row.createCell(2).setCellValue(user.getPhone());
			row.createCell(3).setCellValue(user.getIdCard());
			row.createCell(4).setCellValue(user.getAge());
			row.createCell(5).setCellValue(user.getAddress());
			row.createCell(6).setCellValue(user.getBirthplace());
			row.createCell(7).setCellValue(user.getCredits().toString());
			row.createCell(8).setCellValue(user.getCreatedtimeString());
			row.createCell(9).setCellValue(user.getLastlogtimeString());
			row.createCell(10).setCellValue(
					user.getStatus().equals("0") ? "正常" : "黑名单");
		}
		try {
			//FileOutputStream out = new FileOutputStream(path);
			wb.write(out);
			out.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

 

2、JXL导出Excel

public static void expertHotelOrderExcel(List<HotelOrder> hotelOrderList){
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/octet-stream;charset=UTF-8");
			response.setHeader("Content-Disposition","attachment;filename="+Helper.getDate()+".xls");
			// 注意这里的header参数为Content-Disposition而非第一种中的contentDisposition参数
			// Excel获得文件
			// 打开一个文件的副本,并且指定数据写回到原文件
			WritableWorkbook book = Workbook.createWorkbook(response.getOutputStream());
			// 添加一个工作表
			WritableSheet sheet = book.createSheet("酒店订单表", 0);
			sheet.addCell(new Label(0, 0, "客栈名称"));
			sheet.addCell(new Label(1, 0, "订单编号"));
			sheet.addCell(new Label(2, 0, "会员编号"));
			sheet.addCell(new Label(3, 0, "总金额"));
			sheet.addCell(new Label(4, 0, "实付款"));
			sheet.addCell(new Label(5, 0, "获取积分"));
			sheet.addCell(new Label(6, 0, "使用积分"));
			sheet.addCell(new Label(7, 0, "交易状态"));
			sheet.addCell(new Label(8, 0, "审核状态"));
			sheet.addCell(new Label(9, 0, "成交时间"));
			if (hotelOrderList!= null) {
				IHotelManager hotelManager =SpringContextHolder.getBean("hotelManager",IHotelManager.class);
				for (int i = 0; i < hotelOrderList.size(); i++) {
					HotelOrder hotelOrder = hotelOrderList.get(i);
					
					String status = "";
					if (hotelOrder.getOrderStatus() == 0) {
						status = "未支付";
					} else if (hotelOrder.getOrderStatus() == 1) {
						status = "交易成功";
					} else {
						status = "交易取消";
					}
					String auditStatus = "";
					if (hotelOrder.getAuditStatus().equals("0")) {
						auditStatus = "未审核";
					} else {
						auditStatus = "已审核";
					}
					sheet.addCell(new Label(0, i+1, hotelManager.getById(hotelOrder.getHotelId()).getHotelName()));
					sheet.addCell(new Label(1, i+1, hotelOrder.getId()));
					sheet.addCell(new Label(2, i+1, hotelOrder.getUserId()));
					sheet.addCell(new Label(3, i+1, hotelOrder.getOrderAmount().toString()));
					sheet.addCell(new Label(4, i+1, hotelOrder.getPayAmount().toString()));
					sheet.addCell(new Label(5, i+1, hotelOrder.getGetCredits().toString()));
					sheet.addCell(new Label(6, i+1, hotelOrder.getUsedCredits().toString()));
					sheet.addCell(new Label(7, i+1, status));
					sheet.addCell(new Label(8, i+1, auditStatus));
					if(hotelOrder.getDealTime()==null){
						sheet.addCell(new Label(9, i+1, "暂无时间"));
					}else{
						sheet.addCell(new Label(9, i+1, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(hotelOrder.getDealTime())));
					}
				}
			book.write();
			book.close();
			// 关闭输出流,同时也关闭response里的OutputStream,所以无需再调用response.getOutputStream().close()了
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 

j2ee 导出excel ,poi + jxl

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
下面是对poi导出excel的封装,稍微改动一下就能用于其它项目 pojo类见http://liudeh-009.iteye.com/b
一.导出到Excel 基本思路:点击导出后生成临时.xls文件,返回文件名,供用户下载,下载完后删除文件
转自:http://dev.yesky.com/36/2478036.shtml 发现天极的教程还不错。 引言   报表输出是开发数
下面是对poi导出excel的封装,稍微改动一下就能用于其它项目 pojo类见http://liudeh-009.iteye.com/b
平时用js导出table有些不足,导出的Excel内容也不够好看。 所以写了个通用的将Table导出为Excel的方
前些日子把JXL替换为ApachePOI,原因很简单,JXL在2009年10月已经停止更新,并且不支持Excel 2007 O
最近需要需要项目开发需要从excel导入数据到数据库,于是就开始找开源的java操作excel的框架。貌似
前些日子把JXL替换为ApachePOI,原因很简单,JXL在2009年10月已经停止更新,并且不支持Excel 2007 O
前些日子把JXL替换为ApachePOI,原因很简单,JXL在2009年10月已经停止更新,并且不支持Excel 2007 O
一:poi jar下载地址:点击打开链接: 二:工程截图: 三:运行效果截图: 四:源代码: Student.ja
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号