欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

导出excel多个sheet

程序员文章站 2022-07-13 14:32:55
...

设置导出文件名,设置编码格式

String fileName = "用户【"+userId+"】记录";
//工具类
ExportExcelUtils exportExcelUtils = new ExportExcelUtils();
response.setContentType("application/binary;charset=UTF-8");
ServletOutputStream out=response.getOutputStream();
//文件后缀
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName+".xls", "UTF-8"));

处理数据库查询到的数据

//礼品回收
			List<List<String>> data0 = new ArrayList<List<String>>();
			for (GiftRecyclingw huiShou : huiShous) {
				List rowData = new ArrayList();
				rowData.add(huiShou.getStoreName());
				rowData.add(huiShou.getGoodsNumber()+"");
				rowData.add(huiShou.getGoodsName());
				rowData.add(huiShou.getNum()+"");
				rowData.add(huiShou.getExcScore()+"");
				rowData.add(huiShou.getOperator());
				rowData.add(huiShou.getRecoveryTime());
				data0.add(rowData);
			}
			//礼品兑换
			List<List<String>> data1 = new ArrayList<List<String>>();
			for (GiftExchangew duiHuan : duiHuans) {
				List rowData = new ArrayList();
				rowData.add(duiHuan.getStoreName());
				rowData.add(duiHuan.getGoodsNumber()+"");
				rowData.add(duiHuan.getGoodsName());
				rowData.add(duiHuan.getExcNum()+"");
				rowData.add(duiHuan.getDepleteScore()+"");
				rowData.add(duiHuan.getOperator());
				rowData.add(duiHuan.getExcTime());
				data1.add(rowData);
			}

调用工具类导出excle

exportExcelUtils.export(out,data0,data1,data2,data3,data4);

工具类

@SuppressWarnings("all")
public static HSSFWorkbook export(HSSFWorkbook workbook,List<List<String>> data,String name,String[] titles){
		// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet hssfSheet = workbook.createSheet(name);
		// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = hssfSheet.createRow(0);
		// 第四步,创建单元格,并设置值表头 设置表头居中
		HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
		// 居中样式
		hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		HSSFCell hssfCell = null;
		for (int i = 0; i < titles.length; i++) {
				hssfCell = row.createCell(i);// 列索引从0开始
				hssfCell.setCellValue(titles[i]);// 列名1
				hssfCell.setCellStyle(hssfCellStyle);// 列居中显示
			}
			for (int i = 0; i < data.size(); i++) {
				row = hssfSheet.createRow(i + 1);
				List<String> list = data.get(i);
				// 第六步,创建单元格,并设置值
				for (int j = 0; j < list.size(); j++) {
				row.createCell(j).setCellValue(list.get(j));
			}
		}
		return workbook;
	}
@SuppressWarnings("all")
public static void export(ServletOutputStream out,List<List<String>> data0,List<List<String>> data1,List<List<String>> data2,List<List<String>> data3,List<List<String>> data4) throws Exception {
		try {
			// 第一步,创建一个workbook,对应一个Excel文件
			HSSFWorkbook workbook = new HSSFWorkbook();
			String[] titles0 = { "门店","商品编号", "商品名称","回收数量","兑换积分","操作员","回收时间"};
			String[] titles1 = { "门店","商品编号", "商品名称","兑换数量","消耗积分","操作员","兑换时间"};
			String[] titles2 = { "充值金额","获得黄钻", "获得蓝钻","充值时间"};
			String[] titles3 = { "卡号","储币套餐", "储币金额","操作员","时间"};
			String[] titles4 = { "购币门店","购币金额","购币时间"};
			export(workbook, data0,"礼品回收",titles0);
			export(workbook, data1,"礼品兑换",titles1);
			export(workbook, data2,"会员中心充值",titles2);
			export(workbook, data3,"会员卡储币",titles3);
			export(workbook, data4,"微信购币",titles4);
			// 第七步,将文件输出到客户端浏览器
			try {
				workbook.write(out);
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("导出信息失败!");

		}
	}

相关标签: 导出excle多个sheet