<commit_before>package com.github.abel533.easyxls;

import com.github.abel533.easyxls.bean.DlExcel;
import com.github.abel533.easyxls.common.DateUtil;
import com.github.abel533.easyxls.common.XmlConfig;
import com.github.abel533.easyxls.generater.GenXml;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.*;

import java.io.File;
import java.io.InputStream;
import java.lang.Boolean;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

/**
 * excel，对导入导出进行封装
 *
 * @author liuzh
 */
public class EasyXls {

    public static final String EXCEL = ".xls";
    private static Map<String, DlExcel> cache = new HashMap<String, DlExcel>();

    /**
     * 获取xml配置对象
     *
     * @param xmlPath xml完整路径
     * @return xml配置对象
     */
    private static DlExcel getDlExcel(String xmlPath) {
        DlExcel dlExcel = cache.get(xmlPath);
        if (dlExcel == null) {
            dlExcel = XmlConfig.getXmlConfig(xmlPath);
        }
        if (dlExcel == null) {
            throw new RuntimeException("获取xml配置文件出错!");
        }
        if (dlExcel.getCache() == null || dlExcel.getCache()) {
            cache.put(xmlPath, dlExcel);
        }
        return dlExcel;
    }

    /**
     * 打开代码生成器，请在项目中执行，只有这样才能加载相应的类
     */
    public static void openGenerater() {
        GenXml.run();
    }

    /**
     * 导入xml到List
     *
     * @param xmlPath xml完整路径
     * @param xlsFile xls文件路径
     * @return List对象
     * @throws Exception
     */
    public static List<?> xls2List(String xmlPath, File xlsFile) throws Exception {
        List<Object> list = new ArrayList<Object>();
        try {
            //获取配置文件
            DlExcel config = getDlExcel(xmlPath);
            String[] names = config.getNames();
            String[] types = config.getTypes();

            Workbook wb = Workbook.getWorkbook(xlsFile);
            Sheet sheet = wb.getSheet(config.getSheetNum());

            for (int i = config.getStartRow(); i < sheet.getRows(); i++) {
                Object obj = Class.forName(config.getClazz()).newInstance();
                for (int j = 0; j < sheet.getColumns(); j++) {
                    setValue(obj, names[j], types[j], sheet.getCell(j, i).getContents());
                }
                list.add(obj);
            }
        } catch (Exception e) {
            throw new Exception("转换xls出错:" + e.getMessage());
        }
        return list;
    }

    /**
     * 导入xml到List
     *
     * @param xmlPath     xml完整路径
     * @param inputStream xls文件流
     * @return List对象
     * @throws Exception
     */
    public static List<?> xls2List(String xmlPath, InputStream inputStream) throws Exception {
        List<Object> list = new ArrayList<Object>();
        try {
            //获取配置文件
            DlExcel config = getDlExcel(xmlPath);
            String[] names = config.getNames();
            String[] types = config.getTypes();

            Workbook wb = Workbook.getWorkbook(inputStream);
            Sheet sheet = wb.getSheet(config.getSheetNum());

            for (int i = config.getStartRow(); i < sheet.getRows(); i++) {
                Object obj = Class.forName(config.getClazz()).newInstance();
                for (int j = 0; j < sheet.getColumns(); j++) {
                    setValue(obj, names[j], types[j], sheet.getCell(j, i).getContents());
                }
                list.add(obj);
            }
            wb.close();
        } catch (Exception e) {
            throw new Exception("转换xls出错:" + e.getMessage());
        }
        return list;
    }

    /**
     * 跟对象obj的某个field赋值value
     *
     * @param obj       属性对象
     * @param fieldName 字段名
     * @param value     字段值
     * @throws Exception
     */
    private static void setValue(Object obj, String fieldName, String type, String value) throws Exception {
        Object val = null;
        if (value != null) {
            value = value.trim();
            /**
             * 对类型进行转换，支持int,long,float,double,boolean,Integer,Long,Double,Float,Date,String<commit_msg>空值时设置null<commit_after>package com.github.abel533.easyxls;

import com.github.abel533.easyxls.bean.DlExcel;
import com.github.abel533.easyxls.common.DateUtil;
import com.github.abel533.easyxls.common.XmlConfig;
import com.github.abel533.easyxls.generater.GenXml;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.*;

import java.io.File;
import java.io.InputStream;
import java.lang.Boolean;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

/**
 * excel，对导入导出进行封装
 *
 * @author liuzh
 */
public class EasyXls {

    public static final String EXCEL = ".xls";
    private static Map<String, DlExcel> cache = new HashMap<String, DlExcel>();

    /**
     * 获取xml配置对象
     *
     * @param xmlPath xml完整路径
     * @return xml配置对象
     */
    private static DlExcel getDlExcel(String xmlPath) {
        DlExcel dlExcel = cache.get(xmlPath);
        if (dlExcel == null) {
            dlExcel = XmlConfig.getXmlConfig(xmlPath);
        }
        if (dlExcel == null) {
            throw new RuntimeException("获取xml配置文件出错!");
        }
        if (dlExcel.getCache() == null || dlExcel.getCache()) {
            cache.put(xmlPath, dlExcel);
        }
        return dlExcel;
    }

    /**
     * 打开代码生成器，请在项目中执行，只有这样才能加载相应的类
     */
    public static void openGenerater() {
        GenXml.run();
    }

    /**
     * 导入xml到List
     *
     * @param xmlPath xml完整路径
     * @param xlsFile xls文件路径
     * @return List对象
     * @throws Exception
     */
    public static List<?> xls2List(String xmlPath, File xlsFile) throws Exception {
        List<Object> list = new ArrayList<Object>();
        try {
            //获取配置文件
            DlExcel config = getDlExcel(xmlPath);
            String[] names = config.getNames();
            String[] types = config.getTypes();

            Workbook wb = Workbook.getWorkbook(xlsFile);
            Sheet sheet = wb.getSheet(config.getSheetNum());

            for (int i = config.getStartRow(); i < sheet.getRows(); i++) {
                Object obj = Class.forName(config.getClazz()).newInstance();
                for (int j = 0; j < sheet.getColumns(); j++) {
                    setValue(obj, names[j], types[j], sheet.getCell(j, i).getContents());
                }
                list.add(obj);
            }
        } catch (Exception e) {
            throw new Exception("转换xls出错:" + e.getMessage());
        }
        return list;
    }

    /**
     * 导入xml到List
     *
     * @param xmlPath     xml完整路径
     * @param inputStream xls文件流
     * @return List对象
     * @throws Exception
     */
    public static List<?> xls2List(String xmlPath, InputStream inputStream) throws Exception {
        List<Object> list = new ArrayList<Object>();
        try {
            //获取配置文件
            DlExcel config = getDlExcel(xmlPath);
            String[] names = config.getNames();
            String[] types = config.getTypes();

            Workbook wb = Workbook.getWorkbook(inputStream);
            Sheet sheet = wb.getSheet(config.getSheetNum());

            for (int i = config.getStartRow(); i < sheet.getRows(); i++) {
                Object obj = Class.forName(config.getClazz()).newInstance();
                for (int j = 0; j < sheet.getColumns(); j++) {
                    setValue(obj, names[j], types[j], sheet.getCell(j, i).getContents());
                }
                list.add(obj);
            }
            wb.close();
        } catch (Exception e) {
            throw new Exception("转换xls出错:" + e.getMessage());
        }
        return list;
    }

    /**
     * 跟对象obj的某个field赋值value
     *
     * @param obj       属性对象
     * @param fieldName 字段名
     * @param value     字段值
     * @throws Exception
     */
    private static void setValue(Object obj, String fieldName, String type, String value) throws Exception {
        Object val = null;
        if (value != null && !value.trim().equals("")) {
            value = value.trim();
            /**
             * 对类型进行转换，支持int,long,float,double,boolean,Integer,Long,Double,Float,Date,String
{'fraction_non_alphanumeric': 0.12518573551263001, 'fraction_numerical': 0.0047052996532937095, 'mean_word_length': 2.0014864362690448, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 38}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1212545', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2609, 'n_words': 536}