解析Excel

Posted 不知为何就叫呵呵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解析Excel相关的知识,希望对你有一定的参考价值。

package com.jpcar.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.jpcar.model.entity.ToolboxValue;

public class ExcelUtil {
    private static final char UNDERLINE = ‘_‘;

    public static void main(String[] args) throws Exception {
    InputStream in = new FileInputStream(new File("F:\\test\\config\\toolbox_value.xlsx"));
    List<ToolboxValue> list = parse(in, ToolboxValue.class);
    System.out.println(list);
    }
    
    
    public static <T> List<T> parse(InputStream in, Class<T> cla) throws Exception {
    return parse(in, cla, null);
    }
    /**
     * 
     * @param in
     * @param cla
     * @param mapper
     *            Map<FieldName, ExcelName>
     * @return
     * @throws IOException
     */
    public static <T> List<T> parse(InputStream in, Class<T> cla, Map<String, String> mapper) throws Exception {
    List<T> list = new ArrayList<>();
    Field[] fs = cla.getDeclaredFields();
    Map<String, Field> map = new HashMap<>();
    for (Field f : fs) {
        String fn = f.getName();
        if (null != mapper) {
        fn = mapper.get(fn);
        if (null == fn)
            fn = camelToUnderline(f.getName());
        } else {
        fn = camelToUnderline(fn);
        }
        map.put(fn, f);
    }

    XSSFWorkbook xssfWorkbook = new XSSFWorkbook(in);
    XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);

    int rowstart = xssfSheet.getFirstRowNum();
    int rowEnd = xssfSheet.getLastRowNum();
    int cellSize = xssfSheet.getRow(0).getPhysicalNumberOfCells();
    List<String> keys = new ArrayList<>();
    for (int i = rowstart; i <= rowEnd; i++) {
        if (i == 0) {
        XSSFRow row = xssfSheet.getRow(0);
        for (int j = 0; j < cellSize; j++) {
            String key = row.getCell(j).getStringCellValue();
            keys.add(key);
        }
        continue;
        }
        T t = cla.newInstance();
        XSSFRow row = xssfSheet.getRow(i);
        for (int j = 0; j < cellSize; j++) {
        String str = keys.get(j);
        Field f = map.get(str);
        f.setAccessible(true);
        String type = f.getType().getName();
        XSSFCell cell = row.getCell(j);
        if (type.equals("int") || "java.lang.Integer".equals(type)) {
            int val = (int) cell.getNumericCellValue();
            f.set(t, val);
        } else if ("double".equals(type) || "java.lang.Double".equals(type)) {
            double val = cell.getNumericCellValue();
            f.set(t, val);
        } else if ("java.lang.String".equals(type)) {
            String val = "";
            if (cell.getCellType() != Cell.CELL_TYPE_STRING) {
            val = (int)cell.getNumericCellValue() + "";
            } else {
            val = cell.getStringCellValue();
            }
            f.set(t, val);
        } else if ("boolean".equals(type) || "java.lang.Boolean".equals(type)) {
            boolean val = cell.getBooleanCellValue();
            f.set(t, val);
        } else if ("java.util.Date".equals(type)) {
            Date val = cell.getDateCellValue();
            f.set(t, val);
        }
        }
        list.add(t);
    }

    return list;
    }

    public static String underlineToCamel(String str) {
    if (str == null || "".equals(str.trim())) {
        return "";
    }
    int len = str.length();
    StringBuilder sb = new StringBuilder(len);
    for (int i = 0; i < len; i++) {
        char c = str.charAt(i);
        if (c == UNDERLINE) {
        if (++i < len) {
            sb.append(Character.toUpperCase(str.charAt(i)));
        }
        } else {
        sb.append(c);
        }
    }
    return sb.toString();
    }

    public static String camelToUnderline(String param) {
    if (param == null || "".equals(param.trim())) {
        return "";
    }
    int len = param.length();
    StringBuilder sb = new StringBuilder(len);
    for (int i = 0; i < len; i++) {
        char c = param.charAt(i);
        if (Character.isUpperCase(c)) {
        sb.append(UNDERLINE);
        sb.append(Character.toLowerCase(c));
        } else {
        sb.append(c);
        }
    }
    return sb.toString();
    }
}

 

以上是关于解析Excel的主要内容,如果未能解决你的问题,请参考以下文章

无法解析片段中的 ViewModelProvider 构造?

Relay.js 没有正确解析组合片段

Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段

POI解析Excel代码

从 XML 声明片段获取 XML 编码:部分内容解析不支持 XmlDeclaration

Java代码工具箱之解析Excel