Search in sources :

Example 1 with ExcelOperBaseException

use of com.jeesuite.common2.excel.ExcelOperBaseException in project jeesuite-libs by vakinge.

the class ExcelBeanHelper method setRowValues.

public static <T> List<T> setRowValues(Class<T> clazz, List<String> contents) {
    try {
        Map<String, PropertyDescriptor> pds = getAliasPropertyDescriptors(clazz);
        List<T> results = new ArrayList<>();
        if (contents.isEmpty())
            return results;
        String[] titles = getExcelMeta(clazz).getFinalTitles();
        int titleRowCount = getExcelMeta(clazz).getTitleRowNum();
        // 解析内容标题内容为数组
        List<String> contentTitles = new ArrayList<>();
        // 第一行为sheet信息
        for (int i = 1; i <= titleRowCount; i++) {
            contentTitles.addAll(Arrays.asList(contents.get(i).split(ExcelValidator.FIELD_SPLIT)));
        }
        for (int i = 0; i < titles.length; i++) {
            if (titleRowCount == 1) {
                if (!StringUtils.equals(titles[i], contentTitles.get(i)))
                    throw new ExcelOperBaseException("格式错误,没有找到列[" + titles[i] + "]");
            } else {
                if (!contentTitles.contains(titles[i]))
                    throw new ExcelOperBaseException("格式错误,没有找到列[" + titles[i] + "]");
            }
        }
        String[] vals = null;
        for (int i = titleRowCount + 1; i < contents.size(); i++) {
            String line = contents.get(i);
            if (line.startsWith(ExcelValidator.SHEET_NAME_PREFIX)) {
                throw new ExcelOperBaseException("模板错误(暂不支持多个sheet)");
            }
            T instance = clazz.newInstance();
            vals = line.split(ExcelValidator.FIELD_SPLIT);
            boolean anyColumnNotEmpty = false;
            inner: for (int j = 0; j < titles.length; j++) {
                if (vals.length < j + 1)
                    break inner;
                anyColumnNotEmpty = anyColumnNotEmpty || StringUtils.isNotBlank(vals[j]);
                PropertyDescriptor propertyDescriptor = pds.get(clearWrapper(titles[j]).trim());
                if (propertyDescriptor != null && vals[j] != null) {
                    try {
                        Object rawValue = rawValue(vals[j], propertyDescriptor.getPropertyType());
                        propertyDescriptor.getWriteMethod().invoke(instance, rawValue);
                    } catch (Exception e) {
                    // TODO: handle exception
                    }
                }
            }
            if (anyColumnNotEmpty) {
                results.add(instance);
            }
        }
        return results;
    } catch (Exception e) {
        if (e instanceof ExcelOperBaseException)
            throw (ExcelOperBaseException) e;
        throw new BeanConverterException(e);
    }
}
Also used : ExcelOperBaseException(com.jeesuite.common2.excel.ExcelOperBaseException) PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) ExcelOperBaseException(com.jeesuite.common2.excel.ExcelOperBaseException) IntrospectionException(java.beans.IntrospectionException)

Example 2 with ExcelOperBaseException

use of com.jeesuite.common2.excel.ExcelOperBaseException in project jeesuite-libs by vakinge.

the class ExcelPerfModeReader method readAsXLSX.

private List<String> readAsXLSX(String path) {
    OPCPackage opcPackage = null;
    try {
        opcPackage = OPCPackage.open(path, PackageAccess.READ);
        XLSX2CSV xlsx2csv = new XLSX2CSV(opcPackage, System.out, -1);
        return xlsx2csv.process();
    } catch (Exception e) {
        if (e instanceof OLE2NotOfficeXmlFileException || e instanceof NotOLE2FileException || e instanceof NotOfficeXmlFileException || e instanceof OfficeXmlFileException) {
            throw new ExcelOperBaseException("请选择正确格式excel文件");
        }
        if (e instanceof IOException) {
            throw new ExcelOperBaseException("文件读取失败");
        }
        if (e instanceof InvalidOperationException) {
            throw new ExcelOperBaseException(e);
        }
        throw new RuntimeException(e);
    } finally {
        try {
            opcPackage.close();
        } catch (Exception e) {
        }
    }
}
Also used : NotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException) OLE2NotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException) OfficeXmlFileException(org.apache.poi.poifs.filesystem.OfficeXmlFileException) NotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException) OLE2NotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException) NotOLE2FileException(org.apache.poi.poifs.filesystem.NotOLE2FileException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) OLE2NotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException) IOException(java.io.IOException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XLSX2CSV(com.jeesuite.common2.excel.convert.XLSX2CSV) OfficeXmlFileException(org.apache.poi.poifs.filesystem.OfficeXmlFileException) NotOLE2FileException(org.apache.poi.poifs.filesystem.NotOLE2FileException) IOException(java.io.IOException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) NotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException) OLE2NotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException)

Aggregations

ExcelOperBaseException (com.jeesuite.common2.excel.ExcelOperBaseException)1 XLSX2CSV (com.jeesuite.common2.excel.convert.XLSX2CSV)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)1 NotOfficeXmlFileException (org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException)1 OLE2NotOfficeXmlFileException (org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException)1 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)1 NotOLE2FileException (org.apache.poi.poifs.filesystem.NotOLE2FileException)1 OfficeXmlFileException (org.apache.poi.poifs.filesystem.OfficeXmlFileException)1