package excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cc.isystem.smartfox.company.persistence.mybatis.model.CmnPropertyStaff;
import cc.isystem.smartfox.company.persistence.mybatis.model.CompanyInfo;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
/**
* 首先读取一张表的信息
*
* @author Administrator
*
*/
public class TestExcelOne {
public static void main(String[] args) {
String BASE_FILE_LOCATION = "D:\\文件录入\\文件录入\\";
// 公司信息的对象列表
List<CompanyInfo> companyInfosList = new ArrayList<CompanyInfo>();
// 1. 先将数据读取进内存
ExcelReader readerBase = ExcelUtil.getReader(BASE_FILE_LOCATION + "物业企业录入资料\\基础信息表.xlsx");
List<List<Object>> readAllBase = readerBase.read();
for (int i = 1; i < readAllBase.size(); i++) {
CompanyInfo companyInfo = new CompanyInfo();
String name = (String) readAllBase.get(i).get(0);
String local = (String) readAllBase.get(i).get(1);
// System.out.println(readAllBase.get(i).get(3));
String accommodation = (String) readAllBase.get(i).get(2);
Date founder = (Date) readAllBase.get(i).get(3);
String regNo = (String) readAllBase.get(i).get(4);
String legalName = (String) readAllBase.get(i).get(5);
String legalPhone = null;
Object legalPhoneTemp = readAllBase.get(i).get(6);
if (legalPhoneTemp != null) {
legalPhone = legalPhoneTemp.toString();
}
// String Capital = readAllBase.get(i).get(7).toString();
// String nonLocalStaffNumber = (String) readAllBase.get(i).get(8);
// String ManagerStaffNumber = (String) readAllBase.get(i).get(9);
// String mediumGradeProfessionalNumber = (String) readAllBase.get(i).get(10);
// String businessScope = (String) readAllBase.get(i).get(11);
companyInfo.setName(name);
if (local == "本地") {
companyInfo.setLocal("Y");
} else {
companyInfo.setLocal("N");
}
companyInfo.setAccommodation(accommodation);
companyInfo.setFounder(founder);
companyInfo.setRegNo(regNo);
companyInfo.setLegalName(legalName);
companyInfo.setLegalPhone(legalPhone);
// companyInfo.setCapital(Capital);
// 剩余一些基本信息由于没有及时的更新导致 先实现功能
companyInfosList.add(companyInfo);
String fileLoction = (String) readAllBase.get(i).get(0);
}
// 再根据获取到的公司信息获取人员信息--- 路径与公司名称相关
for (int i = 0; i < companyInfosList.size(); i++) {
String name = companyInfosList.get(i).getName();
// 找不到指定的表格直接处理异常
try {
InputStream inputStream = null;
try {
FileInputStream file = new FileInputStream(
BASE_FILE_LOCATION + "物业企业录入资料" + "\\" + name + "\\" + "物业管理企业人员基本情况表.xlsx");
inputStream = file;
} catch (Exception e) {
System.out.println(
BASE_FILE_LOCATION + "物业企业录入资料" + "\\" + name + "\\" + "物业管理企业人员基本情况表.xlsx" + "找不到!");
}
ExcelReader reader = ExcelUtil.getReader(inputStream);
List<List<Object>> readAllStaff = reader.read();
// 同样获取表的数据进行封装
List<CmnPropertyStaff> cmnPropertyStaffs = new ArrayList<CmnPropertyStaff>();
for (int j = 1; j < readAllStaff.size(); j++) {
try {
CmnPropertyStaff cmnPropertyStaff = new CmnPropertyStaff();
// cmnPropertyStaff.setCompanyId(companyId); // 需要和公司id进行绑定
List<Object> first = readAllStaff.get(j);
if ("" == first.get(2) || null == first.get(2)) {
continue;
}
Long degrees = (Long) first.get(2);
String degreess = "" + degrees;
// System.out.println(degreess+"**********************************************");
if (null == first.get(0) || "" == first.get(0)) {
// break a;
}
a: {
}
cmnPropertyStaff.setName((String) first.get(0));
// 性别、籍贯字段需要在实体类中添加
// System.out.println("测试是否是一个表格读取不到");
// Object c = first.get(j).get(1);
cmnPropertyStaff.setDegree(degreess);
// System.out.println("测试是否是第二个表格读取不到");
if ("" == first.get(3) || null == first.get(3)) {
continue;
}
cmnPropertyStaff.setDuty((String) first.get(3));
// System.out.println("测试是否是第三个表格读取不到");
if ("" == first.get(4) || null == first.get(4)) {
continue;
}
cmnPropertyStaff.setTitle((String) first.get(4));
cmnPropertyStaffs.add(cmnPropertyStaff);
} catch (Exception e) {
System.out.println(e);
System.out.println("读取表失败!" + companyInfosList.get(i) + readAllStaff.get(j).toString());
}
}
if (cmnPropertyStaffs != null) {
for (int m = 0; m < cmnPropertyStaffs.size(); m++) {
String message = cmnPropertyStaffs.get(m).toString();
LogTest logTest = new LogTest();
logTest.logger(message, "D:\\logs\\heyuanexcel");
}
}
reader.close();
} catch (Exception e) {
System.out.println(e);
System.out
.println("找不到:" + BASE_FILE_LOCATION + "物业企业录入资料" + "\\" + name + "\\" + "物业管理企业人员基本情况表.xlsx");
}
}
}
}
两个excel
猜你喜欢
转载自blog.csdn.net/qq_42664961/article/details/108927170
今日推荐
周排行