POI 實現Excel導入導出

/**
 *
 * Author by peng .
 * Descriptions 建立excel 共三步
 *  一、initExcelWithSheetAndHeader
 *  二、fillRowWithData
 *  三、exportExcel
 * @Datetime in 2018/5/30 14:20.
 */
public class ExcelUtil {
    
    private static Workbook     workbook;
    private static Sheet        defaultSheet;
    private static CellStyle    headerCellStyle;
    private static CellStyle    defaultCellStyle;
    private static final String DEFAULT_SHEET_NAME = "Sheet";
    // XSSFWorkbook表示以xlsx爲後綴名的文件,XSSF是指2007年版本以上的
    private static final String SUFFIX_EXCEL_XLSX = ".xlsx";
    // HSSFWorkbook表示以xls爲後綴名的文件,HSSF是指2007年之前的
    private static final String SUFFIX_EXCEL_XLS = ".xls";

    // 建立表頭
    private static void createSheetHeader(List<String> headerTitles) {
        Row headerRow = defaultSheet.createRow(0);

        for (int i = 0; i < headerTitles.size(); i++) {
            Cell headerCell = headerRow.createCell(i);

            headerCell.setCellStyle(headerCellStyle);
            headerCell.setCellValue(headerTitles.get(i));
        }
    }

    /**
     * @desc
     * @author peng
     * @date  2018-08-26
     * @param  fileName
     * @param  response
     * @return void
     */
    public static void exportExcel(String fileName, HttpServletResponse response) throws IOException {
        try {

            // 自定義輸出文件名
            String excelName = new String(fileName.getBytes("UTF-8"), "UTF-8");

            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + excelName + SUFFIX_EXCEL_XLS);

            OutputStream outputStream = response.getOutputStream();

            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            workbook.close();
        }
    }

    /**
     * @desc 填充行數據
     * @author peng
     * @date  2018-08-26
     * @param rowNum
     * @param dataList
     */
    public static void fillRowWithData(int rowNum, List<String> dataList) {
        Row row = defaultSheet.createRow(rowNum);

        for (int i = 0; i < dataList.size(); i++) {

            // 建立單元格,並設置值
            Cell cell = row.createCell(i);

            cell.setCellValue(dataList.get(i));
            cell.setCellStyle(defaultCellStyle);
        }
    }

    // 建立sheet,使用默認sheet名
    private static void initExcelWithSheet() {
        initExcelWithSheet(DEFAULT_SHEET_NAME);
    }

    // 建立sheet
    private static void initExcelWithSheet(String sheetName) {
        initWorkbook();
        defaultSheet = workbook.createSheet(sheetName);
        defaultSheet.setDefaultColumnWidth(20);
        defaultSheet.setDefaultRowHeightInPoints(20);
    }

    /**
     * @desc 建立sheet
     * @author peng
     * @date  2018-08-26
     * @param headerTitles  標題行
     * @return void
     */
    public static void initExcelWithSheetAndHeader(List<String> headerTitles) {
        initExcelWithSheet();
        createSheetHeader(headerTitles);
    }

    /**
     * @desc 建立sheet
     * @author peng
     * @date  2018-08-26
     * @param sheetName sheet名
     * @param headerTitles  標題行
     * @return void
     */
    public static void initExcelWithSheetAndHeader(String sheetName, List<String> headerTitles) {
        initExcelWithSheet(sheetName);
        createSheetHeader(headerTitles);
    }

    private static void initWorkbook() {
        workbook = new HSSFWorkbook();

        Font headerTitleFont = workbook.createFont();

        headerTitleFont.setFontHeightInPoints((short) 14);
        headerTitleFont.setBold(true);

        // 默認font
        Font defaultFont = workbook.createFont();

        defaultFont.setFontHeightInPoints((short) 12);
        headerCellStyle = workbook.createCellStyle();
        headerCellStyle.setAlignment(HorizontalAlignment.CENTER);

        // 前景色不會覆蓋字體顏色
        headerCellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());

        // headerCellStyle.setFillBackgroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headerCellStyle.setBorderBottom(BorderStyle.THIN);
        headerCellStyle.setBorderLeft(BorderStyle.THIN);
        headerCellStyle.setBorderRight(BorderStyle.THIN);
        headerCellStyle.setBorderTop(BorderStyle.THIN);
        headerCellStyle.setFont(headerTitleFont);

        // 默認cell樣式
        defaultCellStyle = workbook.createCellStyle();
        defaultCellStyle.setFont(defaultFont);
        defaultCellStyle.setAlignment(HorizontalAlignment.LEFT);

        // cell中能以多行的形式顯示全部內容,
        // defaultCellStyle.setWrapText(true);
        defaultCellStyle.setLocked(true);
    }
}

複製代碼
相關文章
相關標籤/搜索