Comparte una clase de herramientas de Excel, muy fácil de usar

Uno, la pieza de código

Comparta una clase de herramientas de Excel, no diga tonterías, solo vaya al código:

package com.standard.commonutil.util;

import com.google.common.collect.Lists;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.List;

/**
 * Excel工具类
 *
 * @author weiyuan
 * @version 1.0
 * @date 2016/6/19
 */
public class ExcelUtil {
    
    
    /**
     * 读取excel, 以List<List<String>>数据结构返回
     *
     * @param filepath
     * @return
     */
    public static List<List<String>> readExcel(String filepath) {
    
    
        return readExcel(new File(filepath));
    }

    /**
     * 读取excel,以List<List<String>>数据结构返回
     *
     * @param file
     * @return
     */
    public static List<List<String>> readExcel(File file) {
    
    
        return analyData(getWorkbook(file));
    }

    /**
     * 读取数据
     *
     * @param wb
     * @return
     */
    private static List<List<String>> analyData(Workbook wb) {
    
    
        Sheet sheet = null;
        Row row = null;
        List<List<String>> sheetList = Lists.newArrayList();
        String cellData = null;
        if (wb != null) {
    
    
            sheet = wb.getSheetAt(0);//获取第一个sheet
            int rownum = sheet.getPhysicalNumberOfRows();//获取最大行数
            row = sheet.getRow(0);//获取第一行
            int colnum = row.getPhysicalNumberOfCells();//获取最大列数
            for (int i = 0; i < rownum; i++) {
    
    
                List<String> rowlist = Lists.newArrayList();
                row = sheet.getRow(i);
                for (int j = 0; j < colnum; j++) {
    
    
                    cellData = (String) getCellFormatValue(row.getCell(j));
                    rowlist.add(cellData);
                }
                sheetList.add(rowlist);
            }
        }
        return sheetList;
    }

    /**
     * 读取excel
     *
     * @param file
     * @return
     */
    private static Workbook getWorkbook(File file) {
    
    
        Workbook wb = null;
        String filename = file.getName();
        String extString = filename.substring(filename.lastIndexOf("."));
        InputStream is = null;
        try {
    
    
            is = new FileInputStream(file);
            if (".xls".equals(extString)) {
    
    
                return wb = new HSSFWorkbook(is);
            } else if (".xlsx".equals(extString)) {
    
    
                return wb = new XSSFWorkbook(is);
            } else {
    
    
                return wb = null;
            }
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        return wb;
    }

    /**
     * 获取单元格数据
     *
     * @param cell
     * @return
     */
    private static Object getCellFormatValue(Cell cell) {
    
    
        Object cellValue = null;
        if (cell != null) {
    
    
            switch (cell.getCellType()) {
    
    //判断cell类型
                case Cell.CELL_TYPE_NUMERIC: {
    
    
                    cellValue = String.valueOf(cell.getNumericCellValue());
                    break;
                }
                case Cell.CELL_TYPE_FORMULA: {
    
    //判断cell是否为日期格式
                    if (DateUtil.isCellDateFormatted(cell)) {
    
    //转换为日期格式YYYY-mm-dd
                        cellValue = cell.getDateCellValue();
                    } else {
    
    //数字
                        cellValue = String.valueOf(cell.getNumericCellValue());
                    }
                    break;
                }
                case Cell.CELL_TYPE_STRING: {
    
    
                    cellValue = cell.getRichStringCellValue().getString();
                    break;
                }
                default:
                    cellValue = "";
            }
        } else {
    
    
            cellValue = "";
        }
        return cellValue;
    }
}

Segundo, dependencia de maven

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.12</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>20.0</version>
</dependency>

Tres, finalmente

Si no te lo quieres quitar, dale una recompensa. Si no le das una recompensa, al menos presta atención al blogger, jaja, jaja, jeje.

Supongo que te gusta

Origin blog.csdn.net/datuanyuan/article/details/109101195
Recomendado
Clasificación