package org.lee.demo;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.lee.dao.CourseImp;
import org.lee.model.Course;
public class InsertExcel {
public InsertExcel(){}
public static void start(InputStream in ,String path) throws FileNotFoundException, IOException {
Workbook book = getWorkBook(in,path); //1.获取工作簿
List<Sheet> sheets = getSheets(book); //2.获取所有工作表
SheetIterator(sheets); //3.对所有工作表进行操作
}
//1.获取工作簿
public static Workbook getWorkBook(InputStream in,String path) throws FileNotFoundException, IOException {
return path.endsWith(".xls") ? (new HSSFWorkbook(in))
: (path.endsWith(".xlsx") ? (new XSSFWorkbook(in)) : (null));
}
//2.获取所有工作表
private static List<Sheet> getSheets(Workbook book) {
int numberOfSheets = book.getNumberOfSheets();
System.out.println("numberOfSheets:" + numberOfSheets);
List<Sheet> sheets = new ArrayList<Sheet>();
for (int i = 0; i < numberOfSheets; i++) {
sheets.add(book.getSheetAt(i));
}
return sheets;
}
//3.对所有工作表进行操作
private static void SheetIterator(List<Sheet> sheets) {
for (int i = 0; i < sheets.size(); i++) { //循环每一张工作表
Sheet sheet = sheets.get(i);
if (sheet.getLastRowNum() > 1) { //判断是否为空表,获取有数据的最后一行的行数。如果为零则为空表
System.out.println(sheet.getSheetName() + "============="); //打印不为空的工作表名字
}
Iterator<Row> iterator = sheet.iterator(); //迭代器
//用两个while循环遍历所有单元格
while (iterator.hasNext()) { //遍历每一行
Row nextRow = iterator.next();
if (nextRow.getRowNum() < 1) {
continue;
//nextRow.getRowNum()就是获取行数,由表中看出第一行(getRowNum()=0)为表头,直接跳过
}
//从第二行开始是有用的数据,要保存早数据库,第二行:nextRow.getRowNum()=1
Iterator<Cell> cellIterator = nextRow.cellIterator();
Course course = new Course();
while (cellIterator.hasNext()) { //遍历每一行的每一列
Cell cell = cellIterator.next();
switch(cell.getColumnIndex()){
case 0:
//将单元格内容设置为String类型,也可以这样写cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellType(1);
course.setId(Integer.parseInt(cell.getStringCellValue()));
break;
case 1: //第二列(系)
cell.setCellType(1);
course.setDept(cell.getStringCellValue());
break;
case 2: //第三列(课程)
cell.setCellType(1);
course.setCourse(cell.getStringCellValue());
break;
case 3:
cell.setCellType(1);
course.setCredit(Double.parseDouble(cell.getStringCellValue()));
break;
case 4:
cell.setCellType(1);
course.setBook_num(cell.getStringCellValue());
break;
case 5: //第六列是日期,需要进行特殊处理
//将单元格内容设置为number类型,也可以这样写cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellType(0);
SimpleDateFormat ftm = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
if (HSSFDateUtil.isCellDateFormatted(cell)) {
course.setPublish_date(ftm.format(date));
//如果这里打印的话,还是会打印出数字,但是保存到数据库会变成日期格式
}
break;
case 6:
cell.setCellType(1);
course.setBook_name(cell.getStringCellValue());
break;
}
System.out.print(" ");
}
try {
//到这里已经遍历完一行Execl的所有单元格,并存储到model里面了,现在调用方法保存到数据库
CourseImp c = new CourseImp();
if(c.Exist(course.getId())==true){
System.out.println("此id对象已存在于数据库,不可重复添加");
continue;
}else{
c.save(course);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(" ");
}
System.out.println(" ");
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java运用poi把excel导入数据库demo
共52个文件
jar:18个
class:7个
java:7个
需积分: 48 60 下载量 65 浏览量
2018-07-20
15:57:48
上传
评论
收藏 14.22MB ZIP 举报
温馨提示
java运用poi把excel数据导入到oracle数据库中,上场内容未自己写的小demo,可下载查看,不清楚的可以联系我进行帮助。
资源推荐
资源详情
资源评论
收起资源包目录
poi-excel.zip (52个子文件)
poi-excel
.project 903B
WebContent
WEB-INF
view
upload
fileUpload.jsp 2KB
testUpload.jsp 2KB
result.jsp 1KB
lib
spring-aop-4.3.2.RELEASE.jar 371KB
spring-aspects-4.3.2.RELEASE.jar 57KB
spring-context-4.3.2.RELEASE.jar 1.08MB
dom4j-1.6.1.jar 307KB
jstl-1.2.jar 405KB
spring-webmvc-4.3.2.RELEASE.jar 893KB
poi-ooxml-3.7-20101029.jar 487KB
poi-ooxml-schemas-3.8.jar 4.49MB
commons-logging-1.1.1.jar 59KB
xmlbeans-2.3.0.jar 2.54MB
poi-3.9.jar 1.78MB
spring-beans-4.3.2.RELEASE.jar 739KB
com.springsource.org.apache.commons.fileupload-1.2.0.jar 53KB
mysql-connector-java-5.1.41-bin.jar 970KB
spring-core-4.3.2.RELEASE.jar 1.06MB
spring-expression-4.3.2.RELEASE.jar 258KB
com.springsource.org.apache.commons.io-1.4.0.jar 108KB
spring-web-4.3.2.RELEASE.jar 793KB
web.xml 649B
META-INF
MANIFEST.MF 39B
static
js
ajaxfileupload.js 8KB
jquery.validate.min.js 21KB
bootstrap.min.js 36KB
jquery.metadata.js 4KB
jquery-2.1.4.min.js 82KB
src
config
props
db.properties 115B
org
lee
model
Course.java 1KB
demo
InsertExcel.java 5KB
ReadExcel.java 3KB
Main.java 417B
dao
CourseImp.java 2KB
web
FileUploadController.java 2KB
util
DBUtil.java 2KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 461B
.jsdtscope 567B
build
classes
config
props
db.properties 115B
org
lee
model
Course.class 2KB
demo
Main.class 801B
ReadExcel.class 4KB
InsertExcel.class 5KB
dao
CourseImp.class 3KB
web
FileUploadController.class 3KB
util
DBUtil.class 3KB
.classpath 841B
共 52 条
- 1
资源评论
newTuring
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功