前言

使用上篇博文的导入方法,写一个简单的导入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的更多相关文章

  1. POI导入导出

    一.使用POI导出Execl表格 需要的jar包 package cn.yxj.poi; import java.io.FileOutputStream; import java.util.Date; ...

  2. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  3. Java中使用poi导入、导出Excel

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  4. poi 导入导出的api说明(大全)

    原文链接:http://www.cnblogs.com/qingruihappy/p/8443101.html poi 导入导出的api说明(大全) 一. POI简介 ApachePOI是Apache ...

  5. poi导入Excel,数字科学记数法转换

    在这里分享一下使用poi 导入Excel时 把数字转换为科学记数法的解决方法: 就是使用DecimalFormat对 i 进行了格式化 结果为:

  6. POI导入和导出Excel总结

    POI导入和导出Excel总结   POI使用总结 1.POI读取Excel 打开工作簿的方式有以下两种简单的应用,POI读取和输出工作簿文件都可以通过以下两种方式来声明: //通过输入流的方式打开本 ...

  7. 使用POI导入EXCEL报java.lang.IncompatibleClassChangeError

    使用POI导入xls格式的excel报java.lang.IncompatibleClassChangeError异常,而导入xlsx正常. oracle.apps.fnd.framework.OAE ...

  8. Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)

    ava 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包) 假设现在要做一个通用的导入方法: 要求: 1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件 ...

  9. poi导入读取时间格式问题

    万能处理方案: 所有日期格式都可以通过getDataFormat()值来判断 yyyy-MM-dd-----14 yyyy年m月d日--- 31 yyyy年m月-------57 m月d日  ---- ...

随机推荐

  1. EmptyBeanUtil

    package com.rscode.credits.util; import java.util.List; /** * * 判断实体是否为空 * @author tn * */ public cl ...

  2. switch_case注意事项

    1.switch 语句有至少一个 case 代码块和一个可选的 default 代码块. 这里的 switch 从第一个 case 分支比较 a 的值,值为 3 匹配失败.然后比较 4.匹配,所以从  ...

  3. python学习线路

    第一章:计算机基础 https://www.cnblogs.com/koukouku/p/10646025.html 1.1 计算机认识 1.2操作系统 1.3 计算机的运算(进制) 1.4解释器/编 ...

  4. javaee设计模型简介

    (一)五种模式 1.单例模式 在某些情况下,有些对象只需要一个就可以了,即每个类只需要一个实例.例如,一台计算机上的可以连接多台打印机,但是该计算机上的打印程序只能有一个,这里就可以通过单例模式来避免 ...

  5. Web前端学习第一天

    1.SQL注入攻击的发生 select username, email ,descl from users where id=1; 可能会被伪造成 1 union select password,1, ...

  6. 一次奇妙的http请求之旅

    TCP/IP不是一个协议,而是一个协议族的统称.里面包括IP协议.IMCP协议.TCP协议. 这里有几个需要注意的知识点: 互联网地址:也就是IP地址,一般为网络号+子网号+主机号 域名系统:通俗的来 ...

  7. py2x与py3x区别

    https://blog.csdn.net/samxx8/article/details/21535901

  8. Java 调用 Shell 命令

    近日项目中有这样一个需求:系统中的外币资金调度完成以后,要将调度信息生成一个Txt文件,然后将这个Txt文件发送到另外一个系统(Kondor)中.生成文件自然使用OutputStreamWirter了 ...

  9. 西门子S7-200SMART PLC视频教程(百度网盘)

    西门子S7-200SMART PLC视频教程(百度网盘)西门子S7-200 SMART PLC是西门子公司推出的高性价比小型plc,是国内广泛使用的S7-200PLC的更新换代产品. 以下是关于S7- ...

  10. MySQL常用命令(数据库,表相关的命令)

    数据库相关命令 显示数据库列表 mysql> SHOW  DATABASES; 创建数据库 mysql> CREATE  DATABASE  库名; 如下,创建一个名为crashcours ...