使用POI读取EXCEL并保存到数据库

在一次开发中,遇到一个需求,需求要求是:

使用POI读取EXCEL并保存到数据库_第1张图片


使用POI读取EXCEL并保存到数据库_第2张图片使用POI读取EXCEL并保存到数据库_第3张图片


使用Poi 3.16-beta 包,和相关springjar包。

spring 主要用了bean的管理和自动注入。

持久层框架使用mybatis。

框架分层如下:

使用POI读取EXCEL并保存到数据库_第4张图片


ExcelParserInterface:

public interface ExcelParserInterface {
	List parseExcel(String fileName);
}
ExcelSaverInterface:

public interface ExcelSaverInterface {
	void saveToDB(List exceldata);
}

ExcelParser:

package stu.qi.excelImpoter.services.impl;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbookType;


import stu.qi.excelImpoter.services.ExcelParserInterface;


public class ExcelParser implements ExcelParserInterface{
	private XSSFWorkbook workbook;
	private XSSFSheet sheet;
	private int rowNum;
	private int colNum;
	
	@Override
	public List parseExcel(String fileName) {
		List excelData = null;
		try {
			InputStream in = new FileInputStream(fileName);
			workbook = new XSSFWorkbook(in);
			sheet = workbook.getSheetAt(0);
			rowNum = sheet.getLastRowNum();
			colNum = sheet.getRow(0).getPhysicalNumberOfCells();
			excelData = this.getContent();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return excelData;
	}
	
	/*
	 * 读取表头
	 * */
	public String[] readExcelTitle(){
		String[] title = new String[colNum];
		for (int i = 0; i < title.length; i++) {
			title[i] = getCellValue(sheet.getRow(0).getCell(i));
		}
		return title;
	}
	
	/*
	 * 读取内容
	 * 每个map中保存一行的内容
	 * */
	public List getContent(){
		List excelData = new ArrayList<>();
		Map map=null;
		//String[] title = this.readExcelTitle();
		XSSFRow row;
		String cellContent;
		for (int i = 1; i < rowNum+1; i++) {
			map = new HashMap<>();
			row = sheet.getRow(i);
			int j = 0;
			while(j < colNum){
				cellContent = this.getCellValue(row.getCell(j));
				map.put(j+1, cellContent);
				j++;
			}
			excelData.add(map);
		}
		
		return excelData;
	}
	
	/*
	 * 读取每个单元的内容
	 * */
	public String getCellValue(XSSFCell cell) {
		if(cell==null){
			return null;
		}
		
		String str = "";
		switch(cell.getCellTypeEnum()){
		case STRING:
			str+=cell.getStringCellValue();
			break;	
		case NUMERIC:
			if(HSSFDateUtil.isCellDateFormatted(cell))
				str+=cell.getDateCellValue();
			str+=cell.getNumericCellValue();
			break;
		case BOOLEAN:
			str+=cell.getBooleanCellValue();
			break;
		case BLANK:
			break;
		default:
			break;
		}
		return str;
	}


	public int getColNum() {
		return colNum;
	}		
}


* getCellValue这个方法是必要的,每个单元格的数据有自己的类型。

ExcelSaver:

package stu.qi.excelImpoter.services.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;

import stu.qi.excelImpoter.bean.CourseClass;
import stu.qi.excelImpoter.bean.Questionbank;
import stu.qi.excelImpoter.dao.CourseClassDao;
import stu.qi.excelImpoter.dao.QuestionbankDao;
import stu.qi.excelImpoter.services.ExcelSaverInterface;
import util.SpringUtil;

public class ExcelSaver implements ExcelSaverInterface{
	@Override
	public void saveToDB(List exceldata) {
		@Resource(name="courseClassDao")
		CourseClassDao cDao;
		@Resource(name="questionbankDao")
		QuestionbankDao qDao;
		CourseClass courseClass = new CourseClass();
		Questionbank questionbank = new Questionbank();
		for (int i = 0; i < exceldata.size(); i++) {
			Map map = (Map) exceldata.get(i);
			int cid = cDao.maxId()+1;
			courseClass.setCid(cid);
			courseClass.setCname(map.get(7));
			courseClass.setPid(0);
			questionbank.setExamID(qDao.maxId()+1);
			questionbank.setCourseID(0);				// 课程 (留空) 设0;
			questionbank.setExamtypeID(cid);
			questionbank.setExamscore((int)Double.parseDouble(map.get(3)));
			questionbank.setExamselect(map.get(4));
			questionbank.setExamvalue(map.get(5));
			questionbank.setExamcontent("");
			questionbank.setExamdiffculty((int)Double.parseDouble(map.get(6)));
			questionbank.setUserid(0);					//userId 设0;
			questionbank.setExamclass(map.get(2));
			cDao.insertCourse(courseClass);
			qDao.insertQuestion(questionbank);
		}
	}	
}
根据内容进行数据插入。


你可能感兴趣的:(使用POI读取EXCEL并保存到数据库)