poi import excel file, date format processing.

poi timing tasks import excel to the database

It took some time for the timing import function of the excel file under the actual timing task. Because the format of the inserted file is different, the enumeration can be customized to distinguish it from the corresponding file name.
poi version is 4.1.2

在这里插入代码片
``package com.ruoyi.base.task;

import com.ruoyi.base.constants.FileTypeEnum;
import com.ruoyi.system.domain.MfhfTest;
import com.ruoyi.system.domain.TestDataInfo;
import com.ruoyi.system.service.IMfhfTestService;
import com.ruoyi.system.service.ITestDataInfoService;
import com.ruoyi.web.controller.common.CommonController;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
@Configuration
@EnableScheduling
public class FileTask {

//此处是配置文件种配置的要扫描文件的路径。
    @Value("${file.uploadUrl}")//获取文件的存储路径
    private String uploadUrl;

    @Value("${file.removeUrl}")//获取文件要移除路径
    private String removeUrl;

    @Autowired
    private ITestDataInfoService itestDataInfoService;

    @Autowired
    private IMfhfTestService iMfhfTestService;

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);


    private final static String excel2003L =".xls";    //2003- 版本的excel
    private final static String excel2007U =".xlsx";   //2007+ 版本的excel



/**
     * @Description:获取IO流中的数据,组装成List<List<Object>>对象
     * @param in,fileName
     * @return
     * @throws IOException
     */



    public static List<List<Object>> getListByExcel(InputStream in, String fileName,String strName) throws Exception{
        List<List<Object>> list = null;

        //创建Excel工作薄
        Workbook work = getWorkbook(in,fileName);
        if(null == work){
            throw new Exception("创建Excel工作薄为空!");
        }
        Sheet sheet = null;  //页数
        Row row = null;  //行数
        Cell cell = null;  //列数

        list = new ArrayList<List<Object>>();
        //遍历Excel中所有的sheet
        if (strName.contains(FileTypeEnum.DSI.getFileType())){
            for (int i = 0; i < work.getNumberOfSheets(); i++) {
                sheet = work.getSheetAt(i);
                if(sheet==null){continue;}

                //遍历当前sheet中的所有行
                for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                    row = sheet.getRow(j);
                    if(row==null||row.getFirstCellNum()==j){continue;}

                    //遍历所有的列
                    List<Object> li = new ArrayList<Object>();
                    for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                        cell = row.getCell(y);
                        li.add(getValue(cell));
                    }
                    list.add(li);
                }
            }

        }else if(strName.contains(FileTypeEnum.MFHF.getFileType())){
            for (int i = 0; i < work.getNumberOfSheets(); i++) {
                sheet = work.getSheetAt(i);
                if(sheet==null){continue;}

                //遍历当前sheet中的所有行
                for (int j = 2; j <= sheet.getLastRowNum(); j++) {
                    row = sheet.getRow(j);
                    if(row==null||row.getFirstCellNum()==j){continue;}

                    //遍历所有的列
                    List<Object> li = new ArrayList<Object>();
                    for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                        cell = row.getCell(y);
                        li.add(getValue(cell));
                    }
                    list.add(li);
                }
            }
        }

        return list;

    }




/**
     * @Description:根据文件后缀,自适应上传文件的版本
     * @param inStr,fileName
     * @return
     * @throws Exception
     */



    public static  Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{
        Workbook wb = null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        if(excel2003L.equals(fileType)){
            wb = new HSSFWorkbook(inStr);  //2003-
        }else if(excel2007U.equals(fileType)){
            wb = new XSSFWorkbook(inStr);  //2007+
        }else{
            throw new Exception("解析的文件格式有误!");
        }
        return wb;
    }



/**
     * @Description:对表格中数值进行格式化
     * @param cell
     * @return
     */



    //解决excel类型问题,获得数值
    public static String getValue(Cell cell) {
        String value = "";
        if(null==cell){
            return value;
        }
        switch (cell.getCellType()) {


            //数值型
            case NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    //如果是date类型则 ,获取该cell的date值
                   // Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
                   // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                   // value = format.format(date);

                    SimpleDateFormat sdf = null;
                    // 验证short值,根据不同的日期长度进行判断
                    if (cell.getCellStyle().getDataFormat() == 14) {
                        sdf = new SimpleDateFormat("yyyy/MM/dd");
                    } else if (cell.getCellStyle().getDataFormat() == 21) {
                        sdf = new SimpleDateFormat("HH:mm:ss");
                    } else if (cell.getCellStyle().getDataFormat() == 22) {
                        sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                    } else {
                        throw new RuntimeException("日期格式错误!!!");
                    }
                    Date date = cell.getDateCellValue();
                    value = sdf.format(date);

                } else {// 纯数字
                    Double big=new Double(cell.getNumericCellValue());
                    value = big.toString();
                    //解决1234.0  去掉后面的.0
                    if(null!=value&&!"".equals(value.trim())){
                        String[] item = value.split("[.]");
                        if(1<item.length&&"0".equals(item[1])){
                            value=item[0];
                        }
                    }
                }
                break;
            //字符串类型
            case STRING:
                value = cell.getStringCellValue();
                break;
            // 公式类型
            case FORMULA:
                //读公式计算值
                value = String.valueOf(cell.getNumericCellValue());
                if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
                    value = cell.getStringCellValue();
                }
                break;
            // 布尔类型
            case BOOLEAN:
                value = " "+ cell.getBooleanCellValue();
                break;
            default:
                value = cell.getStringCellValue();
        }
        if("null".endsWith(value.trim())){
            value="";
        }
        return value;
    }

    @Scheduled(cron = "0/10 * * * * ?")
    public void fileUpload() throws Exception {
        log.info("开始进入文件扫描的程序..................");
        //文件存储的初始位置
        String saveUrl = uploadUrl;
        //文件被剪切到的位置
        String remUrl = removeUrl;

        log.info("存储文件的地址为" + saveUrl);
        File file = new File(saveUrl);

        if(!file.exists()){//如果保存的uploadUrl文件夹不存在
            file.mkdir();//创建文件夹
        }

        File file1 = new File(remUrl);

        if(!file1.exists()){//如果保存的removeUrl文件夹不存在
            file1.mkdir();//创建文件夹
        }
        //遍历saveURl文件下的目录和文件
        File[] files = file.listFiles();

        for (File fs : files) {
            //获取文件名
            String fileName = fs.getName();
            String strName = fileName.replaceAll("[.][^.]+$", "");
            //获取文件的后缀名
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            //非目录(即文件是为excel文件.xls)则处理文件,数据插入到库中
            String filepath = uploadUrl + "/" + fileName;

            FileInputStream inputStream = new FileInputStream(new File(filepath));
            //获取到文件中数据的list
            List<List<Object>> list = FileTask.getListByExcel(inputStream, filepath,strName);

            //boolean flag = strName.contains(FileType.DSI.getFileType());
         //   if (FileType.DSI.getFileType().equals(strName)) {
         //FileType是自定义的枚举,为了区分应该数据插入到那个实体类。
            if (strName.contains(FileTypeEnum.DSI.getFileType()) == true && list.size() > 0) {
                for (List<Object> list2 : list) {
                    TestDataInfo testDataInfo = new TestDataInfo();
                    testDataInfo.setNo(String.valueOf(list2.get(0)));
                    testDataInfo.setSource(String.valueOf(list2.get(1)));
                    testDataInfo.setStoreTime(String.valueOf(list2.get(2)));
                    testDataInfo.setSampleType(String.valueOf(list2.get(3)));
                    testDataInfo.setCheckData(String.valueOf(list2.get(4)));
                    testDataInfo.setCheckDate(String.valueOf(list2.get(5)));
                    testDataInfo.setCheckName(String.valueOf(list2.get(6)));
                    int sum = itestDataInfoService.insertTes(testDataInfo);

                    try {
                        if (sum == 1) {
                        //成功
                        }
                    } catch (Exception e) {
                     
//失败
                        e.printStackTrace();

                    }
                }
                //面粉加工精度
            }else if (strName.contains(FileTypeEnum.MFHF.getFileType()) == true && list.size() > 0){
                for (List<Object> list2 : list){
                    MfhfTest mfhfTest = new MfhfTest();
                    mfhfTest.setCsrq(String.valueOf(list2.get(0)));
                    mfhfTest.setCssj(String.valueOf(list2.get(1)));
                    mfhfTest.setScrq(String.valueOf(list2.get(2)));
                    mfhfTest.setBc(String.valueOf(list2.get(3)));
                    mfhfTest.setPm(String.valueOf(list2.get(4)));
                    mfhfTest.setFxmj(String.valueOf(list2.get(5)));
                    mfhfTest.setHdmj(String.valueOf(list2.get(6)));
                    mfhfTest.setMfbd(String.valueOf(list2.get(7)));
                    mfhfTest.setHfhl(String.valueOf(list2.get(8)));
                    mfhfTest.setCs1(String.valueOf(list2.get(9)));
                    mfhfTest.setCs2(String.valueOf(list2.get(10)));
                    mfhfTest.setCs3(String.valueOf(list2.get(11)));
                    mfhfTest.setLzhi(String.valueOf(list2.get(12)));
                    mfhfTest.setAzhi(String.valueOf(list2.get(13)));
                    mfhfTest.setBzhi(String.valueOf(list2.get(14)));
                    int sum = iMfhfTestService.insertMfhf(mfhfTest);
                    try{
                        if (sum == 1){
                         //成功
                        }
                    }catch (Exception e){
               //失败
                        e.printStackTrace();

                    }

                }
            }else if (strName.contains(FileTypeEnum.LSY.getFileType()) == true && list.size() > 0){
                //等等,其他的文件类型
            }


            InputStream inStream = new FileInputStream(filepath); //读入原文件
            String removeremoveUrl = removeUrl +"/"+ fileName;
            FileOutputStream removeFs = new FileOutputStream(removeremoveUrl);

            try {
                int bytesum = 0;
                int byteread = 0;
                byte[] buffer = new byte[1444];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
                    System.out.println(bytesum);
                    removeFs.write(buffer, 0, byteread);
                }
                inputStream.close();
                inStream.close();
                removeFs.close();
                boolean flag = false;
                flag = deleteFile(fs);

                if (flag){
                    log.info("删除文件成功");
                }
               // fs.delete();
            } catch (Exception e) {
                log.error("复制单个文件操作出错");
                e.printStackTrace();

            }
        }

    }


/**
     * 删除文件
     *
     * @param file
     * @return boolean
     */
    private static boolean deleteFile(File file) {
        return file.delete();
    }

}



***
因为是小白,所以很多的判断可能欠缺。欢迎指正。

Guess you like

Origin blog.csdn.net/qq_42623440/article/details/112367912