/********************************************************工具类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