Excel数据批量导入到数据库

Posted 非非是

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Excel数据批量导入到数据库相关的知识,希望对你有一定的参考价值。

1.今天做批量导入网上找了个例子,改了改,运行起来了。用POI实现Excel的读取,需要jar包。

2.ReadExcel.java读取数据

/**
 * 
 */
package com.b510.excel;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.b510.common.Common;
import com.b510.excel.vo.Student;

/**
 * @author Hongten
 * @created 2014-5-18
 */
public class ReadExcel {

    public List<Student> readXls() throws IOException {
        InputStream is = new FileInputStream(Common.EXCEL_PATH);
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
        Student student = null;
        List<Student> list = new ArrayList<Student>();
        // 循环工作表Sheet
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }
            // 循环行Row
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow != null) {
                    student = new Student();
                    HSSFCell no = hssfRow.getCell(0);
                    HSSFCell name = hssfRow.getCell(1);
                    HSSFCell age = hssfRow.getCell(2);
                    HSSFCell score = hssfRow.getCell(3);
                    student.setNo(getValue(no));
                    student.setName(getValue(name));
                    student.setAge(getValue(age));
                    student.setScore(Float.valueOf(getValue(score)));
                    list.add(student);
                }
            }
        }
        return list;
    }
    
     @SuppressWarnings("static-access")
    private String getValue(HSSFCell hssfCell) {
            if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
                // 返回布尔类型的值
                return String.valueOf(hssfCell.getBooleanCellValue());
            } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
                // 返回数值类型的值
                return String.valueOf(hssfCell.getNumericCellValue());
            } else {
                // 返回字符串类型的值
                return String.valueOf(hssfCell.getStringCellValue());
            }
        }
}

3.SaveData2DB.ava保存数据

/**
 * 
 */
package com.b510.excel;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import com.b510.common.Common;
import com.b510.excel.util.DbUtil;
import com.b510.excel.vo.Student;

/**
 * @author Hongten
 * @created 2014-5-18
 */
public class SaveData2DB {

	@SuppressWarnings({ "rawtypes" })
	public void save() throws IOException, SQLException {
		ReadExcel xlsMain = new ReadExcel();
		Student student = null;
		List<Student> list = xlsMain.readXls();

		for (int i = 0; i < list.size(); i++) {
			student = list.get(i);
			List l = DbUtil.selectOne(Common.SELECT_STUDENT_SQL + "\'%" + student.getName() + "%\'", student);
			if (!l.contains(1)) {
				DbUtil.insert(Common.INSERT_STUDENT_SQL, student);
			} else {
				System.out.println("The Record was Exist : No. = " + student.getNo() + " , Name = " + student.getName() + ", Age = " + student.getAge() + ", and has been throw away!");
			}
		}
	}
}

4.源程序http://files.cnblogs.com/files/feifeishi/ExcelTest.rar

以上是关于Excel数据批量导入到数据库的主要内容,如果未能解决你的问题,请参考以下文章

怎样将Excel数据批量导入到word表格中

求一个能把EXCEL批量导入SQL数据库的ASP程序代码!!!

导入excel把数据存在datatable中,用sqlbulkcopy把datable批量导入到数据库的表中

如何把excel表格的数据批量导入word模板文档内?

Excel数据批量导入到数据库

批量Excel数据导入Oracle数据库 导入excel错误:外部表不是预期的格式 解决方案