POI導出Excel模板

導出模板+樣式+格式

/**
     * <p>生成excel導入模板</p>
     *
     * @param sheetName     文件名
     * @param headers           模板列頭
     * @param response         響應下載
     * @param exampleData  示例數據
     * @param columnStyle   每列格式
     */
    public static void writeExcel(String sheetName, String[] headers, HttpServletResponse response, String[] exampleData, Map<Integer, String> columnStyle) {
        // 聲明一個工作薄
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 生成一個表格
        XSSFSheet sheet = workbook.createSheet(sheetName);
        XSSFDataFormat dataFormat = workbook.createDataFormat();
        // 生成一個樣式
        XSSFCellStyle textStyle = workbook.createCellStyle();
        textStyle.setDataFormat(dataFormat.getFormat("@"));
        // 頭部樣式
        XSSFCellStyle headStyle = workbook.createCellStyle();
        headStyle.setDataFormat(dataFormat.getFormat("@"));
        // 數據樣式
        XSSFCellStyle bodyStyle = workbook.createCellStyle();
        bodyStyle.setDataFormat(dataFormat.getFormat("@"));
        // 生成一個字體
        XSSFFont font = workbook.createFont();
        font.setFontName("宋體");
        headStyle.setFont(font);
        headStyle.setAlignment(HorizontalAlignment.LEFT);
        headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 設置填充單元格
        headStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());// 設置填充單元填充顏色
        headStyle.setBorderTop(BorderStyle.THIN);
        headStyle.setBorderBottom(BorderStyle.THIN);
        headStyle.setBorderRight(BorderStyle.THIN);
        headStyle.setBorderLeft(BorderStyle.THIN);
        bodyStyle.setFont(font);
        bodyStyle.setAlignment(HorizontalAlignment.LEFT);
        bodyStyle.setWrapText(true);
        // 設置是某一列格式爲文本格式
        if (columnStyle != null && columnStyle.size() >0) {
            columnStyle.forEach((columnIndex, styleEnum) -> {
                sheet.setDefaultColumnStyle(columnIndex, textStyle);
            });
        }
        // 列頭
        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = row.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(headStyle);
        }
        // 樣例數據
        if (exampleData != null && exampleData.length > 0) {
            XSSFRow row1 = sheet.createRow(1);
            for (int i = 0; i < exampleData.length; i++) {
                XSSFCell cell = row1.createCell(i);
                cell.setCellValue(exampleData[i]);
                cell.setCellStyle(bodyStyle);
            }
        }
        // 自動列寬
        for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
        }
        String fileName = URLEncoder.encode(sheetName, StandardCharsets.UTF_8);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            log.error("", e);
            throw new SystemRuntimeException("導出模板出現異常", RespCodeEnum.E0500);
        } finally {
            try {
                workbook.close();
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                log.error("", e);
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章