前言

使用上篇博文的导入方法,写一个简单的导入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. ubuntu16.04中如何启用floodlight的其中一种方式

    1. 提前一台安装好mininet,另一台安装好floodlight 2. 在mininet里面的custom文件夹下自定义文件ProjectGroup10_Topology.py from mini ...

  2. java基本数据类型和运算符

    一.基本数据类型 种类: 内置数据类型 引用数据类型 1.内置数据类型 一共有八种基本类型,六个数字类型(四个整数类型,两个浮点型),一个布尔型,一个字符类型. (1)byte: byte数据类型是8 ...

  3. [SQL] 从文本中提取数值

    现需求从上方测试数据的“备注”列中提取出金额 目前有两个方法比较容易实现: 1.首先比较容易想到的就是利用函数stuff删除掉所有的非数值字符. STUFF ( character_expressio ...

  4. WiFi万能钥匙 for Macv1.1.0中文版

    wifi万能钥匙 for Mac是一款运行在Mac平台上的免费WiFi热点软件,Mac分享无线流量.管理WiFi连接的必备神器.WiFi万能钥匙Mac版内置千万Wi-Fi热点数据,随时随地轻松接入无线 ...

  5. xamarin android 汉字转拼音

    使用微软的 ChnCharInfo.dll,这个库不仅能在 windows 下完美运行,竟单独在android下也可以完美运行,直接引用即可.当然是使用 visual studio 开发安卓啦,具体用 ...

  6. Linux----------rsync的介绍及安装使用

    目录 一.rsync的介绍 1.1rsync的特点 二.rsync命令 三.rsync的ssh认证协议 四.ssh协议方式使用方法 五.rsync协议方式使用方法即 (rsync + inotifu- ...

  7. stolon cloud native postgresql 高可用方案

    stolon方案与patroni 类似,是一个新的pg ha 方案 包含的组件 keeper:它管理一个PostgreSQL实例,汇聚到由领导者sentinel计算的clusterview. sent ...

  8. 爬虫-day02-抓取和分析

    ###页面抓取### 1.urllib3     是一个功能强大且好用的HTTP客户端,弥补了Python标准库中的不足     安装: pip install urllib3     使用: imp ...

  9. iOS9.3描述文件怎么安装

    iOS9.3 beta描述文件安装教程:1.复制以下地址:http://bbs.feng.com/plugin.php?id=attachment_download:tongji&aid=11 ...

  10. C#控制台下测试多线程的源码

    下边代码是关于C#控制台下测试多线程的的代码,应该是对小伙伴有所用. class Program { static void Main(string[] args) { ThreadStart num ...