POI创建多个sheet的Excel文件

前言

最近项目上有个需求,需要导出员工的考勤报表,需要两个模块,一个考勤的统计,一个考勤记录详情,导出的时候需要同时包含两部分内容。但平时做的都是只导出一个sheet,搜集一些资料经过自己的整合特此记录一下。

代码 

废话不多说先上工具类代码。 

import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * 解析excel文件
 * @author guosk
 */
public class WriteExcelUtils {

    /**
     * 输出Excel文档
     * @param workbook 
     * @param resources 源数据
     * @param headerNames 表头
     * @param sheetName 表格名
     * @param columnNum 列数量
     * @param sheetNum 页码(sheet页码)
     * @throws IOException
     */
    public static void writeExcel(HSSFWorkbook workbook,
    					   List resources, 
    					   String[] headerNames,
    					   String sheetName,
                                           Integer columnNum,
    					   Integer sheetNum) throws IOException {
        // 创建表格
    	HSSFSheet sheet = workbook.createSheet();
    	sheet.setDefaultRowHeightInPoints(13);//默认宽度
        workbook.setSheetName(sheetNum, sheetName);
        // 设置列宽,根据
        for(int i=0; i<=columnNum; i++){
            sheet.setColumnWidth(i, 6000);
        }
        /*
		 * 创建合并区域
		 * CellRangeAddress(int 首行, int 最后一行, int 首列, int 最后一列);
         */
        CellRangeAddress add = new CellRangeAddress(0, 0, 0, columnNum);
        // 将创建的合并区域设置到表格中.
        sheet.addMergedRegion(add);
        // 创建行
        Row header = sheet.createRow(0);
        // 创建单元格. 合并后的单元格,编号合并. 
        //设置样式
        CellStyle titleStyle = workbook.createCellStyle();
        Font titlefont = workbook.createFont();
        titlefont.setFontName("黑体");
        //titlefont.setColor(IndexedColors.VIOLET.index);
        titlefont.setFontHeightInPoints((short)20);
        titlefont.setBold(true);
        titleStyle.setFont(titlefont);
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        Cell c = header.createCell(0);
        c.setCellValue(sheetName);
        c.setCellStyle(titleStyle);
        c = header.createCell(columnNum);
        // 编写表头
        // 定义表头的样式
        CellStyle headerStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontName("宋体");
        //font.setColor(IndexedColors.VIOLET.index);
        font.setFontHeightInPoints((short)16);
        headerStyle.setFont(font);
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        // 设置单元格样式
        Row headerRow = sheet.createRow(1);
        for (int i = 0; i < headerNames.length; i++) {
            Cell cell = headerRow.createCell(i);
            // 设置单元格样式
            cell.setCellStyle(headerStyle);
            cell.setCellValue(headerNames[i]);
        }
        // 设置表格数据的样式
        CellStyle bodyStyle = workbook.createCellStyle();
        Font bodyFont = workbook.createFont();
        bodyFont.setFontName("微软雅黑");
        //bodyFont.setColor(IndexedColors.BLUE.index);
        bodyFont.setFontHeightInPoints((short)12);
        bodyStyle.setFont(bodyFont);

        // 编辑表格体数据
        for (int i = 0; i < resources.size(); i++) {
            // 获取行数据
            String[] temp = resources.get(i);
            // 创建行
            Row bodyRow = sheet.createRow(i + 2);
            for (int cellNum = 0; cellNum < temp.length; cellNum++) {
                Cell bodyCell = bodyRow.createCell(cellNum);
                bodyCell.setCellStyle(bodyStyle);
                bodyCell.setCellValue(temp[cellNum]);
            }
        }
        sheet.getRow(0).setHeightInPoints(24);
        sheet.getRow(1).setHeightInPoints(20);
    }
}

实例 

public void main(String[] args) {
    //考勤统计数据
    List statistics = new ArrayList<>();
    //考勤记录
    List recards = new ArrayList<>();
    //创建工作薄
    HSSFWorkbook workbook = new HSSFWorkbook();
    //生成一个考勤统计sheet
    WriteExcelUtils.writeExcel(workbook, 
	                       statistics, 
	                       new String[]{"人员名称",
                                        "人员ID",
                                        "所属单位",
                                        "当月工作日数量",
                                        "出勤次数",
                                        "缺勤次数",
                                        "迟到次数",
                                        "早退次数",
                                        "缺卡次数"},
                              "考勤统计报表",
                                 8, 
				 0);
    //生成一个考勤详细记录sheet
    WriteExcelUtils.writeExcel(workbook, 
			       recards, 
			       new String[]{"人员名称",
                                        "人员ID",
                                        "所属单位",
                                        "考勤设备",
                                        "考勤日期",
                                        "上班打卡时间",
                                        "上班状态",
                                        "下班打卡时间",
                                        "下班状态"},
                            "原始考勤记录", 
                           8,
                           1);
    //输出
    OutputStream out = null;
    try{
        //输出文件
        File file =new File(filePath);    
	    if  (!file .exists()  && !file .isDirectory())
		{       
		    file.mkdirs();//如果文件夹不存在则创建        
		} 
	    out = new FileOutputStream(filePath+fileName);
	    workbook.write(out);
    } catch (Exception e) {
	    		e.printStackTrace();
    } finally {
        try{
            // 清理资源
			out.close();//关闭
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

jar包指引:

poi:https://mvnrepository.com/artifact/org.apache.poi/poi/3.17

        https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/3.17

 

你可能感兴趣的:(POI创建多个sheet的Excel文件)