使用POI读取Excel数据入库

首先先添加poi的依赖

        
			org.apache.poi
			poi
			3.13
		
		
			org.apache.poi
			poi-ooxml
			3.13
		

 直接上代码

先判断一下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 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){}
}


     
  

 

你可能感兴趣的:(spring)