Hutool导出Excel,导多个Sheet页

重要方法

// 指定要写出的 Sheet 页
bigWriter.setSheet(sheet.getSheetName());

工具类

public class HuExcelUtils {
    
    /** 
     * 导出多个 Sheet 页
     * @param response
     * @param sheetList 页数据
     * @param fileName 文件名
     */
    public static void exportExcel(HttpServletResponse response, List<SheetDTO> sheetList, String fileName) {
        ExcelWriter bigWriter = ExcelUtil.getBigWriter();
        // 重命名第一个Sheet的名称,不然会默认多出一个Sheet1的页
        bigWriter.renameSheet(0, sheetList.get(0).getSheetName());
        for (SheetDTO sheet : sheetList) {
            // 指定要写出的 Sheet 页
            bigWriter.setSheet(sheet.getSheetName());
            Integer[] columnWidth = sheet.getColumnWidth();
            if (columnWidth == null || columnWidth.length != sheet.getFieldAndAlias().size()) {
                // 设置默认宽度 
                for (int i = 0; i < sheet.getFieldAndAlias().size(); i++) {
                    bigWriter.setColumnWidth(i, 25);
                }
            } else {
                // 设置自定义宽度 
                for (int i = 0; i < columnWidth.length; i++) {
                    bigWriter.setColumnWidth(i, columnWidth[i]);
                }
            }
            // 设置字段和别名
            bigWriter.setHeaderAlias(sheet.getFieldAndAlias());
            // 设置只导出有别名的字段
            bigWriter.setOnlyAlias(true);
            // 设置默认行高
            bigWriter.setDefaultRowHeight(18);
            // 设置冻结行
            bigWriter.setFreezePane(1);
            // 一次性写出内容,使用默认样式,强制输出标题
            bigWriter.write(sheet.getCollection(), true);
            // 设置所有列为自动宽度,不考虑合并单元格
//            bigWriter.autoSizeColumnAll();
        }
        
        ServletOutputStream out = null;
        try {
            //response为HttpServletResponse对象
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", 
                    "attachment;filename=" + 
                            URLEncoder.encode(fileName + DateUtil.today() + ".xlsx", "UTF-8"));
            out = response.getOutputStream();
            bigWriter.flush(out, true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭writer,释放内存
            bigWriter.close();
        }
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }
}

/**
 * Excel - Sheet页
 */
public class SheetDTO implements Serializable {

    private static final long serialVersionUID = 1L;
    
    /** sheet页名称 */
    private String sheetName;
    
    /** 字段和别名,如果使用这个,properties 和 titles可以不用处理 */
    private Map<String, String> fieldAndAlias;
    
    /** 字段 */
    private String[] properties;
    
    /** 标题/别名 */
    private String[] titles;
    
    /** 列宽 */
    private Integer[] columnWidth;
    
    /** 数据集 */
    private Collection<?> collection;

    public Map<String, String> getFieldAndAlias() {
        if (fieldAndAlias == null) {
            this.fieldAndAlias = new LinkedHashMap<String, String>();
            for (int i = 0; i < properties.length; i++) {
                fieldAndAlias.put(properties[i], titles[i]);
            }
        }
        return fieldAndAlias;
    }

    public void setFieldAndAlias(Map<String, String> fieldAndAlias) {
        this.fieldAndAlias = fieldAndAlias;
    }
	// 省略其他set get
}

// 示例Controller
public Class TestController {

    /**
     * 条件全部导出
     */
    @ResponseBody
    @RequestMapping("/export_all")
    public void export_all(HttpServletResponse response, PolicyCountSupplementDTO policyCountSupplementDTO) {
        
        List<SheetDTO> sheetList = new ArrayList<SheetDTO>();
        
        List<Map<String, Object>> listDatas = policyCountSupplementService
                .findPage(policyCountSupplementDTO, null).getListData();

        /** 明细 */
        List<Map<String, Object>> policyCountItems = policyCountService.findItemInfoList(idStr);
        List<Map<String, Object>> policyCountSupplements = policyCountSupplementService.findListByPolicyCountId(idStr);

        //返利流水主表
        ExcelMultiViewData excelMultiViewData1 = getExcelMultiViewData1("政策上账导出", listDatas);
        SheetDTO sheet1 = new SheetDTO();
        sheet1.setTitles(excelMultiViewData1.getTitles());
        sheet1.setProperties(excelMultiViewData1.getProperties());
        sheet1.setSheetName(excelMultiViewData1.getSheetName());
        sheet1.setCollection(listDatas);
        sheetList.add(sheet1);
        
        //返利流水明细
        ExcelMultiViewData excelMultiViewData2 = getExcelMultiViewData2("政策上账导出", policyCountItems);
        SheetDTO sheet2 = new SheetDTO();
        sheet2.setTitles(excelMultiViewData2.getTitles());
        sheet2.setProperties(excelMultiViewData2.getProperties());
        sheet2.setSheetName(excelMultiViewData2.getSheetName());
        sheet2.setCollection(policyCountItems);
        sheetList.add(sheet2);
        
        // 特殊增补明细
        ExcelMultiViewData excelMultiViewData3 = getExcelMultiViewData3("政策上账导出", policyCountSupplements);
        SheetDTO sheet3 = new SheetDTO();
        sheet3.setTitles(excelMultiViewData3.getTitles());
        sheet3.setProperties(excelMultiViewData3.getProperties());
        sheet3.setSheetName(excelMultiViewData3.getSheetName());
        sheet3.setCollection(policyCountSupplements);
        sheetList.add(sheet3);

        HuExcelUtils.exportExcel(response, sheetList, "政策上账导出");
    }

}

你可能感兴趣的:(随手记,JavaEE)