POI导入demo
前言
使用上篇博文的导入方法,写一个简单的导入demo。其实有了工具类之后就没啥难度了,也就只简单的拿数据。先写个简单的,然后想办法实现动态读取吧,这样读取其实还是比较烦的,每次该模板都要改代码,说到底我还是比较懒的。
excel文件

实体类
package cc.vvxtoys.poi;
public class Student {
private String id;
private String stcode;
private String stname;
private String sex;
private String phone;
private String school;
private String address;
private String birthday;
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStcode() {
return stcode;
}
public void setStcode(String stcode) {
this.stcode = stcode;
}
public String getStname() {
return stname;
}
public void setStname(String stname) {
this.stname = stname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Student(String id, String stcode, String stname, String sex, String phone, String school, String address) {
super();
this.id = id;
this.stcode = stcode;
this.stname = stname;
this.sex = sex;
this.phone = phone;
this.school = school;
this.address = address;
}
public Student() {
super();
}
}
service
package cc.vvxtoys.poi;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import cc.vvxtoys.util.CommonTools;
public class ExcelService {
private static ImportExcelUtils utils = new ImportExcelUtils();
public static Object getExcelParser(String path, String sheet, String start, String end) throws Exception {
InputStream is = null;
if (utils.isEmpty(path)) {
return new FileNotFoundException("file not found");
} else {
is = new FileInputStream(path);
}
if (path.endsWith(ImportExcelUtils.EXCEL_2003)) {
HSSFWorkbook workbook = new HSSFWorkbook(is);
return importXls(start, end, workbook, sheet);
} else if (path.endsWith(ImportExcelUtils.EXCEL_2007)) {
XSSFWorkbook workbook = new XSSFWorkbook(is);
return importXlsx(start, end, workbook, sheet);
} else {
is.close();
return new Exception("type error");
}
}
public static Map<String, Object> importXls(String start, String end, HSSFWorkbook workbook, String sheet) {
Map<String, Object> result = new HashMap<String, Object>();
List<Student> sList = new ArrayList<>();
HSSFSheet hssfSheet = null;
// 如果传过来sheet页的名称,取当前sheet页,否则遍历所有sheet
if (!utils.isEmpty(sheet)) {
hssfSheet = workbook.getSheet(sheet);
} else {
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
hssfSheet = workbook.getSheetAt(i);
int maxIndex = hssfSheet.getLastRowNum();// 获取最大下标
int startIndex = 0;
int endIndex = 0;
if (utils.isEmpty(start)) {
start = String.valueOf(0);
}
if (utils.isEmpty(end)) {
end = maxIndex + String.valueOf(1);
}
if (Integer.parseInt(start) <= 1) {
startIndex = 1;
} else {
startIndex = Integer.parseInt(start) - 1;
}
if (Integer.parseInt(end) > maxIndex) {
endIndex = maxIndex + 1;
} else {
endIndex = Integer.parseInt(end);
}
for (int j = startIndex; j < endIndex; j++) {
Student student = new Student();
HSSFRow row = hssfSheet.getRow(j);
//空行
if (utils.isBlank(row)) {
break;
}
HSSFCell c1 = row.getCell(0);
HSSFCell c2 = row.getCell(1);
HSSFCell c3 = row.getCell(2);
HSSFCell c4 = row.getCell(3);
HSSFCell c5 = row.getCell(4);
HSSFCell c6 = row.getCell(5);
HSSFCell c7 = row.getCell(6);
student.setId(CommonTools.get32UUID());//获取uuid 这个方法就不列出了,很简单
if(!utils.isEmpty(c1)){
String stcode = utils.getValue(c1);
student.setStcode(stcode);
}
if(!utils.isEmpty(c2)){
String stname = utils.getValue(c2);
student.setStname(stname);
}
if(!utils.isEmpty(c3)){
String sex = utils.getValue(c3);
student.setSex(sex);
}
if(!utils.isEmpty(c4)){
String phone = utils.getValue(c4);
student.setPhone(phone);
}
if(!utils.isEmpty(c5)){
String birthday = utils.getValue(c5).replaceAll("/", "-");
student.setBirthday(birthday);
}
if(!utils.isEmpty(c6)){
String school = utils.getValue(c6);
student.setSchool(school);
}
if(!utils.isEmpty(c7)){
String address = utils.getValue(c7);
student.setAddress(address);
}
sList.add(student);
}
}
}
result.put("sList", sList);
return result;
}
public static Map<String, Object> importXlsx(String start, String end, XSSFWorkbook workbook, String sheet) {
Map<String, Object> result = new HashMap<String, Object>();
List<Student> sList = new ArrayList<>();
XSSFSheet xssfSheet = null;
// 如果传过来sheet页的名称,取当前sheet页,否则遍历所有sheet
if (!utils.isEmpty(sheet)) {
xssfSheet = workbook.getSheet(sheet);
} else {
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
xssfSheet = workbook.getSheetAt(i);
int maxIndex = xssfSheet.getLastRowNum();// 获取最大下标
int startIndex = 0;
int endIndex = 0;
if (utils.isEmpty(start)) {
start = String.valueOf(0);
}
if (utils.isEmpty(end)) {
end = maxIndex + String.valueOf(1);
}
if (Integer.parseInt(start) <= 1) {
startIndex = 1;
} else {
startIndex = Integer.parseInt(start) - 1;
}
if (Integer.parseInt(end) > maxIndex) {
endIndex = maxIndex + 1;
} else {
endIndex = Integer.parseInt(end);
}
for (int j = startIndex; j < endIndex; j++) {
Student student = new Student();
XSSFRow row = xssfSheet.getRow(j);
XSSFCell c1 = row.getCell(0);
XSSFCell c2 = row.getCell(1);
XSSFCell c3 = row.getCell(2);
XSSFCell c4 = row.getCell(3);
XSSFCell c5 = row.getCell(4);
XSSFCell c6 = row.getCell(5);
XSSFCell c7 = row.getCell(6);
student.setId(CommonTools.get32UUID());//uuid
if(!utils.isEmpty(c1)){
String stcode = utils.getValue(c1);
student.setStcode(stcode);
}
if(!utils.isEmpty(c2)){
String stname = utils.getValue(c2);
student.setStname(stname);
}
if(!utils.isEmpty(c3)){
String sex = utils.getValue(c3);
student.setSex(sex);
}
if(!utils.isEmpty(c4)){
String phone = utils.getValue(c4);
student.setPhone(phone);
}
if(!utils.isEmpty(c5)){
String birthday = utils.getValue(c5).replaceAll("/", "-");
student.setBirthday(birthday);
}
if(!utils.isEmpty(c6)){
String school = utils.getValue(c6);
student.setSchool(school);
}
if(!utils.isEmpty(c7)){
String address = utils.getValue(c7);
student.setAddress(address);
}
sList.add(student);
}
}
}
result.put("sList", sList);
return result;
}
public static void main(String[] args) throws Exception {
String path = "D:\\Desktop\\2.xlsx"; //文件路径
String start = "0"; //开始条数
String end = "10";//结束条数
String sheet = null;//sheet页名称
ExcelService service = new ExcelService();
Map<String, Object> result = (Map<String, Object>) service.getExcelParser(path, sheet, start, end);
}
}
POI导入demo的更多相关文章
- POI导入导出
一.使用POI导出Execl表格 需要的jar包 package cn.yxj.poi; import java.io.FileOutputStream; import java.util.Date; ...
- Java利用POI导入导出Excel中的数据
首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...
- Java中使用poi导入、导出Excel
一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...
- poi 导入导出的api说明(大全)
原文链接:http://www.cnblogs.com/qingruihappy/p/8443101.html poi 导入导出的api说明(大全) 一. POI简介 ApachePOI是Apache ...
- poi导入Excel,数字科学记数法转换
在这里分享一下使用poi 导入Excel时 把数字转换为科学记数法的解决方法: 就是使用DecimalFormat对 i 进行了格式化 结果为:
- POI导入和导出Excel总结
POI导入和导出Excel总结 POI使用总结 1.POI读取Excel 打开工作簿的方式有以下两种简单的应用,POI读取和输出工作簿文件都可以通过以下两种方式来声明: //通过输入流的方式打开本 ...
- 使用POI导入EXCEL报java.lang.IncompatibleClassChangeError
使用POI导入xls格式的excel报java.lang.IncompatibleClassChangeError异常,而导入xlsx正常. oracle.apps.fnd.framework.OAE ...
- Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)
ava 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包) 假设现在要做一个通用的导入方法: 要求: 1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件 ...
- poi导入读取时间格式问题
万能处理方案: 所有日期格式都可以通过getDataFormat()值来判断 yyyy-MM-dd-----14 yyyy年m月d日--- 31 yyyy年m月-------57 m月d日 ---- ...
随机推荐
- JAVA日常之二
一.装箱.拆箱 int i=1; Integer iobj=i;(自动装箱) 简单理解为,将基本数据类型(i)经过装箱变成对象(iobj): Integer iobj; int i= iobj;(自动 ...
- PHP之魔术方法
PHP中的魔术方法: PHP的魔术方法主要是在特定的条件下执行相应的魔术方法.这和很多框架中的钩子函数有些类似,不同的是,钩子函数是在生命周期的某个周期内自动执行,而魔术方法是在触发某种条件下自动 ...
- python_ 基本语法
一.基础知识: 1.鸡汤 摘抄至 :简明 python 教程 在人生中取得成功,与其说靠天才与机会,不如说靠专注与毅力! Python 特点:简单.易于学习(简单得语法体系).自由且开发.高级语言.跨 ...
- HTML5-全局属性
HTML5-全局属性 HTML 属性赋予元素意义和语境.全局属性可用于任何 HTML 元素. contentEditable - 规定元素内容是否可编辑.- 注释:如果元素未设置 contentedi ...
- 2019OO第一单元作业总结
OO第一单元作业的主题是求导,下面将分三次作业分别总结一下. --------------------------------------------------------------------- ...
- face_recognition
人脸定位import face_recognition image = face_recognition.load_image_file("your_file.jpg") face ...
- find命令简单使用
find命令是Linux系统查找文件的命令,能帮助用户在使用.管理Linux的日常事务时方便的查找出用户所需要的文件,find命令基本格式是:find [路径] [选项] [操作]. 列举一下find ...
- linux下软件安装的几种方式
linux下软件安装的几种方式(主要有源码安装, rpm安装, yum安装). 一:源码安装 几乎所有的开源软件都支持在Linux下运行,而这些软件一般都以源码形式发放,只需要Linux安装了gcc. ...
- day02格式化输出等
1.格式化输出 format % 占位符 %s:str, %d: dight, %f: float 字符串多行用三个单引号或三个双引号 %%5,百分之五,转义字符%.想 ...
- MySQL 设置root密码报错:mysqladmin: connect to server at 'localhost' failed
MySQL 设置root密码报错:mysqladmin: connect to server at 'localhost' failed 1.安装完MySQL设置root密码报错如下 [root@vm ...