导入数据的工具类PoiExcelImport、以及后台如何运用。

导入数据的工具类PoiExcelImport、以及后台如何运用。

工具类:

package com.nbpi.company.modules.train.utils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;

public class PoiExcelImport {

    /**
     * 检验文件是否有效
     *
     * @param file
     * @throws Exception
     */
    public static void checkFile(MultipartFile file) throws Exception {
        // 判断文件是否存在
        if (null == file) {
            throw new FileNotFoundException("文件不存在!");
        }
        // 获得文件名
        String fileName = file.getOriginalFilename();
        // 判断文件是否是excel文件
        if (!fileName.endsWith("xls") && !fileName.endsWith("xlsx")) {
            throw new IOException(fileName + "不是excel文件");
        }
    }


    /**
     * 获取workbook
     *
     * @param file
     * @return
     */
    public static Workbook getWorkBook(MultipartFile file) {
        // 获得文件名
        String fileName = file.getOriginalFilename();
        // 创建Workbook工作薄对象,表示整个excel
        Workbook workbook = null;
        try {
            // 获取excel文件的io流
            InputStream is = file.getInputStream();
            // 根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
            if (fileName.endsWith("xls")) {
                // 2003
                workbook = new HSSFWorkbook(is);
            } else if (fileName.endsWith("xlsx")) {
                // 2007
                workbook = new XSSFWorkbook(is);
            }
        } catch (IOException e) {

        }
        return workbook;
    }

    public static String getCellValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        switch (cell.getCellType()) {
            case _NONE:
                return "";
            case BOOLEAN:
                return cell.getBooleanCellValue() ? "是" : "否";
            case NUMERIC:
                return Double.valueOf(new BigDecimal(cell.getNumericCellValue()).doubleValue()).toString();
            case STRING:
                return cell.getStringCellValue().replaceAll("\\n", "");
            case BLANK:
                return "";
            case ERROR:
                return "";
            case FORMULA:
                return "";
            default:
                return "";
        }
    }
}

后端如何运用:

/*
    导入数据
    * */
    @PostMapping({"/importStdFile"})
    @JsonResultAnnotation
    @AssertLoginAnnotation
    public JsonResult importStdFile(MultipartHttpServletRequest request, int projectId) {

        Iterator<String> iterator = request.getFileNames();
        while (iterator.hasNext()) {
            try (Workbook workBook = PoiExcelImport.getWorkBook(request.getFile(iterator.next()));) {
                Sheet sheet = workBook.getSheetAt(0);
                for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
                    Row row = sheet.getRow(i);
                    
                    //开始给实体类赋值
                    
                    BusinessPoint businessPoint = new BusinessPoint();
                    //PoiExcelImport.getCellValue(row.getCell(0));为表的第一列的数据
                    String adress = PoiExcelImport.getCellValue(row.getCell(0));
                    Integer integer = jpaQueryFactory.select(qUnit.id).from(qUnit).where(qUnit.name.eq(adress)).fetchOne();
                    if (integer != null) {
                        businessPoint.setAdress(integer);
                    }
                    businessPoint.setPointName(PoiExcelImport.getCellValue(row.getCell(1)));
                    businessPoint.setPointAdress(PoiExcelImport.getCellValue(row.getCell(2)));
                    String sblx = PoiExcelImport.getCellValue(row.getCell(3));
                    Integer integer1 = jpaQueryFactory.select(qSmCode.id).from(qSmCode).where(qSmCode.name.eq(sblx)).fetchOne();
                    if (integer1 != null) {
                        businessPoint.setEquipmentType(integer1);
                    }
                    String cds = PoiExcelImport.getCellValue(row.getCell(4));
                    String[] split = cds.split("\\.");

                    if ("无".equals(cds)) {
                        businessPoint.setLaneNum(0);
                    } else {
                        Integer integer2 = jpaQueryFactory.select(qSmCode.id).from(qSmCode).where(qSmCode.name.eq(split[0])).fetchOne();
                        if (integer2 != null) {
                            businessPoint.setLaneNum(integer2);
                        }
                    }
                    String azfs = PoiExcelImport.getCellValue(row.getCell(5));
                    Integer integer3 = jpaQueryFactory.select(qSmCode.id).from(qSmCode).where(qSmCode.name.eq(azfs)).fetchOne();
                    if (integer3 != null) {
                        businessPoint.setInstallationType(integer3);
                    }
                    businessPoint.setPoleNews(PoiExcelImport.getCellValue(row.getCell(6)));
                    String azgd = PoiExcelImport.getCellValue(row.getCell(7));
                    Integer integer4 = jpaQueryFactory.select(qSmCode.id).from(qSmCode).where(qSmCode.name.eq(azgd)).fetchOne();
                    if (integer4 != null) {
                        businessPoint.setBatHeight(integer4);
                    }
                    String cellValue = PoiExcelImport.getCellValue(row.getCell(8));
                    businessPoint.setBoomLength(Double.valueOf(cellValue));
                    businessPoint.setMonitorAdress(PoiExcelImport.getCellValue(row.getCell(9)));
                    String isLight = PoiExcelImport.getCellValue(row.getCell(10));
                    if ("是".equals(isLight)) {
                        businessPoint.setIsLight(true);
                    } else {
                        businessPoint.setIsLight(false);
                    }
                    String person = PoiExcelImport.getCellValue(row.getCell(11));
                    Integer integer5 = jpaQueryFactory.select(qUser.id).from(qUser).where(qUser.name.eq(person)).fetchOne();
                    if (integer5 != null) {
                        businessPoint.setPerson(integer5);
                    }
                    businessPoint.setApprovalState(11);
                    businessPoint.setProjectId(projectId);
                    String imgWallGuid = GuidHelper.CreateGuid();
                    businessPoint.setImageGuid(imgWallGuid);
                    
                    //赋值完毕保存
                    
                    businessPointDao.getRepository().save(businessPoint);
                    return JsonHelper.toJson("导入成功");

                }
            } catch (IOException e) {
                log.error("[{}]", e);
            } catch (Exception e) {
                return JsonHelper.toJson("导入失败");
            }
        }
        return JsonHelper.toJson("导入成功");
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值