POI通用導(dǎo)出類

一、概述

構(gòu)建通用Excel導(dǎo)出工具類。通過泛型將傳入的對(duì)象進(jìn)行導(dǎo)出

二、項(xiàng)目實(shí)現(xiàn)

  1. pom依賴
  <!-- POI -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.ooxml.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>${poi.ooxml-schemas.version}</version>
            </dependency>

2.導(dǎo)出方法

    public void export(List<Long> eventIds, HttpServletResponse response) throws Exception {
        if (eventIds.isEmpty()) {
            throw new Exception("需要導(dǎo)出的數(shù)據(jù)ID為空");
        }
        // 查詢數(shù)據(jù)庫(kù)記錄
        List<EventExportDO> eventExportDOS = eventRecordDao.queryExportRecords(eventIds);
        // 數(shù)據(jù)庫(kù)字段與Excel字段之間需要做轉(zhuǎn)換
        List<EventExportResult> exportResults = excelDataTransfer(eventExportDOS);

        // excel 構(gòu)建
        SXSSFWorkbook workbook = new SXSSFWorkbook();
        SXSSFSheet sheet = workbook.createSheet("事件庫(kù)記錄");
        // excel 標(biāo)題樣式
        CellStyle titleCellStyle = workbook.createCellStyle();
        titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
        Font titleFont = workbook.createFont();
        titleFont.setFontName("微軟雅黑");
        titleCellStyle.setFont(titleFont);
        // 構(gòu)建標(biāo)題
        ExcelUtil.createExcelTitle(ExcelCommonConstans.EVENT_RESOURCE_EXPORT_TITLE, sheet, titleCellStyle);

        // excel 正文樣式
        CellStyle dataCellStyle = workbook.createCellStyle();
        Font cellFont = workbook.createFont();
        cellFont.setFontName("宋體");
        dataCellStyle.setFont(cellFont);

        ExcelUtil.createExcelContent(exportResults, sheet, dataCellStyle);
        String exportFileName = ExcelCommonConstans.EVENT_EXPORT_FILE_NAME;
        exportFileName = URLEncoder.encode(exportFileName, "utf-8");
        OutputStream out = response.getOutputStream();
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + exportFileName);
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        workbook.write(out);
        out.flush();
        out.close();
    }

3.導(dǎo)出工具類

public  class ExcelUtil{
       /**
     * 創(chuàng)建Excel頭部信息
     *
     * @param titles    頭部標(biāo)題
     * @param sheet     表格
     * @param cellStyle 單元格
     */
    public static void createExcelTitle(String[] titles, SXSSFSheet sheet, CellStyle cellStyle) {
        SXSSFRow headerRow = sheet.createRow(0);
        for (int i = 0; i < titles.length; i++) {
            SXSSFCell cell = headerRow.createCell(i);
            if (null != cellStyle) {
                cell.setCellStyle(cellStyle);
            }
            cell.setCellValue(titles[i]);
        }
    }

    /**
     * 表格內(nèi)容
     *
     * @param dataDOs   表格內(nèi)容對(duì)象
     * @param sheet     表格
     * @param cellStyle 單元格樣式
     * @param <T>       泛型
     * @throws Exception 異常
     */
    public static <T> void createExcelContent(List<T> dataDOs, SXSSFSheet sheet, CellStyle cellStyle) throws Exception {
        for (int rowIndex = 0; rowIndex < dataDOs.size(); rowIndex++) {
            SXSSFRow row = sheet.createRow(rowIndex + 1);
            // 獲取泛型類
            T clazz = dataDOs.get(0);
            // 獲取對(duì)象屬性
            Field[] fields = clazz.getClass().getDeclaredFields();
            int cellSize = fields.length;
            for (int cellIndex = 0; cellIndex < cellSize; cellIndex++) {
                SXSSFCell cell = row.createCell(cellIndex);
                fields[cellIndex].setAccessible(true);
                if (null != cellStyle) {
                    cell.setCellStyle(cellStyle);
                }
                cell.setCellValue(fields[cellIndex].get(clazz) == null ? "" : fields[cellIndex].get(clazz).toString());
            }
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容