java poi workbook 创建很慢_每天一个小技术之Java操作Excel

/ Java / 没有评论 / 1639浏览

java poi workbook 创建很慢_每天一个小技术之Java操作Excel

前言

在开发过程中,很多时候都会遇到导入Excel表的功能,比如批量导入某些数据,或者将某些数据导出,这时候就需要对Excel进行操作,从而实现导入导出功能。今天就学习一下Java操作Excel的技术:POI

一、POI

POI是Apache基金会用java编写的免费开源的跨平台的Java API,POI对Office文档有读和写的功能,但是我们一般用来操作Excel。所以本文仅提供对POI对Excel操作的方式。

1.1 使用简介

导入依赖:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>

主要类WordkBook接口,后续操作都在其上进行, 相当于Excel工作簿,有下边三个子实现:

1.2 写操作

写操作之03版本

@Test
public void testDemo1() throws  Exception{
    //创建Workbook工作簿
    Workbook w = new HSSFWorkbook();
    //创建Sheet
    Sheet sheet = w.createSheet("Excel统计表");
    //创建行
    Row row = sheet.createRow(0);
    //创建列
    Cell cell = row.createCell(0);
    //设置改行该列的值
    cell.setCellValue("统计个数");
    cell = row.createCell(1);
    cell.setCellValue(666);
    row = sheet.createRow(1);
    cell = row.createCell(0);
    cell.setCellValue("生成日期");
    cell = row.createCell(1);
    String date = new DateTime().toString("yyyy-MM-dd HH🇲🇲ss");
    cell.setCellValue(date);
    //写入到文件
    FileOutputStream fo = new FileOutputStream(PATH + "测试表.xls");
    w.write(fo);
    fo.close();
}

写操作之07版本

@Test
public void testDemo1() throws  Exception{
    //创建Workbook工作簿
    Workbook w = new XSSFWorkbook();
    //创建Sheet
    Sheet sheet = w.createSheet("Excel统计表");
    //创建行
    Row row = sheet.createRow(0);
    //创建列
    Cell cell = row.createCell(0);
    //设置改行该列的值
    cell.setCellValue("统计个数");
    cell = row.createCell(1);
    cell.setCellValue(666);
    row = sheet.createRow(1);
    cell = row.createCell(0);
    cell.setCellValue("生成日期");
    cell = row.createCell(1);
    String date = new DateTime().toString("yyyy-MM-dd HH🇲🇲ss");
    cell.setCellValue(date);
    //写入到文件
    FileOutputStream fo = new FileOutputStream(PATH + "测试表.xls");
    w.write(fo);
    fo.close();
}
```
#### 导出的数据如下图
![1](http://www.yanzuoguang.com/upload/2021/06/67uvlmjfo0gger0md5s1s0mj3m.png)

#### 写操作之优化版本
如果在写入07版本的时候,有大量的数据写入,就会有可能出现内存溢出异常,且写入的时间会变得很慢,所以引入了该优化类。
```java
@Test
public void testDemo3() throws Exception{
    long begin = System.currentTimeMillis();
    Workbook w = new SXSSFWorkbook();
    Sheet sheet = w.createSheet("Excel统计表");
    for (int rowNum = 0; rowNum < 100000; rowNum++) {
        Row row = sheet.createRow(rowNum);
        for (int cellNum = 0; cellNum < 10; cellNum++) {
            Cell cell = row.createCell(cellNum);
            cell.setCellValue(cellNum);
        }
    }
    long end = System.currentTimeMillis();
    System.out.println("消耗的时间为:"+ (end-begin));
    FileOutputStream fo = new FileOutputStream(PATH + "测试表07s.xlsx");
    w.write(fo);
    fo.close();
}
```
### 1.3 读操作
```java
@Test
public void testDemo4() throws Exception{
    FileInputStream inputStream = new FileInputStream(PATH+"明细表.xls");
    Workbook workbook = new HSSFWorkbook(inputStream);
    Sheet sheet = workbook.getSheetAt(0);
    Row title = sheet.getRow(0);
    if(title != null){
        int rowCount = title.getPhysicalNumberOfCells();
        for (int rowNum = 0; rowNum < rowCount; rowNum++) {
            Cell cell = title.getCell(rowNum);
            if(cell == null){
                continue;
            }
            String value = cell.getStringCellValue();
            System.out.print(value+"  |  ");
        }
    }
    System.out.println();
    int rowNums = sheet.getPhysicalNumberOfRows();
    for (int rowNum = 1; rowNum < rowNums; rowNum++) {
        Row row = sheet.getRow(rowNum);
        if(row == null){
            continue;
        }
        int count = row.getPhysicalNumberOfCells();
        for (int cellNum = 0; cellNum < count; cellNum++) {
            Cell cell = row.getCell(cellNum);
            if(cell == null){
                continue;
            }
            int cellType = cell.getCellType();
            String cellValue = "";
            switch (cellType){
                case HSSFCell.CELL_TYPE_STRING:
                    cellValue = cell.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    cellValue = String.valueOf(cell.getBooleanCellValue());
                    break;
                case HSSFCell.CELL_TYPE_BLANK:
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    if (HSSFDateUtil.isCellDateFormatted(cell)){
                        Date date = cell.getDateCellValue();
                        cellValue = new DateTime(date).toString("yyyy-MM-dd");
                    }else {
                        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                        cellValue = cell.toString();
                    }
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                    break;
            }
            System.out.print(cellValue + " | ");
        }
        System.out.println();
    }
    inputStream.close();
}
```
## 二、工具类
```java
/**
 * @Auther: Mr.BoBo
 * @Date: 2020/6/10 15:38
 * @Description:
 */
public class ExcelUtil {
    private final static String excel2003L =".xls";    //2003- 版本的excel
    private final static String excel2007U =".xlsx";   //2007+ 版本的excel
    /**
     * Excel导入
     */
    public static List<List<Object>> getBankListByExcel(InputStream in, String fileName) throws Exception{
        List<List<Object>> list = null;
        //创建Excel工作薄
        Workbook work = getWorkbook(in,fileName);
        if(null == work){
            throw new Exception("创建Excel工作薄为空!");
        }
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;
        list = new ArrayList<>();
        //遍历Excel中所有的sheet
        for (int i = 0; i < work.getNumberOfSheets(); i++) {
            sheet = work.getSheetAt(i);
            if(sheet==null){continue;}
            //遍历当前sheet中的所有行
            //包涵头部,所以要小于等于最后一列数,这里也可以在初始值加上头部行数,以便跳过头部
            for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                //读取一行
                row = sheet.getRow(j);
                //去掉空行和表头
                if(row==null||row.getFirstCellNum()==j){continue;}
                //遍历所有的列
                List<Object> li = new ArrayList<Object>();
                for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                    cell = row.getCell(y);
                    li.add(getCellValue(cell));
                }
                list.add(li);
            }
        }
        return list;
    }
    /**
     * 描述:根据文件后缀,自适应上传文件的版本
     */
    public static  Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{
        Workbook wb = null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        if(excel2003L.equals(fileType)){
            wb = new HSSFWorkbook(inStr);  //2003-
        }else if(excel2007U.equals(fileType)){
            wb = new XSSFWorkbook(inStr);  //2007+
        }else{
            throw new Exception("解析的文件格式有误!");
        }
        return wb;
    }
    /**
     * 描述:对表格中数值进行格式化
     */
    public static  Object getCellValue(Cell cell){
        Object value = null;
        DecimalFormat df = new DecimalFormat("0");  //格式化字符类型的数字
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");  //日期格式化
        DecimalFormat df2 = new DecimalFormat("0.00");  //格式化数字
        if(cell != null)
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    value = cell.getRichStringCellValue().getString();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if("General".equals(cell.getCellStyle().getDataFormatString())){
                        value = df.format(cell.getNumericCellValue());
                    }else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
                        value = sdf.format(cell.getDateCellValue());
                    }else{
                        value = df2.format(cell.getNumericCellValue());
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    value = cell.getBooleanCellValue();
                    break;
                case Cell.CELL_TYPE_BLANK:
                    value = "";
                    break;
                default:
                    break;
            }
        return value;
    }
    /**
     * 导入Excel表结束
     * 导出Excel表开始
     * @param sheetName 工作簿名称
     * @param clazz  数据源model类型
     * @param objs   excel标题列以及对应model字段名
     * @param map  标题列行数以及cell字体样式
     */
    public static XSSFWorkbook createExcelFile(Class clazz, List objs, Map<Integer, List<ExcelBean>> map, String sheetName) throws
        IllegalArgumentException,IllegalAccessException,InvocationTargetException,
    ClassNotFoundException, IntrospectionException, ParseException {
        // 创建新的Excel工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
        XSSFSheet sheet = workbook.createSheet(sheetName);
        // 以下为excel的字体样式以及excel的标题与内容的创建,下面会具体分析;
        createFont(workbook); //字体样式
        createTableHeader(sheet, map); //创建标题(头)
        createTableRows(sheet, map, objs, clazz); //创建内容
        return workbook;
    }

    private static XSSFCellStyle fontStyle;
    private static XSSFCellStyle fontStyle2;
    public static void createFont(XSSFWorkbook workbook) {
        // 表头
        fontStyle = workbook.createCellStyle();
        XSSFFont font1 = workbook.createFont();
        font1.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        font1.setFontName("黑体");
        font1.setFontHeightInPoints((short) 14);// 设置字体大小
        fontStyle.setFont(font1);
        fontStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); // 下边框
        fontStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);// 左边框
        fontStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);// 上边框
        fontStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);// 右边框
        fontStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 居中
        // 内容
        fontStyle2=workbook.createCellStyle();
        XSSFFont font2 = workbook.createFont();
        font2.setFontName("宋体");
        font2.setFontHeightInPoints((short) 10);// 设置字体大小
        fontStyle2.setFont(font2);
        fontStyle2.setBorderBottom(XSSFCellStyle.BORDER_THIN); // 下边框
        fontStyle2.setBorderLeft(XSSFCellStyle.BORDER_THIN);// 左边框
        fontStyle2.setBorderTop(XSSFCellStyle.BORDER_THIN);// 上边框
        fontStyle2.setBorderRight(XSSFCellStyle.BORDER_THIN);// 右边框
        fontStyle2.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 居中
    }
    /**
     * 根据ExcelMapping 生成列头(多行列头)
     *
     * @param sheet 工作簿
     * @param map 每行每个单元格对应的列头信息
     */
    public static final void createTableHeader(XSSFSheet sheet, Map<Integer, List<ExcelBean>> map) {
        int startIndex=0;//cell起始位置
        int endIndex=0;//cell终止位置
        for (Map.Entry<Integer, List<ExcelBean>> entry : map.entrySet()) {
            XSSFRow row = sheet.createRow(entry.getKey());
            List<ExcelBean> excels = entry.getValue();
            for (int x = 0; x < excels.size(); x++) {
                //合并单元格
                if(excels.get(x).getCols()>1){
                    if(x==0){
                        endIndex+=excels.get(x).getCols()-1;
                        CellRangeAddress range=new CellRangeAddress(0,0,startIndex,endIndex);
                        sheet.addMergedRegion(range);
                        startIndex+=excels.get(x).getCols();
                    }else{
                        endIndex+=excels.get(x).getCols();
                        CellRangeAddress range=new CellRangeAddress(0,0,startIndex,endIndex);
                        sheet.addMergedRegion(range);
                        startIndex+=excels.get(x).getCols();
                    }
                    XSSFCell cell = row.createCell(startIndex-excels.get(x).getCols());
                    cell.setCellValue(excels.get(x).getHeadTextName());// 设置内容
                    if (excels.get(x).getCellStyle() != null) {
                        cell.setCellStyle(excels.get(x).getCellStyle());// 设置格式
                    }
                    cell.setCellStyle(fontStyle);
                }else{
                    XSSFCell cell = row.createCell(x);
                    cell.setCellValue(excels.get(x).getHeadTextName());// 设置内容
                    if (excels.get(x).getCellStyle() != null) {
                        cell.setCellStyle(excels.get(x).getCellStyle());// 设置格式
                    }
                    cell.setCellStyle(fontStyle);
                }
            }
        }
    }
    public static void createTableRows(XSSFSheet sheet, Map<Integer, List<ExcelBean>> map, List objs, Class clazz)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException,
    ClassNotFoundException, ParseException {
        int rowindex = map.size();
        int maxKey = 0;
        List<ExcelBean> ems = new ArrayList<>();
        for (Map.Entry<Integer, List<ExcelBean>> entry : map.entrySet()) {
            if (entry.getKey() > maxKey) {
                maxKey = entry.getKey();
            }
        }
        ems = map.get(maxKey);
        List<Integer> widths = new ArrayList<Integer>(ems.size());
        for (Object obj : objs) {
            XSSFRow row = sheet.createRow(rowindex);
            for (int i = 0; i < ems.size(); i++) {
                ExcelBean em = (ExcelBean) ems.get(i);
                // 获得get方法
                PropertyDescriptor pd = new PropertyDescriptor(em.getPropertyName(), clazz);
                Method getMethod = pd.getReadMethod();
                Object rtn = getMethod.invoke(obj);
                String value = "";
                // 如果是日期类型进行转换
                if (rtn != null) {
                    if (rtn instanceof Date) {
                        value = DateUtils.formatDate((Date)rtn,"yyyy-MM-dd");
                    } else if(rtn instanceof BigDecimal){
                        NumberFormat nf = new DecimalFormat("#,##0.00");
                        value=nf.format((BigDecimal)rtn).toString();
                    } else if((rtn instanceof Integer) && (Integer.valueOf(rtn.toString())<0 )){
                        value="--";
                    }else {
                        value = rtn.toString();
                    }
                }
                XSSFCell cell = row.createCell(i);
                cell.setCellValue(value);
                cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                cell.setCellStyle(fontStyle2);
                // 获得最大列宽
                int width = value.getBytes().length * 300;
                // 还未设置,设置当前
                if (widths.size() <= i) {
                    widths.add(width);
                    continue;
                }
                // 比原来大,更新数据
                if (width > widths.get(i)) {
                    widths.set(i, width);
                }
            }
            rowindex++;
        }
        // 设置列宽
        for (int index = 0; index < widths.size(); index++) {
            Integer width = widths.get(index);
            width = width < 2500 ? 2500 : width + 300;
            width = width > 10000 ? 10000 + 300 : width + 300;
            sheet.setColumnWidth(index, width);
        }
    }
}
```

## 相关资源:
- [POI读写海量Excel(详细解读)](https://download.csdn.net/download/yaoyaomeiying/5310121?spm=1001.2101.3001.5697)
- [POI内存泄露快速写入](http://poi.apache.org/components/spreadsheet/how-to.html#sxssf)
- [POI官网](http://poi.apache.org/index.html)