/********************************************************工具类start*****************************************************************/

package cn.cnnho.utils;

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 java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
* @功能描述: excel文件解析工具

*/
public class BatchUploadUtlis {

/**
* @功能描述:将excel解析为list结合返回
* @参数说明:@param excelPath excel路径
* @参数说明:@return
* @作者: Jgx
* @创建时间:2019年4月26日 下午3:06:54
*/
@SuppressWarnings("resource")
public List<BatchUploadInfo> mentondExecute(String excelPath) {
BatchUploadInfo buInfo =null;
List<BatchUploadInfo> buList = new ArrayList<BatchUploadInfo>();
try {
File excel = new File(excelPath);

//判断文件是否存在
if (excel.isFile() && excel.exists()) {

//转移.特殊字符
String[] split = excel.getName().split("\\.");
Workbook wb;

//根据文件后缀(xls/xlsx)进行判断
if ( "xls".equals(split[1])){
FileInputStream fis = new FileInputStream(excel); //文件流对象
wb = new HSSFWorkbook(fis);
}else if ("xlsx".equals(split[1])){
wb = new XSSFWorkbook(excel);
}else {
System.out.println("文件类型错误!");
return null;
}

/**
* 开始解析
*/
Sheet sheet = wb.getSheetAt(0);//读取sheet 0
int firstRowIndex = sheet.getFirstRowNum()+1;//第一行是列名,所以不读
int lastRowIndex = sheet.getLastRowNum();//总行数
System.out.println("总行数: "+lastRowIndex);

/* 遍历行*/
for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
Row row = sheet.getRow(rIndex);
if (row != null) {
int firstCellIndex = row.getFirstCellNum();
int lastCellIndex = row.getLastCellNum();

buInfo = new BatchUploadInfo();

/*遍历列*/
for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {
Cell cell = row.getCell(cIndex);
switch(cIndex){
case 1:
buInfo.setDepName(cell.toString());break;
case 2:
buInfo.setStorageLocation(cell.toString());break;
case 3:
buInfo.setContractName(cell.toString());break;
case 4:
buInfo.setContractNo(cell.toString());break;
case 5:
buInfo.setPartyA(cell.toString());break;
case 6:
buInfo.setPartyB(cell.toString());break;
case 7:
buInfo.setHeir(cell.toString());break;
case 8:
buInfo.setTheSignOne(cell.toString());break;
case 9:
buInfo.setTheSignDate(cell.toString());break;
case 10:
buInfo.setElectronicEdition(cell.toString());break;
case 11:
buInfo.setContent(cell.toString());break;
case 12:
buInfo.setStorageType(cell.toString());break;
case 13:
buInfo.setIsSame(cell.toString());break;
case 14:
buInfo.setStorageDate(cell.toString());break;
case 15:
buInfo.setRemark(cell.toString());break;
default:
break;
}
}
buList.add(buInfo);
}
}
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
e.printStackTrace();
}
return buList;
}

public static void main(String[] args) {
BatchUploadUtlis buu = new BatchUploadUtlis();
List<BatchUploadInfo> list = buu.mentondExecute("F:\\工作资料\\20190228档案管理\\合同\\合同\\合同登记表(测试).xls");
for(int i = 0 ; i < list.size() ; i++) {
System.out.println(list.get(i).getContractNo());
}
}
}

/********************************************************工具类end*****************************************************************/

/********************************************************实体类start*****************************************************************/

package cn.cnnho.utils;

/**
* @功能描述:bean

*/
public class BatchUploadInfo {

private String id;
private String depName;
private String storageLocation;
private String contractName;
private String contractNo;
private String partyA;
private String partyB;
private String heir;
private String theSignOne;
private String theSignDate;
private String electronicEdition;
private String content;
private String storageType;
private String isSame;
private String storageDate;
private String remark;

public String getIsSame() {
return isSame;
}
public void setIsSame(String isSame) {
this.isSame = isSame;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDepName() {
return depName;
}
public void setDepName(String depName) {
this.depName = depName;
}
public String getStorageLocation() {
return storageLocation;
}
public void setStorageLocation(String storageLocation) {
this.storageLocation = storageLocation;
}
public String getContractName() {
return contractName;
}
public void setContractName(String contractName) {
this.contractName = contractName;
}
public String getContractNo() {
return contractNo;
}
public void setContractNo(String contractNo) {
this.contractNo = contractNo;
}
public String getPartyA() {
return partyA;
}
public void setPartyA(String partyA) {
this.partyA = partyA;
}
public String getPartyB() {
return partyB;
}
public void setPartyB(String partyB) {
this.partyB = partyB;
}
public String getHeir() {
return heir;
}
public void setHeir(String heir) {
this.heir = heir;
}
public String getTheSignOne() {
return theSignOne;
}
public void setTheSignOne(String theSignOne) {
this.theSignOne = theSignOne;
}
public String getTheSignDate() {
return theSignDate;
}
public void setTheSignDate(String theSignDate) {
this.theSignDate = theSignDate;
}
public String getElectronicEdition() {
return electronicEdition;
}
public void setElectronicEdition(String electronicEdition) {
this.electronicEdition = electronicEdition;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getStorageType() {
return storageType;
}
public void setStorageType(String storageType) {
this.storageType = storageType;
}
public String getStorageDate() {
return storageDate;
}
public void setStorageDate(String storageDate) {
this.storageDate = storageDate;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}

}

/********************************************************实体类end****************************************************************/

所需jar包

commons-collections4-4.1.jar
curvesapi-1.04.jar
poi-3.15.jar
poi-ooxml-3.15.jar
poi-ooxml-schemas-3.15.jar
stax-api-1.0.1.jar
xmlbeans-2.6.0.jar

Java解析excel文档并以List<T>输出的更多相关文章

  1. Android解析Excel文档完整示例

    MainActivity如下: package cc.testexcel; import java.io.File; import jxl.Cell; import jxl.CellType; imp ...

  2. java 解析XML文档

    Java 解析XML文档 一.解析XML文档方式: 1.DOM方式:将整个XML文档读取到内存中,按照XML文件的树状结构图进行解析. 2.SAX方式:基于事件的解析,只需要加载XML中的部分数据,优 ...

  3. Java解析XML文档(简单实例)——dom解析xml

      一.前言 用Java解析XML文档,最常用的有两种方法:使用基于事件的XML简单API(Simple API for XML)称为SAX和基于树和节点的文档对象模型(Document Object ...

  4. POI使用:用poi接口不区分xls/xlsx格式解析Excel文档(41种日期格式解析方法,5种公式结果类型解析方法,3种常用数值类型精度控制办法)

    一.使用poi解析excel文档 注:全部采用poi接口进行解析,不需要区分xls.xlsx格式,不需要判断文档类型. poi中的日期格式判断仅支持欧美日期习惯,对国内的日期格式并不支持判断,怎么办? ...

  5. Java解析XML文档——dom解析xml

    一.前言 用Java解析XML文档,最常用的有两种方法:使用基于事件的XML简单API(Simple API for XML)称为SAX和基于树和节点的文档对象模型(Document Object M ...

  6. Oracle PLSQL读取(解析)Excel文档

    http://www.itpub.net/thread-1921612-1-1.html !!!https://code.google.com/p/plsql-utils/ Introduction介 ...

  7. 浅谈用java解析xml文档(四)

    继续接上一文,这一阵子因为公司项目加紧,导致最后一个解析xml文档的方式,还没有总结,下面总结使用dom4J解析xml. DOM4J(Document Object Model for Java) 使 ...

  8. 浅谈用java解析xml文档(三)

    接上一篇,本文介绍使用JDOM解析xml文档, 首先我们还是应该知道JDOM从何而来,是Breet Mclaughlin和Jason Hunter两大Java高手的创作成果,2000年初, JDOM作 ...

  9. Java解析word文档

    背景 在互联网教育行业,做内容相关的项目经常碰到的一个问题就是如何解析word文档. 因为系统如果无法智能的解析word,那么就只能通过其他方式手动录入word内容,效率低下,而且人工成本和录入出错率 ...

随机推荐

  1. 15 输入三个整数x,y,z,请把这三个数由小到大输出。

    题目:输入三个整数x,y,z,请把这三个数由小到大输出. public class _015ThreeNumberSort { public static void main(String[] arg ...

  2. jQuary总结9:html()的常见用法

    1html() 不传参数 用于获取内容 //html <div> <p></p> <span></span> 文本 </div> ...

  3. 更改oracle数据库字符集

    A.oracle server 端 字符集查询  select userenv('language') from dual 其中NLS_CHARACTERSET 为server端字符集 NLS_LAN ...

  4. Generated by NetworkManager、ubuntu DNS设置丢失(network-manager造成的情况)

    方法一:去掉重启 方法二:卸载network-manager 实测网络不稳,经常掉线(kalinux2.0环境)

  5. SPOJ - AMR11J ——(BFS)

    The wizards and witches of Hogwarts School of Witchcraft found Prof. Binn's History of Magic lesson ...

  6. cenos7切换阿里源

    备份并安装base reop源 cd /etc/yum.repos.d sudo mv CentOS-Base.repo CentOS-Base.repo.bak 下载阿里源并配置 sudo wget ...

  7. opencv—读取一张图片并滤波

    #include <opencv2\opencv.hpp> #include <iostream> #include <string> using namespac ...

  8. linux 管道与重定向

    命令行shell数据流有如下定义: 通过管道和重定向可以控制CLI的数据流

  9. .net 程序集的加载与反射

    一. 程序集的加载: 在CLR内部使用System.Reflection.Assembly类的静态LoadFrom方法尝试加载程序集. LoadFrom方法在内部调用Assembly的Load方法,将 ...

  10. auc的本质

    AUC的本质 定义 auc是roc曲线下的面积.其中,roc是横坐标为fpr,纵坐标是tpr的坐标系上的曲线. TPR(true positive rate):所有正样本中被预测为正的比例 FPR(f ...