ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

excel文件导入到mysql数据库中

2021-11-16 16:58:00  阅读:150  来源: 互联网

标签:导入到 return excel else cell CellType user mysql import


废话不多说,直接上代码
excel导入的工具类



import cn.samples.common.utils.DateUtils;
import cn.samples.web.entity.BGoodsInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTMarker;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author
 * @version 1.0
 */
public class ReadPatientExcelUtil {
    //总行数
    private static int totalRows = 0;
    //总条数
    private static int totalCells = 0;
    //错误信息接收器
    private static String errorMsg;

    /**
     * 读EXCEL文件,获取信息集合
     *
     * @return
     */
    public static List<BGoodsInfo> getExcelInfo(MultipartFile mFile) {
        String fileName = mFile.getOriginalFilename();//获取文件名
        try {
            if (!validateExcel(fileName)) {// 验证文件名是否合格
                return null;
            }
            boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本
            if (isExcel2007(fileName)) {
                isExcel2003 = false;
            }
            List<BGoodsInfo> userList = createExcel(mFile.getInputStream(), isExcel2003);
            return userList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据excel里面的内容读取客户信息
     *
     * @param is          输入流
     * @param isExcel2003 excel是2003还是2007版本
     * @return
     * @throws IOException
     */
    public static List<BGoodsInfo> createExcel(InputStream is, boolean isExcel2003) {
        try {
            Workbook wb = null;
            if (isExcel2003) {// 当excel是2003时,创建excel2003
                wb = new HSSFWorkbook(is);
            } else {// 当excel是2007时,创建excel2007
                wb = new XSSFWorkbook(is);
            }
            List<BGoodsInfo> userList = readExcelValue(wb);// 读取Excel里面客户的信息
            return userList;
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读取Excel里面客户的信息
     *
     * @param wb
     * @return
     */
    private static List<BGoodsInfo> readExcelValue(Workbook wb) throws ParseException {
        //默认会跳过第一行标题
        // 得到第一个shell
        Sheet sheet = wb.getSheetAt(0);
        // 得到Excel的行数
        totalRows = sheet.getPhysicalNumberOfRows();
        // 得到Excel的列数(前提是有行数)
        if (totalRows > 1 && sheet.getRow(0) != null) {
            totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
        }
        List<BGoodsInfo> userList = new ArrayList<BGoodsInfo>();
        // 循环Excel行数
        for (int r = 1; r < totalRows; r++) {
            Row row = sheet.getRow(r);
            if (row == null) {
                continue;
            }
            BGoodsInfo user = new BGoodsInfo();
            // 循环Excel的列
            for (int c = 0; c < totalCells ; c++) {
                Cell cell = row.getCell(c);
                if (null != cell) {
                    if (c == 0) {           //第一列
                        //如果是纯数字,将单元格类型转为String
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //商品编码
                        user.setGCode(cell.getStringCellValue());//将单元格数据赋值给user
                    } else if (c == 1) {
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //商品名称
                        user.setGName(cell.getStringCellValue());
                    }else if (c == 2) {
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //条形码
                        user.setGBarCode(cell.getStringCellValue());
                    } else if (c == 3) {
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //区内企业代码
                        user.setTradeCode(cell.getStringCellValue());
                    } else if (c == 4) {
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //区内企业名称
                        user.setTradeName(cell.getStringCellValue());
                    } else if (c == 5) {
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //商品规格/型号
                        user.setGModel(cell.getStringCellValue());
                    } else if (c == 6) {
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //商品料号
                        user.setGItemNo(cell.getStringCellValue());
                    } else if (c == 7) {
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //申报计量单位
                        user.setGoodsUnit(cell.getStringCellValue());
                    } else if (c == 8) {
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //识别商品码
                        user.setGDiscernCode(cell.getStringCellValue());
                    }else if (c == 9) {
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //识别商品名称
                        user.setGDiscernName(cell.getStringCellValue());
                    }else if (c == 10) {
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //申报人
                        user.setDeclareUser(cell.getStringCellValue());
                    }else if (c == 11) {
                        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                            cell.setCellType(CellType.STRING);
                        }
                        //申报时间
                        String stringCellValue = cell.getStringCellValue();
                        Date date = DateUtils.stringToDate(stringCellValue, "yyyy-MM-dd HH:mm:ss");
                        user.setDeclareTime(date);
                    }
                }
            }
            //将excel解析出来的数据赋值给对象添加到list中
            user.setCreateTime(new Date());
            // 添加到list
            userList.add(user);
        }
        return userList;
    }

    /**
     * 验证EXCEL文件
     *
     * @param filePath
     * @return
     */
    public static boolean validateExcel(String filePath) {
        if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
            errorMsg = "文件名不是excel格式";
            return false;
        }
        return true;
    }

    // @描述:是否是2003的excel,返回true是2003
    public static boolean isExcel2003(String filePath) {
        return filePath.matches("^.+\\.(?i)(xls)$");
    }

    //@描述:是否是2007的excel,返回true是2007
    public static boolean isExcel2007(String filePath) {
        return filePath.matches("^.+\\.(?i)(xlsx)$");
    }
}

h

对于插入的数据进行验证

 public void importExcel(MultipartFile file) throws IOException {
        List<BGoodsInfo> excelInfo = ReadPatientExcelUtil.getExcelInfo(file);
        for (BGoodsInfo s : excelInfo) {
            QueryWrapper<BGoodsInfo> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(BGoodsInfo::getGCode, s.getGCode());
            BGoodsInfo list = getOne(wrapper);
            if (list == null) {
                log.info("该商品没有被备案过,开始备案开始{}", s);
                save(s);
                continue;
            } else {
                LambdaUpdateWrapper<BGoodsInfo> set = new LambdaUpdateWrapper<BGoodsInfo>().eq(BGoodsInfo::getId, list.getId());
                log.info("该商品已经备案过,修改该商品备案的数据开始{}", s);
                update(s, set);
            }
        }
    }

标签:导入到,return,excel,else,cell,CellType,user,mysql,import
来源: https://blog.csdn.net/weixin_44151705/article/details/121359814

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有