Java使用POI做excel导出,poi版本:3.17

1、在pom.xml中导入相关依赖


      org.apache.poi
      poi
      3.17
    
    
      org.apache.poi
      poi-ooxml
      3.17
    

2、创建excel导出工具类

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.jfinal.plugin.activerecord.Record;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

public class ExcelUtil {

    /**
     * listData为数据库里查询出来的List对象
     * @author wangjp
     * @param listData
     * @return
     */
    public static File exportExcel(List listData) {
        // 设置文件名
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String filename = "投资建议书导出列表" + dateFormat.format(date) + ".xls";
        File file = new File(filename);
        // 标题
        Map titleData = new LinkedHashMap();// 标题,后面用到
        titleData.put("investment_code", "申请单编号");
        titleData.put("jingbanren", "申请人");
        titleData.put("create_date", "申请日期");
        titleData.put("type", "业务类型");
        titleData.put("content", "概要");
        titleData.put("rival", "对手方");
        titleData.put("finishWorknode", "状态");
        titleData.put("nextWorknode", "下次审批人");
        file = saveFile(titleData, listData, file);
        return file;
    }

    /**
     * excel生成
     * @author wangjp
     * @param headData
     * @param recordList
     * @param file
     * @return
     */
    private static File saveFile(Map headData, List recordList, File file) {
        // 创建工作薄
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        // sheet:一张表的简称
        // row:表里的行
        // 创建工作薄中的工作表
        HSSFSheet hssfSheet = hssfWorkbook.createSheet();
        // 创建行
        HSSFRow title = null;
        HSSFRow row = null;
        // 创建单元格,设置表头 创建列
        HSSFCell cell = null;
        // 创建标题
        title = hssfSheet.createRow(0);
        title.setHeight((short) 400);
        // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
        cell = title.createCell(0);
        // 创建字体样式
        HSSFFont titleFont = hssfWorkbook.createFont();
        titleFont.setFontName("黑体");
        titleFont.setFontHeightInPoints((short) 14);//设置字体大小
        HSSFFont rowFont = hssfWorkbook.createFont();
        rowFont.setFontName("黑体");
        rowFont.setFontHeightInPoints((short) 10);//设置字体大小
        HSSFFont contentFont = hssfWorkbook.createFont();
        contentFont.setFontHeightInPoints((short) 10);//设置字体大小
        // 设置标题样式为居中
        HSSFCellStyle titleStyle = hssfWorkbook.createCellStyle();
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        titleStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        titleStyle.setBorderBottom(BorderStyle.THIN);//下边框    
        titleStyle.setBorderLeft(BorderStyle.THIN);//左边框    
        titleStyle.setBorderTop(BorderStyle.THIN);//上边框    
        titleStyle.setBorderRight(BorderStyle.THIN);//右边框  
        titleStyle.setFont(titleFont);
        // 创建标题头样式
        HSSFCellStyle rowStyle = hssfWorkbook.createCellStyle();//水平居中
        rowStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        rowStyle.setBorderBottom(BorderStyle.THIN);//下边框    
        rowStyle.setBorderLeft(BorderStyle.THIN);//左边框    
        rowStyle.setBorderTop(BorderStyle.THIN);//上边框    
        rowStyle.setBorderRight(BorderStyle.THIN);//右边框  
        rowStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());// 设置背景色
        rowStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        rowStyle.setFont(rowFont);
        // 创建内容样式
        HSSFCellStyle style = hssfWorkbook.createCellStyle();
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        style.setBorderBottom(BorderStyle.THIN);//下边框    
        style.setBorderLeft(BorderStyle.THIN);//左边框    
        style.setBorderTop(BorderStyle.THIN);//上边框    
        style.setBorderRight(BorderStyle.THIN);//右边框  
        style.setFont(contentFont);
        // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
        hssfSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        hssfSheet.setColumnWidth((short) 0, 5300); //调整第一列宽度
        hssfSheet.setColumnWidth((short) 1, 3500); //调整第二列宽度
        hssfSheet.setColumnWidth((short) 2, 3500); //调整第三列宽度
        hssfSheet.setColumnWidth((short) 3, 4000); //调整第四列宽度
        hssfSheet.setColumnWidth((short) 4, 10000); //调整第五列宽度
        hssfSheet.setColumnWidth((short) 5, 3000); //调整第六列宽度
        hssfSheet.setColumnWidth((short) 6, 3000); //调整第七列宽度
        hssfSheet.setColumnWidth((short) 7, 3000); //调整第八列宽度
        cell.setCellValue("申请单列表");
        cell.setCellStyle(titleStyle);
        // 初始化索引
        int rowIndex = 1;
        int cellIndex = 0;

        // 创建标题行
        row = hssfSheet.createRow(rowIndex);
        row.setHeight((short) 280);

        rowIndex++;
        // 遍历标题
        for (String h : headData.keySet()) {
            // 创建列
            cell = row.createCell(cellIndex);
            // 索引递增
            cellIndex++;
            // 逐列插入标题
            cell.setCellValue(headData.get(h));
            cell.setCellStyle(rowStyle);
        }

        // 得到所有记录 行:列
        Record record = null;

        if (recordList != null) {
            // 获取所有的记录 有多少条记录就创建多少行
            for (int i = 0; i < recordList.size(); i++) {
                row = hssfSheet.createRow(rowIndex);
                row.setHeight((short) 330);
                // 得到所有的行 一个record就代表 一行
                record = recordList.get(i);
                // 下一行索引
                rowIndex++;
                // 刷新新行索引
                cellIndex = 0;
                // 在有所有的记录基础之上,便利传入进来的表头,再创建N行
                for (String h : headData.keySet()) {
                    cell = row.createCell(cellIndex);
                    cellIndex++;
                    // 按照每条记录匹配数据
                    cell.setCellValue(record.get(h) == null ? "" : record.get(h).toString());
                    cell.setCellStyle(style);
                }
            }
        }
        try {
            FileOutputStream fileOutputStreane = new FileOutputStream(file);
            hssfWorkbook.write(fileOutputStreane);
            fileOutputStreane.flush();
            fileOutputStreane.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

}

3、因为项目需求里提到要有背景颜色,另附颜色对比图链接一份

poi背景颜色

你可能感兴趣的:(java学习,poi,java导出excel,poi格式设置)