import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID; import org.apache.poi.ss.usermodel.Cell;
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; public class test {
public importExcel(){
String excelPath = null;//excel表格的文件位置
XSSFWorkbook xwb = null;//创建excel表格的工作空间
InputStream in;
try {
in = new FileInputStream(excelPath);
xwb = new XSSFWorkbook(in);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("找不到指定路径下的excel文件!");
} // System.out.println("该文档一共有sheet"+xwb.getNumberOfSheets()+"个");
//开始循环sheet页
for(int i=0;i<xwb.getNumberOfSheets();i++){
XSSFSheet sheet = xwb.getSheetAt(0);//读取第1个sheet
XSSFRow row;
List<String[]> userData = new ArrayList<String[]>();//用来存从excel表格中获取到的值,然后向数据库中保存
//读取系统配置sheet页
row:for(int rowIndex = 0;rowIndex < sheet.getPhysicalNumberOfRows();rowIndex++){
if((row = sheet.getRow(rowIndex)) != null){//判断该行内容不为空
String[] userRow = new String[row.getLastCellNum()];
//开始循环每一行的列
col:for(int columnIndex = row.getFirstCellNum(); columnIndex < row.getLastCellNum(); columnIndex++){
XSSFCell cell = row.getCell(columnIndex);
if(cell != null){
cell.setCellType(Cell.CELL_TYPE_STRING);
userRow[columnIndex] = cell.getStringCellValue();
}
}
userData.add(userRow);
}
}
//开始往数据库中插入数据
if(userData.size()>0){
//开始往人员表中保存数据
Persons persons = new Persons();
persons.setName(userData.get(0)[1]);
persons.setEmail(userData.get(0)[2]);
persons.setSex(userData.get(0)[3]);
persons.setAge(Integer.parseInt(userData.get(0)[4]));
personsManage.save(persons);
}
}
}
}

最新文章

  1. SLES 11 SP3防火墙设置
  2. SpringMVC中如何在网站启动、结束时执行代码(详细,确保可用)
  3. 深入对比数据科学工具箱:Python和R之争
  4. 最新做路径动画必备Simple Waypoint System5.1.1最新做路径动画必备Simple Waypoint System5.1.1
  5. HTML的超链接
  6. __stdcall 与 __cdecl
  7. [RM HA3] Zookeeper在RM HA的应用
  8. 在Linux下安装C/C++开发工具包的最佳方式
  9. [转] 消息系统该Push/Pull模式分析
  10. C#属性总结
  11. Python os.path模板函数
  12. 在Linux下更新或安装curl
  13. Zookeeper运维
  14. Ignite(一): 概述
  15. (BFS/DFS) leetcode 200. Number of Islands
  16. Mysql 存储过程实例详解
  17. Introduction to pinatrace annotate version 2: a look into latches again
  18. Objective-C编程 - 1. 浅谈内存分配
  19. 用Gearman分发PHP应用程序的工作负载
  20. 技术分享:SSH实战项目

热门文章

  1. MSSQL 事务,视图,索引,存储过程,触发器
  2. Linux学习笔记(一):常用命令
  3. swift 可选类型(optional)--- swift 入门
  4. 最新Linux部署.NET,Mono and DNX
  5. web前端的春天 or 噩梦
  6. .NET 环境中使用RabbitMQ
  7. What is Away3D
  8. Java线程池解析
  9. hibernate一对一外键单向关联
  10. Python学习--02输入和输出