| @@ -30,7 +30,7 @@ import cn.afterturn.easypoi.excel.imports.ExcelImportService; | |||
| import cn.afterturn.easypoi.excel.imports.sax.SaxReadExcel; | |||
| import cn.afterturn.easypoi.excel.imports.sax.parse.ISaxRowRead; | |||
| import cn.afterturn.easypoi.exception.excel.ExcelImportException; | |||
| import cn.afterturn.easypoi.handler.inter.IExcelReadRowHanlder; | |||
| import cn.afterturn.easypoi.handler.inter.IExcelReadRowHandler; | |||
| /** | |||
| * Excel 导入工具 | |||
| @@ -149,7 +149,7 @@ public class ExcelImportUtil { | |||
| */ | |||
| @SuppressWarnings("rawtypes") | |||
| public static void importExcelBySax(InputStream inputstream, Class<?> pojoClass, | |||
| ImportParams params, IExcelReadRowHanlder hanlder) { | |||
| ImportParams params, IExcelReadRowHandler hanlder) { | |||
| new SaxReadExcel().readExcel(inputstream, pojoClass, params, null, hanlder); | |||
| } | |||
| @@ -68,7 +68,7 @@ public class ImportParams extends ExcelBaseParams { | |||
| /** | |||
| * 校验处理接口 | |||
| */ | |||
| private IExcelVerifyHandler verifyHanlder; | |||
| private IExcelVerifyHandler verifyHandler; | |||
| /** | |||
| * 保存上传的Excel目录,默认是 如 TestEntity这个类保存路径就是 | |||
| * upload/excelUpload/Test/yyyyMMddHHmss_***** 保存名称上传时间_五位随机数 | |||
| @@ -120,8 +120,8 @@ public class ImportParams extends ExcelBaseParams { | |||
| return titleRows; | |||
| } | |||
| public IExcelVerifyHandler getVerifyHanlder() { | |||
| return verifyHanlder; | |||
| public IExcelVerifyHandler getVerifyHandler() { | |||
| return verifyHandler; | |||
| } | |||
| public boolean isNeedSave() { | |||
| @@ -157,8 +157,8 @@ public class ImportParams extends ExcelBaseParams { | |||
| this.titleRows = titleRows; | |||
| } | |||
| public void setVerifyHanlder(IExcelVerifyHandler verifyHanlder) { | |||
| this.verifyHanlder = verifyHanlder; | |||
| public void setVerifyHandler(IExcelVerifyHandler verifyHandler) { | |||
| this.verifyHandler = verifyHandler; | |||
| } | |||
| public int getLastOfInvalidRow() { | |||
| @@ -1,63 +0,0 @@ | |||
| /** | |||
| * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | |||
| * | |||
| * Licensed under the Apache License, Version 2.0 (the "License"); | |||
| * you may not use this file except in compliance with the License. | |||
| * You may obtain a copy of the License at | |||
| * | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| package cn.afterturn.easypoi.excel.entity.result; | |||
| /** | |||
| * Excel导入处理返回结果 | |||
| * | |||
| * @author JueYue | |||
| * 2014年6月23日 下午11:03:29 | |||
| */ | |||
| public class ExcelVerifyHanlderResult { | |||
| /** | |||
| * 是否正确 | |||
| */ | |||
| private boolean success; | |||
| /** | |||
| * 错误信息 | |||
| */ | |||
| private String msg; | |||
| public ExcelVerifyHanlderResult() { | |||
| } | |||
| public ExcelVerifyHanlderResult(boolean success) { | |||
| this.success = success; | |||
| } | |||
| public ExcelVerifyHanlderResult(boolean success, String msg) { | |||
| this.success = success; | |||
| this.msg = msg; | |||
| } | |||
| public String getMsg() { | |||
| return msg; | |||
| } | |||
| public boolean isSuccess() { | |||
| return success; | |||
| } | |||
| public void setMsg(String msg) { | |||
| this.msg = msg; | |||
| } | |||
| public void setSuccess(boolean success) { | |||
| this.success = success; | |||
| } | |||
| } | |||
| @@ -49,7 +49,7 @@ import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; | |||
| import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; | |||
| import cn.afterturn.easypoi.excel.export.base.BaseExportService; | |||
| import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; | |||
| import cn.afterturn.easypoi.excel.export.template.TemplateSumHanlder.TemplateSumEntity; | |||
| import cn.afterturn.easypoi.excel.export.template.TemplateSumHandler.TemplateSumEntity; | |||
| import cn.afterturn.easypoi.excel.html.helper.MergedRegionHelper; | |||
| import cn.afterturn.easypoi.exception.excel.ExcelExportException; | |||
| import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; | |||
| @@ -83,7 +83,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| */ | |||
| private MergedRegionHelper mergedRegionHelper; | |||
| private TemplateSumHanlder templateSumHanlder; | |||
| private TemplateSumHandler templateSumHandler; | |||
| /** | |||
| * 往Sheet 填充正常数据,根据表头信息 使用导入的部分逻辑,坐对象映射 | |||
| @@ -253,7 +253,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| boolean colForeach) throws Exception { | |||
| deleteCell(sheet, map); | |||
| mergedRegionHelper = new MergedRegionHelper(sheet); | |||
| templateSumHanlder = new TemplateSumHanlder(sheet); | |||
| templateSumHandler = new TemplateSumHandler(sheet); | |||
| if (colForeach) { | |||
| colForeach(sheet, map); | |||
| } | |||
| @@ -277,7 +277,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| } | |||
| private void hanlderSumCell(Sheet sheet) { | |||
| for (TemplateSumEntity sumEntity : templateSumHanlder.getDataList()) { | |||
| for (TemplateSumEntity sumEntity : templateSumHandler.getDataList()) { | |||
| Cell cell = sheet.getRow(sumEntity.getRow()).getCell(sumEntity.getCol()); | |||
| cell.setCellValue(cell.getStringCellValue() | |||
| .replace("sum:(" + sumEntity.getSumKey() + ")", sumEntity.getValue() + "")); | |||
| @@ -468,7 +468,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| createRowNoRow(cell.getRowIndex() + rowspan,cell.getRow().getSheet().getLastRowNum(),(datas.size() - 1) * rowspan); | |||
| cell.getRow().getSheet().shiftRows(cell.getRowIndex() + rowspan, | |||
| cell.getRow().getSheet().getLastRowNum(), (datas.size() - 1) * rowspan, true, true); | |||
| templateSumHanlder.shiftRows(cell.getRowIndex(),(datas.size() - 1) * rowspan); | |||
| templateSumHandler.shiftRows(cell.getRowIndex(),(datas.size() - 1) * rowspan); | |||
| } | |||
| while (its.hasNext()) { | |||
| Object t = its.next(); | |||
| @@ -573,7 +573,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| row.getCell(ci).setCellStyle(params.getCellStyle()); // plq modify at 2017-11-13 | |||
| //判断这个属性是不是需要统计 | |||
| if (params.isNeedSum()) { | |||
| templateSumHanlder.addValueOfKey(params.getName(), val); | |||
| templateSumHandler.addValueOfKey(params.getName(), val); | |||
| } | |||
| //如果合并单元格,就把这个单元格的样式和之前的保持一致 | |||
| setMergedRegionStyle(row, ci, columns.get(i)); | |||
| @@ -719,7 +719,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||
| params.setRowspan(colAndrow[0]); | |||
| params.setColspan(colAndrow[1]); | |||
| } | |||
| params.setNeedSum(templateSumHanlder.isSumKey(params.getName())); | |||
| params.setNeedSum(templateSumHandler.isSumKey(params.getName())); | |||
| return params; | |||
| } | |||
| @@ -1,198 +0,0 @@ | |||
| package cn.afterturn.easypoi.excel.export.template; | |||
| import static cn.afterturn.easypoi.util.PoiElUtil.*; | |||
| import java.util.ArrayList; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.poi.ss.usermodel.Cell; | |||
| import org.apache.poi.ss.usermodel.Row; | |||
| import org.apache.poi.ss.usermodel.Sheet; | |||
| import cn.afterturn.easypoi.util.PoiCellUtil; | |||
| /** | |||
| * 针对模板统计问题做统一处理 | |||
| * 1.处理模板之前统计需要SUM的数据以及位置 | |||
| * 2.遍历时统计数据 | |||
| * 3.遍历后设置数据 | |||
| * @author JueYue | |||
| * 2016年6月19日 | |||
| */ | |||
| public class TemplateSumHanlder { | |||
| private Map<String, TemplateSumEntity> sumMap = new HashMap<String, TemplateSumEntity>(); | |||
| public TemplateSumHanlder(Sheet sheet) { | |||
| getAllSumCell(sheet); | |||
| } | |||
| /** | |||
| * 统计计算所有的统计单元格 | |||
| */ | |||
| private void getAllSumCell(Sheet sheet) { | |||
| Row row = null; | |||
| int index = 0; | |||
| while (index <= sheet.getLastRowNum()) { | |||
| row = sheet.getRow(index++); | |||
| if (row == null) { | |||
| continue; | |||
| } | |||
| for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) { | |||
| if (row.getCell(i) != null && PoiCellUtil.getCellValue(row.getCell(i)).contains(SUM)) { | |||
| addSumCellToList(row.getCell(i)); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| private void addSumCellToList(Cell cell) { | |||
| String cellValue = cell.getStringCellValue(); | |||
| int index = 0; | |||
| while ((index = indexOfIgnoreCase(cellValue, SUM, index)) != -1) { | |||
| TemplateSumEntity entity = new TemplateSumEntity(); | |||
| entity.setCellValue(cellValue); | |||
| entity.setSumKey(getSumKey(cellValue, index++)); | |||
| entity.setCol(cell.getColumnIndex()); | |||
| entity.setRow(cell.getRowIndex()); | |||
| sumMap.put(entity.getSumKey(), entity); | |||
| } | |||
| } | |||
| public boolean isSumKey(String key) { | |||
| return sumMap.containsKey(key); | |||
| } | |||
| /** | |||
| * SUM:(key) | |||
| * | |||
| * @param cellValue | |||
| * @param index | |||
| * @return | |||
| */ | |||
| private String getSumKey(String cellValue, int index) { | |||
| return cellValue.substring(index + 5, cellValue.indexOf(")", index)); | |||
| } | |||
| public void addValueOfKey(String key, String val) { | |||
| if (StringUtils.isNoneEmpty(key)) { | |||
| sumMap.get(key).setValue(sumMap.get(key).getValue() + Double.valueOf(val)); | |||
| } | |||
| } | |||
| public List<TemplateSumEntity> getDataList() { | |||
| return new ArrayList<TemplateSumEntity>(sumMap.values()); | |||
| } | |||
| public void addListSizeToSumEntity() { | |||
| } | |||
| /** | |||
| * | |||
| * @param rowIndex | |||
| * @param size | |||
| */ | |||
| public void shiftRows(int rowIndex, int size) { | |||
| for (TemplateSumEntity entity : getDataList()) { | |||
| if (entity.getRow() > rowIndex) { | |||
| entity.setRow(entity.getRow() + size); | |||
| } | |||
| } | |||
| } | |||
| private static int indexOfIgnoreCase(String str, String searchStr, int startPos) { | |||
| if (str == null || searchStr == null) { | |||
| return -1; | |||
| } | |||
| if (startPos < 0) { | |||
| startPos = 0; | |||
| } | |||
| int endLimit = (str.length() - searchStr.length()) + 1; | |||
| if (startPos > endLimit) { | |||
| return -1; | |||
| } | |||
| if (searchStr.length() == 0) { | |||
| return startPos; | |||
| } | |||
| for (int i = startPos; i < endLimit; i++) { | |||
| if (str.regionMatches(true, i, searchStr, 0, searchStr.length())) { | |||
| return i; | |||
| } | |||
| } | |||
| return -1; | |||
| } | |||
| /** | |||
| * 统计对象 | |||
| * @author JueYue | |||
| */ | |||
| protected class TemplateSumEntity { | |||
| /** | |||
| * CELL的值 | |||
| */ | |||
| private String cellValue; | |||
| /** | |||
| * 需要计算的KEY | |||
| */ | |||
| private String sumKey; | |||
| /** | |||
| * 列 | |||
| */ | |||
| private int col; | |||
| /** | |||
| * 行 | |||
| */ | |||
| private int row; | |||
| /** | |||
| * 最后值 | |||
| */ | |||
| private double value; | |||
| public String getCellValue() { | |||
| return cellValue; | |||
| } | |||
| public void setCellValue(String cellValue) { | |||
| this.cellValue = cellValue; | |||
| } | |||
| public String getSumKey() { | |||
| return sumKey; | |||
| } | |||
| public void setSumKey(String sumKey) { | |||
| this.sumKey = sumKey; | |||
| } | |||
| public int getCol() { | |||
| return col; | |||
| } | |||
| public void setCol(int col) { | |||
| this.col = col; | |||
| } | |||
| public int getRow() { | |||
| return row; | |||
| } | |||
| public void setRow(int row) { | |||
| this.row = row; | |||
| } | |||
| public double getValue() { | |||
| return value; | |||
| } | |||
| public void setValue(double value) { | |||
| this.value = value; | |||
| } | |||
| } | |||
| } | |||
| @@ -212,14 +212,14 @@ public class CellValueService { | |||
| /** | |||
| * 获取cell值 | |||
| * @param dataHanlder | |||
| * @param dataHandler | |||
| * @param object | |||
| * @param cellEntity | |||
| * @param excelParams | |||
| * @param titleString | |||
| * @return | |||
| */ | |||
| public Object getValue(IExcelDataHandler<?> dataHanlder, Object object, | |||
| public Object getValue(IExcelDataHandler<?> dataHandler, Object object, | |||
| SaxReadCellEntity cellEntity, Map<String, ExcelImportEntity> excelParams, | |||
| String titleString) { | |||
| ExcelImportEntity entity = excelParams.get(titleString); | |||
| @@ -230,7 +230,7 @@ public class CellValueService { | |||
| Object result = cellEntity.getValue(); | |||
| result = hanlderSuffix(entity.getSuffix(), result); | |||
| result = replaceValue(entity.getReplace(), result); | |||
| result = handlerValue(dataHanlder, object, result, titleString); | |||
| result = handlerValue(dataHandler, object, result, titleString); | |||
| return getValueByType(xclass, result, entity); | |||
| } | |||
| @@ -50,7 +50,7 @@ import cn.afterturn.easypoi.excel.entity.ImportParams; | |||
| import cn.afterturn.easypoi.excel.entity.params.ExcelCollectionParams; | |||
| import cn.afterturn.easypoi.excel.entity.params.ExcelImportEntity; | |||
| import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; | |||
| import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHanlderResult; | |||
| import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult; | |||
| import cn.afterturn.easypoi.excel.imports.base.ImportBaseService; | |||
| import cn.afterturn.easypoi.exception.excel.ExcelImportException; | |||
| import cn.afterturn.easypoi.exception.excel.enums.ExcelImportEnum; | |||
| @@ -267,8 +267,8 @@ public class ExcelImportService extends ImportBaseService { | |||
| verfiyFail = true; | |||
| } | |||
| } | |||
| if (params.getVerifyHanlder() != null) { | |||
| ExcelVerifyHanlderResult result = params.getVerifyHanlder().verifyHandler(object); | |||
| if (params.getVerifyHandler() != null) { | |||
| ExcelVerifyHandlerResult result = params.getVerifyHandler().verifyHandler(object); | |||
| if (!result.isSuccess()) { | |||
| if (cell == null) { | |||
| cell = row.createCell(row.getLastCellNum()); | |||
| @@ -34,7 +34,7 @@ import cn.afterturn.easypoi.excel.entity.ImportParams; | |||
| import cn.afterturn.easypoi.excel.imports.sax.parse.ISaxRowRead; | |||
| import cn.afterturn.easypoi.excel.imports.sax.parse.SaxRowRead; | |||
| import cn.afterturn.easypoi.exception.excel.ExcelImportException; | |||
| import cn.afterturn.easypoi.handler.inter.IExcelReadRowHanlder; | |||
| import cn.afterturn.easypoi.handler.inter.IExcelReadRowHandler; | |||
| /** | |||
| * 基于SAX Excel大数据读取,读取Excel 07版本,不支持图片读取 | |||
| @@ -48,7 +48,7 @@ public class SaxReadExcel { | |||
| private static final Logger LOGGER = LoggerFactory.getLogger(SaxReadExcel.class); | |||
| public <T> List<T> readExcel(InputStream inputstream, Class<?> pojoClass, ImportParams params, | |||
| ISaxRowRead rowRead, IExcelReadRowHanlder hanlder) { | |||
| ISaxRowRead rowRead, IExcelReadRowHandler hanlder) { | |||
| try { | |||
| OPCPackage opcPackage = OPCPackage.open(inputstream); | |||
| return readExcel(opcPackage, pojoClass, params, rowRead, hanlder); | |||
| @@ -59,7 +59,7 @@ public class SaxReadExcel { | |||
| } | |||
| private <T> List<T> readExcel(OPCPackage opcPackage, Class<?> pojoClass, ImportParams params, | |||
| ISaxRowRead rowRead, IExcelReadRowHanlder hanlder) { | |||
| ISaxRowRead rowRead, IExcelReadRowHandler hanlder) { | |||
| try { | |||
| XSSFReader xssfReader = new XSSFReader(opcPackage); | |||
| SharedStringsTable sst = xssfReader.getSharedStringsTable(); | |||
| @@ -36,7 +36,7 @@ import cn.afterturn.easypoi.excel.entity.sax.SaxReadCellEntity; | |||
| import cn.afterturn.easypoi.excel.imports.CellValueService; | |||
| import cn.afterturn.easypoi.excel.imports.base.ImportBaseService; | |||
| import cn.afterturn.easypoi.exception.excel.ExcelImportException; | |||
| import cn.afterturn.easypoi.handler.inter.IExcelReadRowHanlder; | |||
| import cn.afterturn.easypoi.handler.inter.IExcelReadRowHandler; | |||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | |||
| import cn.afterturn.easypoi.util.PoiReflectorUtil; | |||
| @@ -69,9 +69,9 @@ public class SaxRowRead extends ImportBaseService implements ISaxRowRead { | |||
| private CellValueService cellValueServer; | |||
| private IExcelReadRowHanlder hanlder; | |||
| private IExcelReadRowHandler hanlder; | |||
| public SaxRowRead(Class<?> pojoClass, ImportParams params, IExcelReadRowHanlder hanlder) { | |||
| public SaxRowRead(Class<?> pojoClass, ImportParams params, IExcelReadRowHandler hanlder) { | |||
| list = Lists.newArrayList(); | |||
| this.params = params; | |||
| this.pojoClass = pojoClass; | |||
| @@ -1,31 +0,0 @@ | |||
| /** | |||
| * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | |||
| * | |||
| * Licensed under the Apache License, Version 2.0 (the "License"); | |||
| * you may not use this file except in compliance with the License. | |||
| * You may obtain a copy of the License at | |||
| * | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| package cn.afterturn.easypoi.handler.inter; | |||
| /** | |||
| * 接口自定义处理类 | |||
| * @author JueYue | |||
| * 2015年1月16日 下午8:06:26 | |||
| * @param <T> | |||
| */ | |||
| public interface IExcelReadRowHanlder<T> { | |||
| /** | |||
| * 处理解析对象 | |||
| * @param t | |||
| */ | |||
| public void hanlder(T t); | |||
| } | |||
| @@ -15,7 +15,7 @@ | |||
| */ | |||
| package cn.afterturn.easypoi.handler.inter; | |||
| import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHanlderResult; | |||
| import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult; | |||
| /** | |||
| * 导入校验接口 | |||
| @@ -32,6 +32,6 @@ public interface IExcelVerifyHandler<T> { | |||
| * 当前对象 | |||
| * @return | |||
| */ | |||
| public ExcelVerifyHanlderResult verifyHandler(T obj); | |||
| public ExcelVerifyHandlerResult verifyHandler(T obj); | |||
| } | |||
| @@ -52,18 +52,18 @@ public class ExcelListEntity extends ExcelBaseParams { | |||
| this.clazz = clazz; | |||
| } | |||
| public ExcelListEntity(List<?> list, Class<?> clazz, IExcelDataHandler dataHanlder) { | |||
| public ExcelListEntity(List<?> list, Class<?> clazz, IExcelDataHandler dataHandler) { | |||
| this.list = list; | |||
| this.clazz = clazz; | |||
| setDataHanlder(dataHanlder); | |||
| setDataHandler(dataHandler); | |||
| } | |||
| public ExcelListEntity(List<?> list, Class<?> clazz, IExcelDataHandler dataHanlder, | |||
| public ExcelListEntity(List<?> list, Class<?> clazz, IExcelDataHandler dataHandler, | |||
| int headRows) { | |||
| this.list = list; | |||
| this.clazz = clazz; | |||
| this.headRows = headRows; | |||
| setDataHanlder(dataHanlder); | |||
| setDataHandler(dataHandler); | |||
| } | |||
| public ExcelListEntity(List<?> list, Class<?> clazz, int headRows) { | |||
| @@ -6,7 +6,7 @@ | |||
| 导入的逻辑就变成了 | |||
| ```java | |||
| ImportParams params = new ImportParams(); | |||
| params.setDataHanlder(new MapImportHanlder()); | |||
| params.setDataHandler(new MapImportHandler()); | |||
| long start = new Date().getTime(); | |||
| List<Map<String, Object>> list = ExcelImportUtil.importExcel( | |||
| new File(PoiPublicUtil.getWebRootPath("import/check.xls")), Map.class, params); | |||