上传入库整个过程

package com.irm.jd.service;

import com.irm.jd.entity.Sit;
import com.irm.jd.mapper.SitMapper;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class SitService {
    @Autowired
    private SitMapper sitMapper;

    @Async
    public void upload(MultipartFile file) throws IOException {
        // 执行文件上传
        String originalFilename = file.getOriginalFilename();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        UUID uuid = UUID.randomUUID();
        String fileName = uuid + fileExtension;
        String dir = "D:/uploads/" + fileName;
        File willUploadFile = new File(dir);
        file.transferTo(willUploadFile);
        try {
            FileInputStream fileInputStream = new FileInputStream(willUploadFile);
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream);
            if (xssfWorkbook != null) {
                XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
                List<Sit> sits = new ArrayList<>();
                int count = 0;
                for (int i = 0; i < sheet.getLastRowNum(); i++) {
                    XSSFRow row = sheet.getRow(i);
                    if (row != null) {
                        Sit sit = new Sit();
                        for (int k = 0; k < row.getLastCellNum(); k++) {
                            if (row.getCell(0) != null) {
                                if (!row.getCell(0).toString().equals("null") && !row.getCell(0).toString().equals("")) {
                                    sit.setGongwei(row.getCell(0).toString());
                                }
                            }

                            if (row.getCell(1) != null) {
                                if (!row.getCell(1).toString().equals("null") && !row.getCell(1).toString().equals("")) {
                                    sit.setFloor(row.getCell(1).toString());
                                }
                            }

                            if (row.getCell(2) != null) {
                                if (!row.getCell(2).toString().equals("null") && !row.getCell(2).toString().equals("")) {
                                    sit.setTelephone_ip(row.getCell(2).toString());
                                }
                            }


                            if (row.getCell(3) != null) {
                                if (!row.getCell(3).toString().equals("null") && !row.getCell(3).toString().equals("")) {
                                    sit.setComputer_ip(row.getCell(3).toString());
                                }
                            }


                            if (row.getCell(4) != null) {
                                if (!row.getCell(4).toString().equals("null") && !row.getCell(4).toString().equals("")) {
                                    sit.setArea(row.getCell(4).toString());
                                }
                            }
                        }
                        sits.add(sit);
                    } else {
                        System.out.println("null");
                    }
                }
                sitMapper.insertSite(sits);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

  

转载于:https://www.cnblogs.com/leigepython/p/10191894.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值