使用POI读取Excel数据入库

首先先添加poi的依赖

        <dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.13</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.13</version>
		</dependency>

 直接上代码

先判断一下Excel的文件格式,然后再用for循环来循环取数据,最后将数据插入数据库中。

package com.example.demo.service;

import com.example.demo.entity.*;
import com.example.demo.mapper.ExcelMapper;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @Author:
 * @Description:
 * @Date: 
 */
@Service
public class ExcelService {

    @Autowired
    private ExcelMapper userMapper;


    /**
     * 判断文件格式
     *
     * @param inStr
     * @param fileName
     * @return
     * @throws Exception
     */
    public Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
        Workbook workbook = null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        if (".xls".equals(fileType)) {
            workbook = new HSSFWorkbook(inStr);
        } else if (".xlsx".equals(fileType)) {
            workbook = new XSSFWorkbook(inStr);
        } else {
            throw new Exception("请上传excel文件!");
        }
        return workbook;
    }


    /**
     * 处理上传的文件
     *
     * @param in
     * @param fileName
     * @return
     * @throws Exception
     */
    public List getBankListByExcel(InputStream in, String fileName) throws Exception {
        List list = new ArrayList<>();
        //创建Excel工作薄
        Workbook work = this.getWorkbook(in, fileName);
        if (null == work) {
            throw new Exception("创建Excel工作薄为空!");
        }
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;
        System.out.println(fileName+"   ++++++++++   "+work.getNumberOfSheets());

        for (int i = 0; i < work.getNumberOfSheets(); i++) {
            sheet = work.getSheetAt(i);
            if (sheet == null) {
                continue;
            }
            int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();

            for (int j = sheet.getFirstRowNum(); j <= physicalNumberOfRows; j++) {

                int lastRowNum = sheet.getLastRowNum();
                //获取第一行
                Row row1 = sheet.getRow(0);
                //获取表头数量
                short lastCellNum = row1.getLastCellNum();
                System.out.println( "-----   "+lastRowNum);
                row = sheet.getRow(j);
                if (row == null || row.getFirstCellNum() == j || row.getPhysicalNumberOfCells()==0) {
                    continue;
                }
                List<Object> li = new ArrayList<>();
                for (int y = row.getFirstCellNum(); y < lastCellNum; y++) {

                    cell = row.getCell(y);
                    if(cell == null){
                        row.createCell(y).setCellValue("");

                    }else {
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                    }

                    li.add(cell);
                }
                if(i==0){
                    //私有方法
                    this.doMember(row);

                }else if(i==1){

                    this.doIrs(row);

                }else if(i==2){
                    this.doOuter(row);

                }else if(i==3){

                    this.doWorkrecord(row);

                }else{
                    this.doEvent(row);

                }

                list.add(li);
            }
        }
        work.close();
        return list;
    }

    private void doMember(Row row){}
    private void doIrs(Row row){}
    private void doOuter(Row row){}
    private void doEvent(Row row){}
    private void doWorkrecord(Row row){}
}


    

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值