excel批量导入

最近的一个项目又用到了excel导入这么个功能,虽然以前做过,但是事隔一年多,已经一年多,早忘了,看了网上的一些资料,讲得不是很到位,花了点时间整理了一下,今天有一点空,就把它记录下来:
主要用到的技术有:
jxl.jar(只这一包就可以,很简单)
spring jdbcTemplate

由于是批量导入,如果直接使用hibernate,效率有些低,所以直接使用jdbcTemplate,从最基本的入手:

List<Question> questions = new ArrayList<Question>();
		Question question = null;
		try {
			Workbook book = Workbook.getWorkbook(new File("d:/Test.xls"));

			// 获得第一个sheet,默认有三个
			Sheet sheet = book.getSheet(0);
			// 一共有多少行多少列数据
			int rows = sheet.getRows();
			int columns = sheet.getColumns();

			boolean hasText = false;
			for (int i = 0; i < rows; i++) {
				// 过滤掉没有文本内容的行
				for (int j = 0; j < columns; j++)
					if (sheet.getCell(j, i).getContents() != "") {
						hasText = true;
						break;
					}
				if (hasText) {
					question = new Question();
					question.setTitle(sheet.getCell(0, i).getContents());
					question.setType1(sheet.getCell(1, i).getContents());
					question.setType2(sheet.getCell(2, i).getContents());
					question.setType(Integer.valueOf(sheet.getCell(3, i)
							.getContents()));
					question.setScore(Integer.valueOf(sheet.getCell(4, i)
							.getContents()));
					question.setA(sheet.getCell(5, i).getContents());
					question.setB(sheet.getCell(6, i).getContents());
					question.setC(sheet.getCell(7, i).getContents());
					question.setD(sheet.getCell(8, i).getContents());
					question.setE(sheet.getCell(9, i).getContents());
					question.setTrueAnswer(sheet.getCell(10, i).getContents());
					questions.add(question);
				}
			}
			book.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return questions;


上面代码的作用就是将指定路径的excel文件放在一个List内。这个步骤比想象的的确要简单一些,居然就这么点代码就能完成excel导入java list的工作,下面进一步要做的就是,将其导入数据库,本来原来是打算用hibernate的,经过网上查找,发现都存在不少问题,于是就打算采用jdbcTemplate,而jdbcTemplate使用起来也非常的简单,首先需要配置jdbcTemplate bean,配置方法与配置hibernateTemplate差不多:

.....
<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource"><ref bean="dataSource"/></property>
    </bean>
.....


然后将其注入相应的dao中,就可以进入实际插入操作了:

....
public void batchInsert(final List<Question> q) {
		// TODO Auto-generated method stub
		final List<Question> questions = getQuestionsByXls();
		final int size = questions.size();
		String sql = "insert into question(title,type,type1,type2,state,creationTime,checked,score,a,b,c,d,e,trueAnswer) "
				+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		myJdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

			public int getBatchSize() {
				// TODO Auto-generated method stub
				return size;
			}

			// i - index of the statement we're issuing in the batch, starting
			// from 0
			public void setValues(PreparedStatement ps, int i)
					throws SQLException {
				Question question = questions.get(i);
				ps.setString(1, question.getTitle());
				ps.setInt(2, question.getType());
				ps.setString(3, question.getType1());
				ps.setString(4, question.getType2());
				ps.setInt(5, 1);
				ps.setDate(6, new Date(new java.util.Date().getTime()));
				ps.setInt(7, 0);
				ps.setInt(8, question.getScore());
				ps.setString(9, question.getA());
				ps.setString(10, question.getB());
				ps.setString(11, question.getC());
				ps.setString(12, question.getD());
				ps.setString(13, question.getE());
				ps.setString(14, question.getTrueAnswer());
			}
		});
	}
...


上面这段代码与原始的jdbc几乎没什么区别,需要注意的是,setValues()方法参数中,有个i,这个参数的意思注释已经说了。当然还有叫getBatchSize()的方法,文档中说过,"This method will be called the number of times that you specified in the getBatchSize call",就是说,getBatchSize返回的数字表示要插入的数据的条数。

你可能感兴趣的:(spring,sql,Hibernate,bean,Excel)