java利用poi實現Excel考勤報表的輸出

java利用poi實現Excel考勤報表的輸出

實現效果

  • SXSSFWorkbook超大數據導出
  • 標題、表頭、內容有樣式
  • 可以多個sheet(滿65535行數據換新的sheet)
  • 一度爲快

maven依賴

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.15</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.15</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.jexcelapi</groupId>
        <artifactId>jxl</artifactId>
        <version>2.6.10</version>
    </dependency>

函數實現

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.*;

public class Testt {
    public static void test(String toFilePath, int total_records) {
        File file = new File(toFilePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String[] assetHeadTemp = {"姓名", "工號", "人員類型", "出勤天數", "休息天數", "2019-12-13"};
        SXSSFWorkbook wb = new SXSSFWorkbook(1024);
        Row row;
        OutputStream os = null;
        //樣式
        CellStyle titleStyle = createTitleCellStyle(wb);
        CellStyle titleReportTimeCellStyle = createTitleReportTimeCellStyle(wb);
        CellStyle headerStyle = createHeadCellStyle(wb);
        CellStyle contentStyle = createContentCellStyle(wb);
        try {
            os = new FileOutputStream(file.getAbsolutePath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Sheet sheet;

        //確認sheet的個數--65534+表頭=65535;一般的表格每個sheet支持65535row數據
        int temp_sheet_count = total_records / 65534;
        int sheet_count = (total_records % 65534) == 0 ? temp_sheet_count : temp_sheet_count + 1;

        for (int j = 0; j < sheet_count; j++) {
            sheet = wb.createSheet("彙總分頁" + (j + 1));
            sheet.setDefaultRowHeight((short) 500);
            //創建標題
            row = sheet.createRow(0);
            row.setHeight((short) 800);
            Cell title_cell = row.createCell(0);
            title_cell.setCellValue("彙總報表  日期 2019-12-12 00:00:00 - 2019-12-13 00:00:00");
            title_cell.setCellStyle(titleStyle);
            sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), 0, assetHeadTemp.length - 1));

            //創建生成時間
            row = sheet.createRow(1);
            row.setHeight((short) 600);
            Cell create_report_cell = row.createCell(0);
            create_report_cell.setCellValue("報表生成時間 2019-12-13 00:00:00");
            create_report_cell.setCellStyle(titleReportTimeCellStyle);
            sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), row.getRowNum(), 0, assetHeadTemp.length - 1));

            // 輸出表頭
            row = sheet.createRow(2);
            for (int i = 0; i < assetHeadTemp.length; i++) {
                if (i > 4) {
                    sheet.setColumnWidth(i, 9532);
                } else {
                    sheet.setColumnWidth(i, 4766);
                }
                Cell cell = row.createCell(i);
                cell.setCellStyle(headerStyle);
                cell.setCellValue(assetHeadTemp[i]);
            }
            // 輸出內容
            int rowIndex = 3;
            //創建每個sheet的row數據
            int row_nums = 65534;
            if (j == sheet_count - 1) {
                row_nums = total_records % 65534;
            }

            for (int n = 0; n < row_nums; n++) {
                row = sheet.createRow(rowIndex++);
                row.setHeight((short) 600);

                Cell cell_0 = row.createCell(0);
                cell_0.setCellStyle(contentStyle);
                cell_0.setCellValue("0");

                Cell cell_1 = row.createCell(1);
                cell_1.setCellStyle(contentStyle);
                cell_1.setCellValue("1");

                Cell cell_2 = row.createCell(2);
                cell_2.setCellStyle(contentStyle);
                cell_2.setCellValue("2");

                Cell cell_3 = row.createCell(3);
                cell_3.setCellStyle(contentStyle);
                cell_3.setCellValue("3");

                Cell cell_4 = row.createCell(4);
                cell_4.setCellStyle(contentStyle);
                cell_4.setCellValue("4");

                Cell cell_5 = row.createCell(5);
                cell_5.setCellStyle(contentStyle);
                cell_5.setCellValue("5");
            }
        }
        try {
            wb.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (wb != null) {
                try {
                    wb.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static CellStyle createTitleCellStyle(SXSSFWorkbook wb) {
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直對齊
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());//背景顏色

        Font headerFont1 = wb.createFont(); // 創建字體樣式
        headerFont1.setBold(true); //字體加粗
        headerFont1.setColor(IndexedColors.GREEN.getIndex());
        headerFont1.setFontName("黑體"); // 設置字體類型
        headerFont1.setFontHeightInPoints((short) 12); // 設置字體大小
        cellStyle.setFont(headerFont1); // 爲標題樣式設置字體樣式

        return cellStyle;
    }

    private static CellStyle createTitleReportTimeCellStyle(SXSSFWorkbook wb) {
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直對齊
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());//背景顏色

        Font headerFont1 = wb.createFont(); // 創建字體樣式
        headerFont1.setBold(true); //字體加粗
        headerFont1.setColor(IndexedColors.GREEN.getIndex());
        headerFont1.setFontName("黑體"); // 設置字體類型
        headerFont1.setFontHeightInPoints((short) 10); // 設置字體大小
        cellStyle.setFont(headerFont1); // 爲標題樣式設置字體樣式

        return cellStyle;
    }

    private static CellStyle createHeadCellStyle(SXSSFWorkbook wb) {
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setWrapText(true);// 設置自動換行
        cellStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());//背景顏色
        cellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直對齊
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
        cellStyle.setBorderBottom(BorderStyle.THIN); //下邊框
        cellStyle.setBorderLeft(BorderStyle.THIN); //左邊框
        cellStyle.setBorderRight(BorderStyle.THIN); //右邊框
        cellStyle.setBorderTop(BorderStyle.THIN); //上邊框

        Font headerFont = wb.createFont(); // 創建字體樣式
        headerFont.setBold(true); //字體加粗
        headerFont.setFontName("黑體"); // 設置字體類型
        headerFont.setFontHeightInPoints((short) 12); // 設置字體大小
        cellStyle.setFont(headerFont); // 爲標題樣式設置字體樣式

        return cellStyle;
    }

    private static CellStyle createContentCellStyle(SXSSFWorkbook wb) {
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中
        cellStyle.setWrapText(true);// 設置自動換行

        Font font = wb.createFont();
        font.setFontHeightInPoints((short) 12);
        cellStyle.setFont(font);

        return cellStyle;
    }

    public static void main(String[] args) {
        test("D:\\test.xlsx", 10);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章