您的位置:首页 > 数据库

POI导入2003和2007到数据库

2013-06-24 09:17 302 查看
package com.oa.teacher.common;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

import org.apache.poi.hdgf.streams.Stream;

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 org.apache.poi.ss.formula.functions.Value;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import test.ImportExcel;

/**

* POI处理Excel2003和2007兼容有漏洞,接口使用不当!没办法,只能2003和2007分开处理

* @author Administrator

*

*/

public class ImportExcelUtil {

/*********

*

* 导入Excel数据

*

* @return

* @throws Exception

*/

private static final int version2003 = 2003;

private static final int version2007 = 2007;

private static int version = version2003;

private static Workbook wb = null;

private static Sheet sheet = null;

private static Cell cell = null;

private static Row row = null;

//解决Excel2003不兼容添加

private static HSSFWorkbook hwb = null;

private static HSSFSheet hsheet=null;

private static HSSFCell hcell=null;

private static HSSFRow hrow=null;

@SuppressWarnings("finally")

public static List<ImportExcel> importExcel(File excelFile) {

List<ImportExcel> list = new ArrayList<ImportExcel>();

String tid = null;

String coursecode = null;

Long sid = null;

Double score = null;

String memo = null;

int k = 0;

int flag = 0; // 指示指针所访问的位置

FileInputStream stream = null;

if (excelFile != null) {

String path = excelFile.getAbsolutePath();// 获取文件的路径

try {

wb = new XSSFWorkbook(path);

for (int numSheets = 0; numSheets < wb.getNumberOfSheets(); numSheets++) { // 读取每一个sheet

if (null != wb.getSheetAt(numSheets)) {

sheet = (Sheet) wb.getSheetAt(numSheets);// 定义Sheet对象

for (int rowNumOfSheet = 0; rowNumOfSheet <= sheet

.getLastRowNum(); rowNumOfSheet++) {// 循环每一行

if (null != sheet.getRow(rowNumOfSheet)) {

row = sheet.getRow(rowNumOfSheet); // 定义行,并赋值

for (int cellNumOfRow = 0; cellNumOfRow <= row

.getLastCellNum(); cellNumOfRow++) { // 循环行中的每一列

cell = row.getCell(cellNumOfRow); // 获得行对象

if (null != cell) {

if (rowNumOfSheet == 0) {// //

// 如果rowNumOfSheet的值为0,则读取表头,判断excel的格式和预定格式是否相符

int type = cell.getCellType();

switch (type) {// 判断数据类型

case Cell.CELL_TYPE_STRING:

if (cellNumOfRow == 0) {

if (cell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("教师工号")) {

flag++;

} else {

System.out

.println("错误:表头的教师工号不符合约定格式");

}

} else if (cellNumOfRow == 1) {

if (cell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("课程编号")) {

flag++;

} else {

System.out

.println("错误:表头的课程编号不符合约定格式");

}

} else if (cellNumOfRow == 2) {

if (cell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("学期编号")) {

flag++;

} else {

System.out

.println("错误:表头的学期编号不符合约定格式");

}

} else if (cellNumOfRow == 3) {

if (cell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("分数")) {

flag++;

} else {

System.out

.println("错误:表头的分数不符合约定格式");

}

} else if (cellNumOfRow == 4) {

if (cell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("备注")) {

flag++;

} else {

System.out

.println("错误:表头的备注不符合约定格式");

}

}

break;

case Cell.CELL_TYPE_BOOLEAN:

break;

case Cell.CELL_TYPE_NUMERIC:

break;

default:

break;

}

} else {// 开始打印数据

int tempType = cell.getCellType();

if (tempType == Cell.CELL_TYPE_STRING

|| tempType == Cell.CELL_TYPE_BOOLEAN

|| tempType == Cell.CELL_TYPE_NUMERIC) { // 为字符串型

if (cellNumOfRow == 0) {

if (tempType == Cell.CELL_TYPE_NUMERIC) {

tid = String

.valueOf(cell

.getNumericCellValue());

} else {

tid = cell

.getStringCellValue()

.replace('\t',

' ')

.replace('\n',

' ')

.replace('\r',

' ')

.trim();

}

if (tid == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的教师工号不能为空");

}

} else if (cellNumOfRow == 1) {

if (tempType == Cell.CELL_TYPE_NUMERIC) {

coursecode = String

.valueOf(cell

.getNumericCellValue());

} else {

coursecode = cell

.getStringCellValue()

.replace('\t',

' ')

.replace('\n',

' ')

.replace('\r',

' ')

.trim();

}

if (coursecode == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的课程编号不能为空");

}

} else if (cellNumOfRow == 2) {

if (tempType == Cell.CELL_TYPE_STRING) {

sid = Long

.parseLong(cell

.getStringCellValue());

} else {

sid = (long) cell

.getNumericCellValue();

}

if (sid == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的学期编号不能为空");

}

} else if (cellNumOfRow == 3) { // 备案单位

if (tempType == Cell.CELL_TYPE_STRING) {

score = Double

.parseDouble(cell

.getStringCellValue());

} else {

score = cell

.getNumericCellValue();

}

if (score == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的分数不能为空");

}

} else if (cellNumOfRow == 4) { // 备案时间

memo = cell

.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim();

}

} else if (tempType == Cell.CELL_TYPE_BLANK) {

System.out

.println("提示:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的值为空,请查看核对是否符合约定要求");

}

}

}// 判断列不为空

}// 循环每一列

if (flag != 5) {

System.out.println("请核对后重试");

}

if (tid != null && coursecode != null

&& sid != null && score != null) {

ImportExcel ie = new ImportExcel();

ie.setTid(tid);

ie.setCoursecode(coursecode);

ie.setSid(sid);

ie.setScore(score);

ie.setMemo(memo);

list.add(ie);

k++;

}

}// 判断行不为空

}// 循环每一行

}// 判断sheet不为空

}// 循环读取每一个sheet

} catch (Exception ex) {

try {

stream = new FileInputStream(excelFile);

hwb = new HSSFWorkbook(stream); // 2003和2007兼容问题,这里报错不能强制转换!

} catch (Exception e) {

ex.printStackTrace();

}

for (int numSheets = 0; numSheets < hwb.getNumberOfSheets(); numSheets++) { // 读取每一个sheet

if (null != hwb.getSheetAt(numSheets)) {

hsheet = hwb.getSheetAt(numSheets);// 定义Sheet对象

for (int rowNumOfSheet = 0; rowNumOfSheet <= hsheet

.getLastRowNum(); rowNumOfSheet++) {// 循环每一行

if (null != hsheet.getRow(rowNumOfSheet)) {

hrow = hsheet.getRow(rowNumOfSheet); // 定义行,并赋值

for (int cellNumOfRow = 0; cellNumOfRow <= hrow

.getLastCellNum(); cellNumOfRow++) { // 循环行中的每一列

hcell = hrow.getCell((short) cellNumOfRow); // 获得行对象

if (null != hcell) {

if (rowNumOfSheet == 0) {// //

// 如果rowNumOfSheet的值为0,则读取表头,判断excel的格式和预定格式是否相符

int type = hcell.getCellType();

switch (type) {// 判断数据类型

case Cell.CELL_TYPE_STRING:

if (cellNumOfRow == 0) {

if (hcell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("教师工号")) {

flag++;

} else {

System.out

.println("错误:表头的教师工号不符合约定格式");

}

} else if (cellNumOfRow == 1) {

if (hcell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("课程编号")) {

flag++;

} else {

System.out

.println("错误:表头的课程编号不符合约定格式");

}

} else if (cellNumOfRow == 2) {

if (hcell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("学期编号")) {

flag++;

} else {

System.out

.println("错误:表头的学期编号不符合约定格式");

}

} else if (cellNumOfRow == 3) {

if (hcell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("分数")) {

flag++;

} else {

System.out

.println("错误:表头的分数不符合约定格式");

}

} else if (cellNumOfRow == 4) {

if (hcell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("备注")) {

flag++;

} else {

System.out

.println("错误:表头的备注不符合约定格式");

}

}

break;

case Cell.CELL_TYPE_BOOLEAN:

break;

case Cell.CELL_TYPE_NUMERIC:

break;

default:

break;

}

} else {// 开始打印数据

int tempType = hcell.getCellType();

if (tempType == Cell.CELL_TYPE_STRING

|| tempType == Cell.CELL_TYPE_BOOLEAN

|| tempType == Cell.CELL_TYPE_NUMERIC) { // 为字符串型

if (cellNumOfRow == 0) {

if (tempType == Cell.CELL_TYPE_NUMERIC) {

tid = String

.valueOf(hcell

.getNumericCellValue());

} else {

tid = hcell

.getStringCellValue()

.replace('\t',

' ')

.replace('\n',

' ')

.replace('\r',

' ')

.trim();

}

if (tid == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的教师工号不能为空");

}

} else if (cellNumOfRow == 1) {

if (tempType == Cell.CELL_TYPE_NUMERIC) {

coursecode = String

.valueOf(hcell

.getNumericCellValue());

} else {

coursecode = hcell

.getStringCellValue()

.replace('\t',

' ')

.replace('\n',

' ')

.replace('\r',

' ')

.trim();

}

if (coursecode == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的课程编号不能为空");

}

} else if (cellNumOfRow == 2) {

if (tempType == Cell.CELL_TYPE_STRING) {

sid = Long

.parseLong(hcell

.getStringCellValue());

} else {

sid = (long) hcell

.getNumericCellValue();

}

if (sid == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的学期编号不能为空");

}

} else if (cellNumOfRow == 3) { // 备案单位

if (tempType == Cell.CELL_TYPE_STRING) {

score = Double

.parseDouble(hcell

.getStringCellValue());

} else {

score = hcell

.getNumericCellValue();

}

if (score == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的分数不能为空");

}

} else if (cellNumOfRow == 4) { // 备案时间

memo = hcell

.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim();

}

} else if (tempType == Cell.CELL_TYPE_BLANK) {

System.out

.println("提示:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的值为空,请查看核对是否符合约定要求");

}

}

}// 判断列不为空

}// 循环每一列

if (flag != 5) {

System.out.println("请核对后重试");

}

if (tid != null && coursecode != null

&& sid != null && score != null) {

ImportExcel ie = new ImportExcel();

ie.setTid(tid);

ie.setCoursecode(coursecode);

ie.setSid(sid);

ie.setScore(score);

ie.setMemo(memo);

list.add(ie);

k++;

}

}// 判断行不为空

}// 循环每一行

}// 判断sheet不为空

}// 循环读取每一个sheet

} finally {

if (stream != null)

try {

stream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}// 如果文件存在的判断

}

return list;

}

}

//大家会发现,这里不是多此一举代码冗余了吗?是的!确实是这样!不过这也是没办法的办法了。阅读了POI的帮助文档之后,明明HSSFwork和XSSFwork共同的接口是Workbook,但是,在执行的过程中,只有XSSFwork能转换成Workbook,HssFwork却不能,这个我无法理解。强制转换之后,就会报强制类型不能转换的错误!

package com.oa.teacher.common;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import org.apache.poi.hdgf.streams.Stream;

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 com.oa.teacher.model.Import;

public class ImportExcel {

private File excelFile;// File对象,目的是获取页面上传的文件

public File getExcelFile() {

return excelFile;

}

public void setExcelFile(File excelFile) {

this.excelFile = excelFile;

}

private List<Import> imports = new ArrayList<Import>();

private static final int version2003 = 2003;

private static final int version2007 = 2007;

private static int version = version2003;

private static Workbook wb = null;

private static Sheet sheet = null;

private static Cell cell = null;

private static Row row = null;

@SuppressWarnings({ "finally", "deprecation" })

public String importExcel() throws Exception {

// 老师工号

String tid = null;

// 课程编号

String coursecode = null;

// 学年编号

Long sid = null;

// 分数

Double score = null;

// 备注

String memo = null;

int k = 0;

int flag = 0; // 指示指针所访问的位置

if (excelFile != null) {

String path = excelFile.getAbsolutePath();// 获取文件的路径

System.out.println("文件的路径是:" + path);

if (excelFile.getName().endsWith(".xls"))

version = version2003;

else if (excelFile.getName().endsWith(".xlsx")) {

version = version2007;

}

InputStream stream = null;

if (version == version2003) {

stream = new FileInputStream(excelFile);

wb = (Workbook) new HSSFWorkbook(stream);

} else if (version == version2007) {

wb = new XSSFWorkbook(path);

}

try {

for (int numSheets = 0; numSheets < wb.getNumberOfSheets(); numSheets++) { // 读取每一个sheet

if (null != wb.getSheetAt(numSheets)) {// 如果当前sheet不为空

sheet = wb.getSheetAt(numSheets);// 定义Sheet对象

for (int rowNumOfSheet = 0; rowNumOfSheet <= sheet

.getLastRowNum(); rowNumOfSheet++) {//循环每一行

if (null != sheet.getRow(rowNumOfSheet)) {

row = sheet.getRow(rowNumOfSheet); // 定义行,并赋值

for (int cellNumOfRow = 0; cellNumOfRow <= row

.getLastCellNum(); cellNumOfRow++) { // 读取rowNumOfSheet值所对应行的数据

cell = row.getCell(cellNumOfRow); // 获得列

int type = cell.getCellType();

if (null != row.getCell(cellNumOfRow)) {

if (rowNumOfSheet == 0) {// 如果rowNumOfSheet的值为0,则读取表头,判断excel的格式和预定格式是否相符

switch (type) {

case Cell.CELL_TYPE_NUMERIC:

break;

case Cell.CELL_TYPE_BOOLEAN:

break;

case Cell.CELL_TYPE_STRING:

if (cellNumOfRow == 0) {

if (cell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("教师工号")) {

flag++;

} else {

System.out

.println("错误:第一行的教师工号不符合约定格式");

}

} else if (cellNumOfRow == 1) {

if (cell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("课程编号")) {

flag++;

} else {

System.out

.println("错误:第一行的课程编号不符合约定格式");

}

} else if (cellNumOfRow == 2) {

if (cell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("学年编号")) {

flag++;

} else {

System.out

.println("第一行的学年编号不符合约定格式");

}

} else if (cellNumOfRow == 3) {

if (cell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("分数")) {

flag++;

System.out

.println("=========flag:"

+ flag);

} else {

System.out

.println("第一行的学年编号不符合约定格式");

}

} else if (cellNumOfRow == 4) {

if (cell.getStringCellValue()

.replace('\t', ' ')

.replace('\n', ' ')

.replace('\r', ' ')

.trim()

.equals("备注")) {

flag++;

System.out

.println("=========flag:"

+ flag);

} else {

System.out

.println("第一行的备注不符合约定格式");

}

}

break;

default:

break;

}

} else {

if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC

|| cell.getCellType() == Cell.CELL_TYPE_BOOLEAN

|| cell.getCellType() == Cell.CELL_TYPE_STRING) { // 为数值型

if (cellNumOfRow == 0) {

tid = String

.valueOf(cell

.getStringCellValue()

.replace(

'\t',

' ')

.replace(

'\n',

' ')

.replace(

'\r',

' ')

.trim());

if (tid == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的教师工号不能为空");

}

} else if (cellNumOfRow == 1) {

coursecode = String

.valueOf(cell

.getStringCellValue()

.replace(

'\t',

' ')

.replace(

'\n',

' ')

.replace(

'\r',

' ')

.trim());

if (coursecode == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的课程编号不能为空");

}

} else if (cellNumOfRow == 2) {

sid = Long

.parseLong(cell

.getStringCellValue()

.replace(

'\t',

' ')

.replace(

'\n',

' ')

.replace(

'\r',

' ')

.trim());

if (sid == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的学年编号不能为空");

}

} else if (cellNumOfRow == 3) {

score = Double

.parseDouble(cell

.getStringCellValue()

.replace(

'\t',

' ')

.replace(

'\n',

' ')

.replace(

'\r',

' ')

.trim());

if (score == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的分数不能为空");

}

} else if (cellNumOfRow == 4) { // 备案时间

memo = String

.valueOf(cell

.getStringCellValue()

.replace(

'\t',

' ')

.replace(

'\n',

' ')

.replace(

'\r',

' ')

.trim());

if (memo == null) {

System.out

.println("错误:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的备注不能为空");

}

}

} else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {

System.out

.println("提示:在Sheet"

+ (numSheets + 1)

+ "中的第"

+ (rowNumOfSheet + 1)

+ "行的第"

+ (cellNumOfRow + 1)

+ "列的值为空,请查看核对是否符合约定要求");

}

}

}//判断当前行的当前列不为空

}//循环每一行的所有列

if (flag != 5) {

System.out.println("请核对后重试");

}

if (tid != null && coursecode != null

&& sid != null && score != null

&& memo != null) {

// 这里应该不需要从数据库中读到数据之后再保存,之后写本地SQL

Import import1 = new Import();

import1.setTid(tid);

import1.setCoursecode(coursecode);

import1.setSid(sid);

import1.setScore(score);

import1.setMemo(memo);

imports.add(import1);

k++;

}

}//判断当前行不为空

} //循环所有行

}//判断每个sheet不为空

}//循环每个sheet

} catch (Exception ex) {

ex.printStackTrace();

} finally {

try {

if (stream != null)

stream.close();

} catch (Exception e1) {

e1.printStackTrace();

}

System.out.println("导入了多少" + imports.size());

for (Import import1 : imports) {

System.out.println(import1.getTid());

System.out.println(import1.getCoursecode());

System.out.println(import1.getSid());

System.out.println(import1.getScore());

System.out.println(import1.getMemo());

}

}

}//判断文件存在

return "SUCCESS";

}

}

这样是比较简洁的,但是,会执行不过的!不知道POI出现了什么样的问题!我用了POI3。5 ,3.6,3.8都不能解决这个问题,如果有朋友解决了,请提醒!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: