导出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("导出信息失败!");
}
}
上一篇: Excel将多个sheet单独保存放在文件夹中 --gx
下一篇: 自定义li前面的圆点颜色
推荐阅读
-
java poi导出图片到excel示例代码
-
c#高效率导出多维表头excel的实例代码
-
C#导出生成excel文件的方法小结(xml,html方式)
-
使用ADODB将Excel中Sheet页另存为UTF8编码CSV 博客分类: VBA ADODBVBASheetCSVUTF8
-
将Excel里所有的Sheet页同时另存为单独的UTF8编码的CSV文件 博客分类: VBA ADODBVBACSVUTF8Sheet
-
ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例_PHP
-
将Excel里所有的Sheet页同时另存为单独的CSV文件 博客分类: VBA VBAExcelSheetCSV多个
-
python脚本实现数据导出excel格式的简单方法(推荐)
-
Python实现将数据库一键导出为Excel表格的实例
-
求解关于PHP导出EXCEL的一个疑义