|
@@ -0,0 +1,849 @@
|
|
|
+/**
|
|
|
+ * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
|
|
|
+ */
|
|
|
+package com.jeeplus.common.utils.excel;
|
|
|
+
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.jeeplus.common.utils.Reflections;
|
|
|
+import com.jeeplus.common.utils.excel.annotation.ExcelField;
|
|
|
+import com.jeeplus.modules.ruralprojectrecords.entity.ProjectReportSignatureInfo;
|
|
|
+import com.jeeplus.modules.sys.entity.Area;
|
|
|
+import com.jeeplus.modules.sys.entity.Office;
|
|
|
+import com.jeeplus.modules.sys.entity.User;
|
|
|
+import com.jeeplus.modules.sys.utils.DictUtils;
|
|
|
+import com.jeeplus.modules.sys.utils.UserUtils;
|
|
|
+import com.jeeplus.modules.workjobgrade.entity.WorkJobGrade;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
+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 org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.text.NumberFormat;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 导入Excel文件(支持“XLS”和“XLSX”格式)
|
|
|
+ * @author jeeplus
|
|
|
+ * @version 2013-03-10
|
|
|
+ */
|
|
|
+public class ImportExcelNew {
|
|
|
+
|
|
|
+ private static Logger log = LoggerFactory.getLogger(ImportExcelNew.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 工作薄对象
|
|
|
+ */
|
|
|
+ private Workbook wb;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 工作表对象
|
|
|
+ */
|
|
|
+ private Sheet sheet;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 标题行号
|
|
|
+ */
|
|
|
+ private int headerNum;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public ImportExcelNew(String fileName, int headerNum)
|
|
|
+ throws InvalidFormatException, IOException {
|
|
|
+ this(new File(fileName), headerNum);
|
|
|
+ }
|
|
|
+ public int sheetCirculation() {
|
|
|
+ int sheetCount = -1;
|
|
|
+ sheetCount = this.wb.getNumberOfSheets();
|
|
|
+ return sheetCount;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public ImportExcelNew(File file, int headerNum)
|
|
|
+ throws InvalidFormatException, IOException {
|
|
|
+ this(file, headerNum, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
+ * @param sheetIndex 工作表编号
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public ImportExcelNew(String fileName, int headerNum, int sheetIndex)
|
|
|
+ throws InvalidFormatException, IOException {
|
|
|
+ this(new File(fileName), headerNum, sheetIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
+ * @param sheetIndex 工作表编号
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public ImportExcelNew(File file, int headerNum, int sheetIndex)
|
|
|
+ throws InvalidFormatException, IOException {
|
|
|
+// this(file.getName(), new FileInputStream(file), headerNum, sheetIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
+ * @param sheetIndex 工作表编号
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public List ImportExcelNew(MultipartFile multipartFile, int headerNum, int sheetIndex)
|
|
|
+ throws InvalidFormatException, IOException {
|
|
|
+ return this.list(multipartFile.getOriginalFilename(), multipartFile.getInputStream(), headerNum, sheetIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @param headerNum 标题行号,数据行号=标题行号+1
|
|
|
+ * @param sheetIndex 工作表编号
|
|
|
+ * @throws InvalidFormatException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public List list(String fileName, InputStream is, int headerNum, int sheetIndex)
|
|
|
+ throws NullPointerException, IOException {
|
|
|
+ if (StringUtils.isBlank(fileName)){
|
|
|
+ throw new RuntimeException("导入文档为空!");
|
|
|
+ }else if(fileName.toLowerCase().endsWith("xls")){
|
|
|
+ this.wb = new HSSFWorkbook(is);
|
|
|
+ }else if(fileName.toLowerCase().endsWith("xlsx")){
|
|
|
+ this.wb = new XSSFWorkbook(is);
|
|
|
+ }else{
|
|
|
+ throw new RuntimeException("文档格式不正确!");
|
|
|
+ }
|
|
|
+ if (this.wb.getNumberOfSheets()<sheetIndex){
|
|
|
+ throw new RuntimeException("文档中没有工作表!");
|
|
|
+ }
|
|
|
+ this.sheet = this.wb.getSheetAt(sheetIndex);
|
|
|
+ this.headerNum = headerNum;
|
|
|
+
|
|
|
+ int firstRowNum = sheet.getFirstRowNum();
|
|
|
+ Row firstRow = sheet.getRow(firstRowNum);
|
|
|
+ int lastCellNum = firstRow.getLastCellNum();
|
|
|
+
|
|
|
+ List<ProjectReportSignatureInfo> list = new ArrayList<ProjectReportSignatureInfo>();
|
|
|
+
|
|
|
+ if(lastCellNum<3){
|
|
|
+ throw new RuntimeException("当前表格无数据!");
|
|
|
+ }else{
|
|
|
+ for(int i=2;i<lastCellNum;i++){ //第三列 0,1,2
|
|
|
+ //到每一列 获取对应行单元格数据
|
|
|
+ ProjectReportSignatureInfo projectReportSignatureInfo = new ProjectReportSignatureInfo();
|
|
|
+ if(null != sheet.getRow(0).getCell(i) && StringUtils.isNotBlank(sheet.getRow(0).getCell(i).toString())) {
|
|
|
+ projectReportSignatureInfo.setProjectReportId(sheet.getRow(0).getCell(i).toString());
|
|
|
+ if(sheet.getRow(1).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setSubmitFee(sheet.getRow(1).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(2).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setAuthorizeFee(sheet.getRow(2).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(3).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setConstructionUnitAssessmentFee(sheet.getRow(3).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(4).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setAuthorizeFeeDeductAssessmentFee(sheet.getRow(4).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(5).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setConstructionUnit(sheet.getRow(5).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(6).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setDescriptionOfDeduction(sheet.getRow(6).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(7).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setDescriptionOfExceedingTheContractAmount(sheet.getRow(7).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(8).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setDescriptionOfSpecialMatters(sheet.getRow(8).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(9).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(9).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setStampDate(sheet.getRow(9).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(10).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setPrintQuantity(sheet.getRow(10).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(11).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(11).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setReportPreparationDate(sheet.getRow(11).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(12).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(12).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setAgreedStartDate(sheet.getRow(12).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(13).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(13).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setAgreedEndDate(sheet.getRow(13).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(14).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setConsultant(sheet.getRow(14).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(15).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setSelfCalibrationOpinion(sheet.getRow(15).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(16).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setReviewOpinion(sheet.getRow(16).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(17).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(17).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setReviewDate(sheet.getRow(17).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(18).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(18).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setDateOfConsultationReport(sheet.getRow(18).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(19).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(19).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setStartDateOfConsultingOperation(sheet.getRow(19).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(20).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(20).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setEndDateOfConsultation(sheet.getRow(20).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(21).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(21).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setStartAuditDate(sheet.getRow(21).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(22).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setProjectScaleContentScope(sheet.getRow(22).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(23).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setMainContentsOfProject(sheet.getRow(23).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(24).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setTotalContractPrice(sheet.getRow(24).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(25).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(25).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setContractStartDate(sheet.getRow(25).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(26).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(26).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setContractEndDate(sheet.getRow(26).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(27).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(27).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setActualStartDate(sheet.getRow(27).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(28).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(28).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setActualEndDate(sheet.getRow(28).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(29).getCell(i) != null) {
|
|
|
+ if(HSSFDateUtil.isCellDateFormatted(sheet.getRow(29).getCell(i))){
|
|
|
+ projectReportSignatureInfo.setCompletionAcceptanceDate(sheet.getRow(29).getCell(i).getDateCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(sheet.getRow(30).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setDesignUnit(sheet.getRow(30).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(31).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setConstructionControlUnit(sheet.getRow(31).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(32).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setFollowUpAuditUnit(sheet.getRow(32).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(33).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setEngineeringDesignChangeOrder(sheet.getRow(33).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(34).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setConfirmationSheetOfQuantities(sheet.getRow(34).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(35).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setProjectVisa(sheet.getRow(35).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(36).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setQualityPriceConfirmationOfEngineeringMaterials(sheet.getRow(36).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(37).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setListmaterialsEquipmentByA(sheet.getRow(37).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(38).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setProjectRewardAndDamages(sheet.getRow(38).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(39).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setCharge(sheet.getRow(39).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(40).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setTaxes(sheet.getRow(40).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(41).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setDurationDifferenceCausesAndResponsibilities(sheet.getRow(41).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(42).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setQualityDifferenceCausesAndResponsibilities(sheet.getRow(42).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(43).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setOtherContentsAndExpenses(sheet.getRow(43).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(44).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setProjectCostConsultingContract(sheet.getRow(44).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(45).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setSettlementPricingBasisDocument(sheet.getRow(45).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(46).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setMaterialsAndEquipmentByA(sheet.getRow(46).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(47).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setaMaterialsByB(sheet.getRow(47).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(48).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setRewardAndPunishmentFees(sheet.getRow(48).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(49).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setWaterAndElectricityFeesHandleSituation(sheet.getRow(49).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(50).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setConstructionQualityWarrantyDeposit(sheet.getRow(50).getCell(i).toString());
|
|
|
+ }
|
|
|
+ if(sheet.getRow(51).getCell(i) != null) {
|
|
|
+ projectReportSignatureInfo.setTaxDifferenceCalculationAndProcessing(sheet.getRow(51).getCell(i).toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ list.add(projectReportSignatureInfo);
|
|
|
+ }else {
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.debug("Initialize success.");
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ImportExcelNew() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取行对象
|
|
|
+ * @param rownum
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Row getRow(int rownum){
|
|
|
+ return this.sheet.getRow(rownum);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * header内容
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getHeaderStr(){
|
|
|
+ return this.sheet.getSheetName();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取数据行号
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public int getDataRowNum(){
|
|
|
+ return headerNum+1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取最后一个数据行号
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public int getLastDataRowNum(){
|
|
|
+ return this.sheet.getLastRowNum()+headerNum;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取最后一个列号
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public int getLastCellNum(){
|
|
|
+ return this.getRow(headerNum).getLastCellNum();
|
|
|
+ }
|
|
|
+
|
|
|
+ //实验
|
|
|
+
|
|
|
+ public int getRowNumNew(){
|
|
|
+ int firstRowNum = sheet.getFirstRowNum();
|
|
|
+ int lastRowNum = sheet.getLastRowNum();
|
|
|
+ Row firstRow = sheet.getRow(firstRowNum);
|
|
|
+ int firstCellNum = firstRow.getFirstCellNum();
|
|
|
+ int lastCellNum = firstRow.getLastCellNum();
|
|
|
+ System.out.println("第一行行号:" + firstRowNum);
|
|
|
+ System.out.println("最后一行行号:" + lastRowNum);
|
|
|
+ System.out.println("第一列列号:" + firstCellNum);
|
|
|
+ System.out.println("最后一列列号:" + lastCellNum);
|
|
|
+ return this.sheet.getFirstRowNum();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取单元格值
|
|
|
+ * @param row 获取的行
|
|
|
+ * @param column 获取单元格列号
|
|
|
+ * @return 单元格值
|
|
|
+ */
|
|
|
+ public Object getCellValue(Row row, int column) {
|
|
|
+ Object val = "";
|
|
|
+ try {
|
|
|
+ Cell cell = row.getCell(column);
|
|
|
+
|
|
|
+ if (cell != null) {
|
|
|
+ if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
|
|
|
+ // val = cell.getNumericCellValue();
|
|
|
+ // 当excel 中的数据为数值或日期是需要特殊处理
|
|
|
+ if (HSSFDateUtil.isCellDateFormatted(cell)) {
|
|
|
+ double d = cell.getNumericCellValue();
|
|
|
+ Date date = HSSFDateUtil.getJavaDate(d);
|
|
|
+ SimpleDateFormat dformat = new SimpleDateFormat(
|
|
|
+ "yyyy-MM-dd");
|
|
|
+ val = dformat.format(date);
|
|
|
+ } else {
|
|
|
+ NumberFormat nf = NumberFormat.getInstance();
|
|
|
+ nf.setGroupingUsed(false);// true时的格式:1,234,567,890
|
|
|
+ val = nf.format(cell.getNumericCellValue());// 数值类型的数据为double,所以需要转换一下
|
|
|
+ if(((String) val).contains(",")){
|
|
|
+ val = nf.parse(val.toString()).doubleValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
|
|
|
+ val = replaceBlank(cell.getStringCellValue());
|
|
|
+ } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
|
|
|
+ //val = cell.getCellFormula();
|
|
|
+ try {
|
|
|
+ val = String.valueOf(cell.getNumericCellValue());
|
|
|
+ if(String.valueOf(cell.getNumericCellValue()).indexOf("E")==-1){
|
|
|
+ val= String.valueOf(cell.getNumericCellValue());
|
|
|
+ }else {
|
|
|
+ NumberFormat formatter=NumberFormat.getNumberInstance();
|
|
|
+ formatter.setMinimumFractionDigits(4);
|
|
|
+ val=formatter.format(cell.getNumericCellValue());
|
|
|
+ if(((String) val).contains(",")){
|
|
|
+ val= val.toString().replaceAll(",","");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IllegalStateException e) {
|
|
|
+ val = String.valueOf(cell.getRichStringCellValue());
|
|
|
+ }
|
|
|
+ } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
|
|
|
+ val = cell.getBooleanCellValue();
|
|
|
+ } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
|
|
|
+ val = cell.getErrorCellValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ return val;
|
|
|
+ }
|
|
|
+ return val;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取导入数据列表
|
|
|
+ * @param cls 导入对象类型
|
|
|
+ * @param groups 导入分组
|
|
|
+ */
|
|
|
+ public <E> List<E> getDataList(Class<E> cls, int... groups) throws InstantiationException, IllegalAccessException{
|
|
|
+ List<Object[]> annotationList = Lists.newArrayList();
|
|
|
+ // Get annotation field
|
|
|
+ Field[] fs = cls.getDeclaredFields();
|
|
|
+ for (Field f : fs){
|
|
|
+ ExcelField ef = f.getAnnotation(ExcelField.class);
|
|
|
+ if (ef != null && (ef.type()==0 || ef.type()==2)){
|
|
|
+ if (groups!=null && groups.length>0){
|
|
|
+ boolean inGroup = false;
|
|
|
+ for (int g : groups){
|
|
|
+ if (inGroup){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ for (int efg : ef.groups()){
|
|
|
+ if (g == efg){
|
|
|
+ inGroup = true;
|
|
|
+ annotationList.add(new Object[]{ef, f});
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ annotationList.add(new Object[]{ef, f});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Get annotation method
|
|
|
+ Method[] ms = cls.getDeclaredMethods();
|
|
|
+ for (Method m : ms){
|
|
|
+ ExcelField ef = m.getAnnotation(ExcelField.class);
|
|
|
+ if (ef != null && (ef.type()==0 || ef.type()==2)){
|
|
|
+ if (groups!=null && groups.length>0){
|
|
|
+ boolean inGroup = false;
|
|
|
+ for (int g : groups){
|
|
|
+ if (inGroup){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ for (int efg : ef.groups()){
|
|
|
+ if (g == efg){
|
|
|
+ inGroup = true;
|
|
|
+ annotationList.add(new Object[]{ef, m});
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ annotationList.add(new Object[]{ef, m});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Field sorting
|
|
|
+ Collections.sort(annotationList, new Comparator<Object[]>() {
|
|
|
+ public int compare(Object[] o1, Object[] o2) {
|
|
|
+ return new Integer(((ExcelField)o1[0]).sort()).compareTo(
|
|
|
+ new Integer(((ExcelField)o2[0]).sort()));
|
|
|
+ };
|
|
|
+ });
|
|
|
+ //log.debug("Import column count:"+annotationList.size());
|
|
|
+ // Get excel data
|
|
|
+ List<E> dataList = Lists.newArrayList();
|
|
|
+ for (int i = this.getDataRowNum(); i < this.getLastDataRowNum(); i++) {
|
|
|
+ E e = (E)cls.newInstance();
|
|
|
+ int column = 0;
|
|
|
+ Row row = this.getRow(i);
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (Object[] os : annotationList){
|
|
|
+ ExcelField ef = (ExcelField)os[0];
|
|
|
+ Object val = null;
|
|
|
+ if (ef.colNum()!=0) {
|
|
|
+ val = this.getCellValue(row, ef.colNum());
|
|
|
+ }else {
|
|
|
+ val = this.getCellValue(row, column++);
|
|
|
+ }
|
|
|
+ if (val != null){
|
|
|
+
|
|
|
+ // If is dict type, get dict value
|
|
|
+ if (StringUtils.isNotBlank(ef.dictType())){
|
|
|
+ val = DictUtils.getDictValue(val.toString(), ef.dictType(), "");
|
|
|
+ //log.debug("Dictionary type value: ["+i+","+colunm+"] " + val);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(ef.mainDictType())){
|
|
|
+ val = DictUtils.getMainDictValue(val.toString(), ef.mainDictType(), "");
|
|
|
+ //log.debug("Dictionary type value: ["+i+","+colunm+"] " + val);
|
|
|
+ }
|
|
|
+ // Get param type and type cast
|
|
|
+ Class<?> valType = Class.class;
|
|
|
+ if (os[1] instanceof Field){
|
|
|
+ valType = ((Field)os[1]).getType();
|
|
|
+ }else if (os[1] instanceof Method){
|
|
|
+ Method method = ((Method)os[1]);
|
|
|
+ if ("get".equals(method.getName().substring(0, 3))){
|
|
|
+ valType = method.getReturnType();
|
|
|
+ }else if("set".equals(method.getName().substring(0, 3))){
|
|
|
+ valType = ((Method)os[1]).getParameterTypes()[0];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //log.debug("Import value type: ["+i+","+column+"] " + valType);
|
|
|
+ try {
|
|
|
+ //如果导入的java对象,需要在这里自己进行变换。
|
|
|
+ if (valType == String.class){
|
|
|
+ String s = String.valueOf(val.toString());
|
|
|
+ if(StringUtils.endsWith(s, ".0")){
|
|
|
+ val = StringUtils.substringBefore(s, ".0");
|
|
|
+ }else{
|
|
|
+ val = String.valueOf(val.toString());
|
|
|
+ }
|
|
|
+ }else if (valType == Integer.class){
|
|
|
+ val = Double.valueOf(val.toString()).intValue();
|
|
|
+ }else if (valType == Long.class){
|
|
|
+ val = Double.valueOf(val.toString()).longValue();
|
|
|
+ }else if (valType == Double.class){
|
|
|
+ if(val instanceof String){
|
|
|
+ val = ((String) val).replaceAll(",","");
|
|
|
+ }
|
|
|
+ val = Double.valueOf(val.toString());
|
|
|
+ }else if (valType == Float.class){
|
|
|
+ val = Float.valueOf(val.toString());
|
|
|
+ }else if (valType == Date.class){
|
|
|
+ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ val=sdf.parse(val.toString());
|
|
|
+ }else if (valType == User.class){
|
|
|
+ val = UserUtils.getByUserName(val.toString());
|
|
|
+ }else if (valType == Office.class){
|
|
|
+ val = UserUtils.getByOfficeName(val.toString());
|
|
|
+ }else if (valType == Area.class){
|
|
|
+ val = UserUtils.getByAreaName(val.toString());
|
|
|
+ }else if (valType == WorkJobGrade.class){
|
|
|
+ val = UserUtils.getByWorkJobGradeName(val.toString());
|
|
|
+ }else{
|
|
|
+ if (ef.fieldType() != Class.class){
|
|
|
+ val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString());
|
|
|
+ }else{
|
|
|
+ val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
|
|
|
+ "fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.info("Get cell value ["+i+","+column+"] error: " + ex.toString());
|
|
|
+ val = null;
|
|
|
+ }
|
|
|
+ // set entity value
|
|
|
+ if (os[1] instanceof Field){
|
|
|
+ Reflections.invokeSetter(e, ((Field)os[1]).getName(), val);
|
|
|
+ }else if (os[1] instanceof Method){
|
|
|
+ String mthodName = ((Method)os[1]).getName();
|
|
|
+ if ("get".equals(mthodName.substring(0, 3))){
|
|
|
+ mthodName = "set"+StringUtils.substringAfter(mthodName, "get");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Reflections.invokeMethod(e, mthodName, new Class[]{valType}, new Object[]{val});
|
|
|
+ }catch (Exception e3){
|
|
|
+ log.error("解析Excel失败。",e3);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.append(val+", ");
|
|
|
+ }
|
|
|
+ dataList.add(e);
|
|
|
+ log.debug("Read success: ["+i+"] "+sb.toString());
|
|
|
+ }
|
|
|
+ return dataList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 客户的报表数据
|
|
|
+ * @return
|
|
|
+ * @throws InstantiationException
|
|
|
+ * @throws IllegalAccessException
|
|
|
+ *//*
|
|
|
+ public <E> Map<Object,Object> getClientDataList(Class<E> cls, int... groups) throws InstantiationException, IllegalAccessException{
|
|
|
+ HashMap<Object, Object> map = Maps.newHashMap();
|
|
|
+ List<Object[]> annotationList = Lists.newArrayList();
|
|
|
+ // Get annotation field
|
|
|
+ Field[] fs = cls.getDeclaredFields();
|
|
|
+ for (Field f : fs){
|
|
|
+ ExcelField ef = f.getAnnotation(ExcelField.class);
|
|
|
+ if (ef != null && (ef.type()==0 || ef.type()==2)){
|
|
|
+ if (groups!=null && groups.length>0){
|
|
|
+ boolean inGroup = false;
|
|
|
+ for (int g : groups){
|
|
|
+ if (inGroup){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ for (int efg : ef.groups()){
|
|
|
+ if (g == efg){
|
|
|
+ inGroup = true;
|
|
|
+ annotationList.add(new Object[]{ef, f});
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ annotationList.add(new Object[]{ef, f});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Get annotation method
|
|
|
+ Method[] ms = cls.getDeclaredMethods();
|
|
|
+ for (Method m : ms){
|
|
|
+ ExcelField ef = m.getAnnotation(ExcelField.class);
|
|
|
+ if (ef != null && (ef.type()==0 || ef.type()==2)){
|
|
|
+ if (groups!=null && groups.length>0){
|
|
|
+ boolean inGroup = false;
|
|
|
+ for (int g : groups){
|
|
|
+ if (inGroup){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ for (int efg : ef.groups()){
|
|
|
+ if (g == efg){
|
|
|
+ inGroup = true;
|
|
|
+ annotationList.add(new Object[]{ef, m});
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ annotationList.add(new Object[]{ef, m});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Field sorting
|
|
|
+ Collections.sort(annotationList, new Comparator<Object[]>() {
|
|
|
+ public int compare(Object[] o1, Object[] o2) {
|
|
|
+ return new Integer(((ExcelField)o1[0]).sort()).compareTo(
|
|
|
+ new Integer(((ExcelField)o2[0]).sort()));
|
|
|
+ };
|
|
|
+ });
|
|
|
+ //log.debug("Import column count:"+annotationList.size());
|
|
|
+ // Get excel data
|
|
|
+ List<E> dataList = Lists.newArrayList();
|
|
|
+ List<String> errList = Lists.newArrayList();
|
|
|
+ for (int i = this.getDataRowNum(); i < this.getLastDataRowNum(); i++) {
|
|
|
+ E e = (E)cls.newInstance();
|
|
|
+ int column = 0;
|
|
|
+ Row row = this.getRow(i);
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ String errMsg = "";
|
|
|
+ for (Object[] os : annotationList){
|
|
|
+ Object val = this.getCellValue(row, column++);
|
|
|
+ if (val != null){
|
|
|
+ ExcelField ef = (ExcelField)os[0];
|
|
|
+ // If is dict type, get dict value
|
|
|
+ if (StringUtils.isNotBlank(ef.dictType())){
|
|
|
+ val = DictUtils.getDictValue(val.toString(), ef.dictType(), "");
|
|
|
+ //log.debug("Dictionary type value: ["+i+","+colunm+"] " + val);
|
|
|
+ }
|
|
|
+ // Get param type and type cast
|
|
|
+ Class<?> valType = Class.class;
|
|
|
+ if (os[1] instanceof Field){
|
|
|
+ valType = ((Field)os[1]).getType();
|
|
|
+ }else if (os[1] instanceof Method){
|
|
|
+ Method method = ((Method)os[1]);
|
|
|
+ if ("get".equals(method.getName().substring(0, 3))){
|
|
|
+ valType = method.getReturnType();
|
|
|
+ }else if("set".equals(method.getName().substring(0, 3))){
|
|
|
+ valType = ((Method)os[1]).getParameterTypes()[0];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //log.debug("Import value type: ["+i+","+column+"] " + valType);
|
|
|
+ try {
|
|
|
+ //如果导入的java对象,需要在这里自己进行变换。
|
|
|
+ if (valType == String.class){
|
|
|
+ String s = String.valueOf(val.toString());
|
|
|
+ if(StringUtils.endsWith(s, ".0")){
|
|
|
+ val = StringUtils.substringBefore(s, ".0");
|
|
|
+ }else{
|
|
|
+ val = String.valueOf(val.toString());
|
|
|
+ }
|
|
|
+ }else if (valType == Integer.class){
|
|
|
+ val = Double.valueOf(val.toString()).intValue();
|
|
|
+ }else if (valType == Long.class){
|
|
|
+ val = Double.valueOf(val.toString()).longValue();
|
|
|
+ }else if (valType == Double.class){
|
|
|
+ val = Double.valueOf(val.toString());
|
|
|
+ }else if (valType == Float.class){
|
|
|
+ val = Float.valueOf(val.toString());
|
|
|
+ }else if (valType == Date.class){
|
|
|
+ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ val=sdf.parse(val.toString());
|
|
|
+ }else if (valType == User.class){
|
|
|
+ val = UserUtils.getByUserName(val.toString());
|
|
|
+ }else if (valType == Office.class){
|
|
|
+ val = UserUtils.getByOfficeName(val.toString());
|
|
|
+ }else if (valType == Area.class){
|
|
|
+ val = UserUtils.getByAreaName(val.toString());
|
|
|
+ }else{
|
|
|
+ if (ef.fieldType() != Class.class){
|
|
|
+ val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString());
|
|
|
+ }else{
|
|
|
+ val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
|
|
|
+ "fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.info("Get cell value ["+i+","+column+"] error: " + ex.toString());
|
|
|
+ val = null;
|
|
|
+ }
|
|
|
+ if(column == 1 || column == 2 || column == 3
|
|
|
+ || column == 4 || column == 5 || column == 6 || column == 7
|
|
|
+ || column == 8 || column == 9 || column == 10){
|
|
|
+ if(val == null || val == ""){
|
|
|
+ errMsg += "第+i+条,第"+column+"列数据不能为空";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // set entity value
|
|
|
+ if (os[1] instanceof Field){
|
|
|
+ if(Strings.isNullOrEmpty(errMsg)){
|
|
|
+ Reflections.invokeSetter(e, ((Field)os[1]).getName(), val);
|
|
|
+ }
|
|
|
+
|
|
|
+ }else if (os[1] instanceof Method){
|
|
|
+ String mthodName = ((Method)os[1]).getName();
|
|
|
+ if ("get".equals(mthodName.substring(0, 3))){
|
|
|
+ mthodName = "set"+StringUtils.substringAfter(mthodName, "get");
|
|
|
+ }
|
|
|
+ if(Strings.isNullOrEmpty(errMsg)){
|
|
|
+ Reflections.invokeMethod(e, mthodName, new Class[] {valType}, new Object[] {val});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.append(val+", ");
|
|
|
+ }
|
|
|
+ if(!"".equals(errMsg)){
|
|
|
+ errList.add(errMsg);
|
|
|
+ }else{
|
|
|
+ dataList.add(e);
|
|
|
+ }
|
|
|
+ log.debug("Read success: ["+i+"] "+sb.toString());
|
|
|
+ }
|
|
|
+ map.put("dataList",dataList);
|
|
|
+ map.put("errList",errList);
|
|
|
+ return map;
|
|
|
+ }*/
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 导入测试
|
|
|
+// */
|
|
|
+// public static void main(String[] args) throws Throwable {
|
|
|
+//
|
|
|
+// ImportExcel ei = new ImportExcel("target/export.xlsx", 1);
|
|
|
+//
|
|
|
+// for (int i = ei.getDataRowNum(); i < ei.getLastDataRowNum(); i++) {
|
|
|
+// Row row = ei.getRow(i);
|
|
|
+// for (int j = 0; j < ei.getLastCellNum(); j++) {
|
|
|
+// Object val = ei.getCellValue(row, j);
|
|
|
+// System.out.print(val+", ");
|
|
|
+// }
|
|
|
+// System.out.print("\n");
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+ public static String replaceBlank(String str) {
|
|
|
+ String dest = "";
|
|
|
+ if (str!=null) {
|
|
|
+ Pattern p = Pattern.compile("\t|\r|\n");
|
|
|
+ Matcher m = p.matcher(str);
|
|
|
+ dest = m.replaceAll("");
|
|
|
+ }
|
|
|
+ return dest;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* public static void main(String[] args) {
|
|
|
+ replaceBlank("桩基框架-剪力墙结构\n" +
|
|
|
+ "(主体部分)空间钢桁架结构(屋顶部分)");
|
|
|
+ }*/
|
|
|
+}
|