POI导出数据库数据到excel

废话就不说了,直接上代码。

public class TestPoi{
		
	/**
	*
	*
	*/
	private InputStream istream;
	private String fileName;
	public String exportSatistics() {
		try {
			list = orderService.getExportCount(map);
			String userAgent = this.getCurrentOperator().getUserAgent();//自己封装的获取使用的浏览器
			// 乱码
			if (fileName == null)
				fileName = "";
			String name = "统计";//导出文件名称
			//处理在不同浏览器导出文件乱码
			if (null != userAgent) {
				if (-1 != userAgent.indexOf("firefox")) {
					fileName = "=?UTF-8?B?"	+ (new String(Base64.encodeBase64(name.getBytes("UTF-8"))))+ "?=";
					
				} else if (-1 != userAgent.indexOf("chrome")) {
					fileName = new String(name.getBytes(), "ISO8859-1");
				} else {
					fileName = URLEncoder.encode(name, "UTF-8");
					fileName = fileName.replace("+", "%20");
				}
			}
			fileName = fileName + new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date()) + ".xls";
			this.createCountExcel(list, fileName);
		} catch (Exception e) {
			addActionError("导出文件异常!");
			flag = false;
			e.printStackTrace();
			return ERROR;
		}
		return "filedownload";
	}

	/**
	*
	*
	*/
	private InputStream createCountExcel(List<OrderStatisticVO> list, String fileName) {
		// 第①步,创建一个webbook,对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第②步,在webbook中添加一个sheet,对应Excel文件中的sheet
		String name = "商户订单统计报表";
		HSSFSheet sheet = wb.createSheet(name);
		// 指定合并区域
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, (short) 0,0, (short) 25);
		sheet.addMergedRegion(cellRangeAddress);
		HSSFCellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 指定单元格垂直居中对齐
		// 设置单元格字体
		HSSFFont font = wb.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font.setFontName("宋体");
		font.setFontHeight((short) 300);
		cellStyle.setFont(font);

		// 第③步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("商户订单统计报表");
		cell.setCellStyle(cellStyle);
		row = sheet.createRow(1);
		cell = row.createCell(0);
		cell.setCellValue("日期");
		cell = row.createCell(1);
		cell.setCellValue("订单号");
		cell = row.createCell(2);
		cell.setCellValue("商品名称");
		cell = row.createCell(3);
		cell.setCellValue("商品价格");
		cell = row.createCell(4);
		cell.setCellValue("商品数量");
		cell = row.createCell(5);
		cell.setCellValue("商品总价");
		cell = row.createCell(6);
		cell.setCellValue("收货人");
		cell = row.createCell(7);
		cell.setCellValue("收货地址");
		cell = row.createCell(8);
		cell.setCellValue("订单状态");
		// 第⑤步,写入实体数据 实际应用中这些数据从数据库得到
		if (list != null && !list.isEmpty()) {
			Iterator it=list.iterator();
			for (int i = 0; i < list.size(); i++) {
				row = sheet.createRow(i + 2);				
				OrderStatisticVO vo = (OrderStatisticVO) it.next();
				// 得到所有区域
				sheet.getNumMergedRegions();
				// 第④步,创建单元格,并设置值
				if (vo != null) {
					cell = row.createCell(0);
					cell.setCellValue(vo.getOrderDate());
					cell = row.createCell(1);
					cell.setCellValue(vo.getOrderId());
					cell = row.createCell(2);
					cell.setCellValue(vo.getProductName());
					cell= row.createCell(3);
					cell.setCellValue(vo.getProductPrice().doubleValue());
					cell = row.createCell(4);
					cell.setCellValue(vo.getProductCount());
					cell = row.createCell(5);
					cell.setCellValue(vo.getTotalPrice().doubleValue());
					cell = row.createCell(6);
					cell.setCellValue(vo.getUserName());
					cell = row.createCell(7);
					cell.setCellValue(vo.getShipAddress());
					cell = row.createCell(8);
					cell.setCellValue(vo.getOrderState());
				}
			}
		}

		// 第六步,将文件存到指定位置
		try {
			fileName = new String(fileName.getBytes("utf-8"), "utf-8");
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			wb.write(bos);
			istream = new ByteArrayInputStream(bos.toByteArray());
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return istream;
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值