1. 程式人生 > >使用POI讀取Excel資料入庫

使用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){}
}