Excel導出多個sheet表格

public class ExportExcelUtil {

    /**
     * 生成Excel表格
     *
     * @author:
     * @CreateDate: 2019/5/8 9:35
     *
     * @Modifier
     * @ModifyData
     * @ModifyDescription 描述修改內(nèi)容
     * @param [workbook, sheetNum, sheetTitle, headers, result]
     * @return void
     * @throws
     */
    public static void exportExcel(HSSFWorkbook workbook, int sheetNum, String sheetTitle,
                                   String[] headers, List<List<String>> result) {

        // 生成一個表格
        HSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);
        // 設置表格默認列寬度為20個字節(jié)
        sheet.setDefaultColumnWidth(20);

        // 生成一個樣式
        HSSFCellStyle style = workbook.createCellStyle();
        // 設置以下樣式
        style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // 生成一個字體
        HSSFFont font = workbook.createFont();
        font.setColor(HSSFColor.BLACK.index);
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        // 把字體應用到當前的樣式
        style.setFont(font);

        // 指定當單元格內(nèi)容顯示不下時自動換行
        style.setWrapText(true);

        // 產(chǎn)生表格標題行
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(style);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text.toString());
        }

        // 遍歷集合數(shù)據(jù),產(chǎn)生數(shù)據(jù)行
        if (result != null) {
            int index = 1;
            for (List<String> m : result) {
                row = sheet.createRow(index);
                int cellIndex = 0;
                for (String str : m) {
                    HSSFCell cell = row.createCell(cellIndex);
                    cell.setCellValue(str);
                    cellIndex++;
                }
                index++;
            }
        }
    }

}
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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