
本人开发过程中遇到这样一个需求,导入上文双表头excel。我尝试用过easyPoi,但是由于二级表头名称一样(养老保险----养老保险),故失败。故自己先了一个工具,解决思路:
1.原生POI读出双表头名(代扣个人款项-养老保险、公司承担款项-养老保险)、对应列数(第几列)
2.手动写一个注解,用来标识excel某一列对应实体类某个字段,作用类似于easyPoi中的@excel
/*** 个人承担养老保险*/@ExcelMerge(columnName = "代扣个人款项-养老保险", filed = "endowmentIns")private BigDecimal endowmentIns = BigDecimal.ZERO;
3.对比表头和实体类的注解,利用反射创建实体类并赋值
具体代码如下:
1.注解
package com.accfun.fssc.common.anno;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** Created by yyc on 2018/12/21.*/
@Target({ElementType.FIELD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelMerge {String columnName();String filed();
}
2.实体类
/*** 职员姓名*/@ExcelMerge(columnName = "姓名", filed = "cPsnName")private String cPsnName;/*** 部门编码*/private String cDeptCode;/*** 部门名称*/@ExcelMerge(columnName = "部门", filed = "cDeptName")private String cDeptName;/*** 个税起征点*/@ExcelMerge(columnName = "个税免征额", filed = "taxThreshold")private BigDecimal taxThreshold = BigDecimal.ZERO;/*** 基本工资*/@ExcelMerge(columnName = "工资总额-工资", filed = "basicSalary")private BigDecimal basicSalary = BigDecimal.ZERO;
3.工具类
package com.accfun.fssc.common.utils;import com.accfun.easyui.framework.poi.excel.entity.result.ExcelImportResult;
import com.accfun.fssc.common.anno.ExcelMerge;
import com.accfun.fssc.invoice.model.ExpSalary;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;/*** Created by yyc on 2018/11/28.*/
@Slf4j
public class ExcelUtil {/*** 原生poi获取excel指定行、列的值* @param model* @param sheet* @param row* @param cell* @return*/public static String getTitleValue(ExcelImportResult> model, int sheet, int row, int cell){String value = "";if(model.getWorkbook().getSheetAt(sheet) != null &&model.getWorkbook().getSheetAt(sheet).getRow(row) != null &&model.getWorkbook().getSheetAt(sheet).getRow(row).getCell(cell) != null){value = model.getWorkbook().getSheetAt(sheet).getRow(row).getCell(cell).getStringCellValue();}return value;}public static List
4.读取excel
@RequestMapping(path = "importInXls", method = RequestMethod.POST)public PcApiJson importInXls(@RequestParam("file") MultipartFile file, HttpServletRequest request) {ImportParams params = new ImportParams();params.setTitleRows(1);params.setLastOfInvalidRow(1);try {List list = importDate(file, request);if (list == null || list.size() <= 0) {throw new AccfunMessageException("年月不能为空");}salaryService.importInXls(list);} catch (AccfunMessageException e) {return new PcApiJson(PcApiJson.PcJsonCode.FAIL, e.getMessage());} catch (Exception e) {e.printStackTrace();return new PcApiJson(PcApiJson.PcJsonCode.FAIL, e.getMessage());}return new PcApiJson(PcApiJson.PcJsonCode.SUCCESS, "导入成功");}public List importDate(MultipartFile file, HttpServletRequest request) throws Exception {List list = new ArrayList<>();//不确定excel是03版本还是07版本Workbook wbs = null;try {wbs = new XSSFWorkbook(file.getInputStream());}catch (Exception e){wbs = new HSSFWorkbook(file.getInputStream());}Sheet childSeet = wbs.getSheetAt(0);List list2 = ExcelUtil.readExcel(childSeet, ExpSalary.class,1);String showSalaryEditionName = childSeet.getSheetName();String showSalaryEdition = "";if("简易版工资表".equals(showSalaryEditionName)){showSalaryEdition = "1";}else if("详细版工资表".equals(showSalaryEditionName)){showSalaryEdition = "2";}else{throw new AccfunMessageException("Excel工作簿名称错误");}for (Object model : list2) {ExpSalary salary = (ExpSalary) model;salary.setShowSalaryEdition(showSalaryEdition);list.add(salary);}return list;}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!