kimi
2020-05-27 c007f0ca1785db093d48f4846cda82fe8e955765
src/main/java/com/highdatas/mdm/util/ExcelUtil.java
@@ -13,6 +13,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -39,49 +41,49 @@
        List list = new ArrayList<>();
        Workbook work = null;
        try{
        // 创建excel工作簿
        work = getWorkbook(in, fileName);
        if (null == work) {
            throw new Exception("创建Excel工作薄为空!");
        }
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;
        for (int i = 0; i < work.getNumberOfSheets(); i++) {
            sheet = work.getSheetAt(i);
            if(sheet == null) {
                continue;
        try {
            // 创建excel工作簿
            work = getWorkbook(in, fileName);
            if (null == work) {
                throw new Exception("创建Excel工作薄为空!");
            }
            // 滤过第一行标题
            for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                row = sheet.getRow(j);
                if (row == null || row.getFirstCellNum() == j) {
            Sheet sheet = null;
            Row row = null;
            Cell cell = null;
            for (int i = 0; i < work.getNumberOfSheets(); i++) {
                sheet = work.getSheetAt(i);
                if (sheet == null) {
                    continue;
                }
                ViewMappingItem item = new ViewMappingItem();
                Cell preCell = row.getCell(0);
                if (preCell != null) {
                    item.setPre(preCell.getStringCellValue());
                }
                // 滤过第一行标题
                for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                    row = sheet.getRow(j);
                    if (row == null || row.getFirstCellNum() == j) {
                        continue;
                    }
                Cell fixCell = row.getCell(1);
                if (fixCell != null) {
                    item.setFix(fixCell.getStringCellValue());
                }
                    ViewMappingItem item = new ViewMappingItem();
                    Cell preCell = row.getCell(0);
                    if (preCell != null) {
                        item.setPre(preCell.getStringCellValue());
                    }
                list.add(item);
                    Cell fixCell = row.getCell(1);
                    if (fixCell != null) {
                        item.setFix(getValByCell(fixCell).toString());
                    }
                    list.add(item);
                }
            }
        }
        }catch (Exception e) {
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
        } finally {
            try {
                work.close();
            } catch (IOException e) {
@@ -91,8 +93,48 @@
        return list;
    }
    private static Object getValByCell(Cell cell) {
        if (cell == null) {
            return "";
        }
        Object obj = null;
        switch (cell.getCellTypeEnum()) {
            case BOOLEAN:
                obj = cell.getBooleanCellValue();
                break;
            case ERROR:
                obj = cell.getErrorCellValue();
                break;
            case FORMULA:
                try {
                    obj = String.valueOf(cell.getStringCellValue());
                } catch (IllegalStateException e) {
                    String valueOf = String.valueOf(cell.getNumericCellValue());
                    BigDecimal bd = new BigDecimal(Double.valueOf(valueOf));
                    bd = bd.setScale(2, RoundingMode.HALF_UP);
                    obj = bd;
                }
                break;
            case NUMERIC:
                obj = cell.getNumericCellValue();
                break;
            case STRING:
                String value = String.valueOf(cell.getStringCellValue());
                value = value.replace(" ", "");
                value = value.replace("\n", "");
                value = value.replace("\t", "");
                obj = value;
                break;
            default:
                break;
        }
        return obj;
    }
    /**
     * 判断文件格式
     *
     * @param in
     * @param fileName
     * @return
@@ -102,7 +144,7 @@
        Workbook book = null;
        String filetype = fileName.substring(fileName.lastIndexOf("."));
        if(".xls".equals(filetype)) {
        if (".xls".equals(filetype)) {
            book = new HSSFWorkbook(in);
        } else if (".xlsx".equals(filetype)) {
            book = new XSSFWorkbook(in);
@@ -134,7 +176,7 @@
        //循环字段名数组,创建标题行
        Row row = sheet.createRow(0);
        for (int j = 0; j< fieldList.size(); j++){
        for (int j = 0; j < fieldList.size(); j++) {
            //创建列
            SysField sysField = fieldList.get(j);
            Cell cell = row.createCell(j);
@@ -143,13 +185,13 @@
            cell.setCellValue(sysField.getAlias());
        }
        //创建普通行
        for (int i = 0;i<datalist.size();i++){
        for (int i = 0; i < datalist.size(); i++) {
            //因为第一行已经用于创建标题行,故从第二行开始创建
            row = sheet.createRow(i+1);
            row = sheet.createRow(i + 1);
            //如果是第一行就让其为标题行
            Map<String, Object> oneData = datalist.get(i);
            for (int j = 0;j<fieldList.size();j++){
            for (int j = 0; j < fieldList.size(); j++) {
                //创建列
                SysField sysField = fieldList.get(j);
@@ -164,7 +206,7 @@
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-type", "application/octet-stream;charset=UTF-8");
        //默认Excel名称
        response.setHeader("Content-Disposition", "attachment;fileName="+ URLEncoder.encode("mapping数据.xlsx","UTF-8"));
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("mapping数据.xlsx", "UTF-8"));
        try {
            response.flushBuffer();
@@ -175,15 +217,15 @@
    }
    private static String transCellType(Object value){
    private static String transCellType(Object value) {
        String str = null;
        if (value instanceof Date){
        if (value instanceof Date) {
            Date date = (Date) value;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            str = sdf.format(date);
        }else{
        } else {
            str = String.valueOf(value);
            if (str == "null"){
            if (str == "null") {
                str = "";
            }
        }