|
|
@@ -0,0 +1,517 @@
|
|
|
+package com.usoftchina.smartschool.school.common.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.usoftchina.smartschool.context.BaseContextHolder;
|
|
|
+import com.usoftchina.smartschool.exception.BizException;
|
|
|
+import com.usoftchina.smartschool.school.common.service.ExcelService;
|
|
|
+import com.usoftchina.smartschool.school.exception.BizExceptionCode;
|
|
|
+import com.usoftchina.smartschool.school.mapper.DataImportMapper;
|
|
|
+import com.usoftchina.smartschool.school.mapper.DataTempletMapper;
|
|
|
+import com.usoftchina.smartschool.school.po.DataImport;
|
|
|
+import com.usoftchina.smartschool.school.po.DataImportDetail;
|
|
|
+import com.usoftchina.smartschool.school.po.DataTemplet;
|
|
|
+import com.usoftchina.smartschool.school.po.TempletSet;
|
|
|
+import com.usoftchina.smartschool.utils.CollectionUtils;
|
|
|
+import com.usoftchina.smartschool.utils.DateUtils;
|
|
|
+import com.usoftchina.smartschool.utils.RegexpUtils;
|
|
|
+import org.apache.poi.hssf.util.HSSFColor;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: guq
|
|
|
+ * @create: 2018-11-26 15:19
|
|
|
+ **/
|
|
|
+@Service
|
|
|
+public class ExcelServiceImpl implements ExcelService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DataTempletMapper dataTempletMapper;
|
|
|
+ @Autowired
|
|
|
+ private DataImportMapper dataImportMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> CreateTemplet(String caller) {
|
|
|
+ Long companyId = BaseContextHolder.getCompanyId();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ SXSSFWorkbook workbook = new SXSSFWorkbook();
|
|
|
+ DataTemplet dataTemplet = dataTempletMapper.selectByCaller(caller, companyId);
|
|
|
+ if (null == dataTemplet) {
|
|
|
+ throw new BizException(9876, "没有查询到对应的excel配置");
|
|
|
+ }
|
|
|
+ //列
|
|
|
+ String cols = dataTemplet.getDt_columns();
|
|
|
+ JSONArray array = (JSONArray) JSONArray.parse(cols);
|
|
|
+ //模板数据
|
|
|
+ String remark = dataTemplet.getDt_description();
|
|
|
+ String exampledata = dataTemplet.getDt_exampledata();
|
|
|
+ JSONArray datas = (JSONArray) JSONArray.parse(exampledata);
|
|
|
+ createWorkbook(workbook, 1, array, datas, remark);
|
|
|
+ map.put("workbook", workbook);
|
|
|
+ map.put("title", dataTemplet.getDt_title());
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void clearBefore(String caller) {
|
|
|
+ Long schoolId = BaseContextHolder.getSchoolId();
|
|
|
+ String ids = dataImportMapper.selectByCaller(caller, schoolId);
|
|
|
+ if (!StringUtils.isEmpty(ids)) {
|
|
|
+ dataImportMapper.deleteDetailByIds(ids);
|
|
|
+ dataImportMapper.deleteByIds(ids);
|
|
|
+ };
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public Integer parseTemplet(Workbook wb, String caller, String fileName) {
|
|
|
+ if (wb == null || StringUtils.isEmpty(caller)) {
|
|
|
+ throw new BizException(BizExceptionCode.EMPTY_DATA);
|
|
|
+ }
|
|
|
+ //Long companyId = BaseContextHolder.getCompanyId();
|
|
|
+ Long school_id = BaseContextHolder.getSchoolId();
|
|
|
+ DataImport di = new DataImport();
|
|
|
+ di.setSchool_id(school_id);
|
|
|
+ di.setDi_caller(caller);
|
|
|
+ di.setDi_date(new Date());
|
|
|
+ di.setDi_man(BaseContextHolder.getUserName());
|
|
|
+ di.setDi_toformal(0);
|
|
|
+ di.setDi_success(0);
|
|
|
+ List<Map<String,String>> datas = new ArrayList<Map<String,String>>();
|
|
|
+ DataTemplet dataTemplet = dataTempletMapper.selectByCaller(caller, school_id);
|
|
|
+ String cols = dataTemplet.getDt_columns();
|
|
|
+ if (StringUtils.isEmpty(cols)) {
|
|
|
+ throw new BizException(12135, "没有查询到对应的excel配置");
|
|
|
+ }
|
|
|
+ //验证标题是否正确
|
|
|
+ String title = dataTemplet.getDt_title();
|
|
|
+ if (StringUtils.isEmpty(fileName) || fileName.indexOf(title) < 0) {
|
|
|
+ throw new BizException(BizExceptionCode.BIZ_IMPORT_ERROREXCEL);
|
|
|
+ }
|
|
|
+ //前端列表信息
|
|
|
+ List<TempletSet> templetSets = JSONArray.parseArray(cols, TempletSet.class);
|
|
|
+ Map<String, List<TempletSet>> columns = CollectionUtils.groupBy(templetSets, TempletSet::getPosition);
|
|
|
+ List<String> positions = SetToList(columns.keySet());
|
|
|
+ String cellData = null;
|
|
|
+ String codeValue = null;
|
|
|
+ //读取默认从第2行开始读取
|
|
|
+ List<String> keys = new ArrayList<>();
|
|
|
+ //获取第一个sheet
|
|
|
+ Sheet sheet = wb.getSheetAt(0);
|
|
|
+ //获取最大行数
|
|
|
+ int rownum = sheet.getPhysicalNumberOfRows();
|
|
|
+ if (rownum < 3) {
|
|
|
+ throw new BizException(12324, "请先填写数据");
|
|
|
+ }
|
|
|
+ //获取第2行
|
|
|
+ Row row = sheet.getRow(1);
|
|
|
+ //获取最大列数
|
|
|
+ int colnum = row.getPhysicalNumberOfCells();
|
|
|
+ String key = null;
|
|
|
+ Map<String,String> map = null;
|
|
|
+ //添加keys
|
|
|
+ for (int j = 0; j < colnum; j++){
|
|
|
+ key = (String) getCellFormatValue(row.getCell(j));
|
|
|
+ keys.add(key);
|
|
|
+ }
|
|
|
+ //存储数据
|
|
|
+ for (int i = 2; i < rownum; i++) {
|
|
|
+ map = new LinkedHashMap<String,String>();
|
|
|
+ row = sheet.getRow(i);
|
|
|
+ if(row !=null){
|
|
|
+ for (int j = 0;j < colnum; j++){
|
|
|
+ cellData = (String) getCellFormatValue(row.getCell(j));
|
|
|
+ map.put(keys.get(j), cellData);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ datas.add(map);
|
|
|
+ }
|
|
|
+ dataImportMapper.insertSelective(di);
|
|
|
+ Integer keyvalue = di.getDi_id();
|
|
|
+ List<String> validateCode = Lists.newArrayList();
|
|
|
+ //数据转换成字段
|
|
|
+ if (!CollectionUtils.isEmpty(datas)) {
|
|
|
+ StringBuilder err = new StringBuilder();
|
|
|
+ JSONArray mains = new JSONArray();
|
|
|
+ JSONArray details = new JSONArray();
|
|
|
+ //数据行循环
|
|
|
+ for (int i = 0; i < datas.size(); i++) {
|
|
|
+ Map<String,String> data = datas.get(i);
|
|
|
+ DataImportDetail dd = new DataImportDetail();
|
|
|
+ //界面值
|
|
|
+ String value = null;
|
|
|
+ JSONObject mainData = null;
|
|
|
+ JSONObject detailData = null;
|
|
|
+ //标识是否一个主表的数据
|
|
|
+ boolean difference = true;
|
|
|
+ dd.setSchool_id(school_id);
|
|
|
+ dd.setDd_diid(keyvalue);
|
|
|
+ //主从表循环
|
|
|
+ for (int j = 0; j < positions.size(); j++) {
|
|
|
+ String position = positions.get(j);
|
|
|
+ //依据主从表分类
|
|
|
+ if ("main".equals(position)) {
|
|
|
+ //主表字段配置
|
|
|
+ List<TempletSet> main = columns.get(position);
|
|
|
+ for (TempletSet set : main) {
|
|
|
+ //取excel值
|
|
|
+ value = data.get(set.getDescription());
|
|
|
+ //取编号字段
|
|
|
+ if (set.isCodefield() && !"".equals(value)) {
|
|
|
+ mainData = new JSONObject();
|
|
|
+ codeValue = RegexpUtils.replaceSpecialCharacter(value);
|
|
|
+ difference = true;
|
|
|
+ validateCode.add(codeValue);
|
|
|
+ dd.setDd_codevalue(codeValue);
|
|
|
+ }
|
|
|
+ //检查是否是同一单
|
|
|
+ if (set.isCodefield() && "".equals(value)) {
|
|
|
+ difference = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ //检查主表必填字段是否完整
|
|
|
+ if (difference) {
|
|
|
+ if ("true".equals(set.getNecessary()) && "".equals(value)) {
|
|
|
+ err.append("第" + (i + 3) + "行 " + set.getDescription() + " 必填字段未填写!<br/> ");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //检测日期类型是否规范
|
|
|
+ if ("date".equals(set.getType()) && !StringUtils.isEmpty(value)) {
|
|
|
+ value = praseDate(value);
|
|
|
+ if (null == value) {
|
|
|
+ err.append("第" + (i + 3) + "行 " + set.getDescription() + " 日期格式不正确!<br/> ");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //如果为数字类型且为空默认赋值0,检测是否为数字类型
|
|
|
+ if ("number".equals(set.getType())) {
|
|
|
+ if (StringUtils.isEmpty(value)) {
|
|
|
+ value = "0";
|
|
|
+ } else {
|
|
|
+ Boolean numner = isNumner(value);
|
|
|
+ if (!numner) {
|
|
|
+ err.append("第" + (i + 3) + "行 " + set.getDescription() + " 数字格式不正确!<br/> ");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //插入主表数据
|
|
|
+ if (null != mainData) {
|
|
|
+ mainData.put(set.getField(), value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //所有主表数据进行分类
|
|
|
+ if (null != mainData) {
|
|
|
+ //mains.add(mainData);
|
|
|
+ dd.setDd_maindata(mainData.toJSONString());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //从表字段
|
|
|
+ List<TempletSet> detail = columns.get(position);
|
|
|
+ detailData = new JSONObject();
|
|
|
+ for (TempletSet set : detail) {
|
|
|
+ //取excel值
|
|
|
+ value = data.get(set.getDescription());
|
|
|
+ if ("true".equals(set.getNecessary()) && StringUtils.isEmpty(value)) {
|
|
|
+ detailData = null;
|
|
|
+ err.append("第" + (i + 3) + "行 " + set.getDescription() + " 必填字段未填写!<br/> ");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ //检测日期类型是否规范
|
|
|
+ if ("date".equals(set.getType()) && !StringUtils.isEmpty(value)) {
|
|
|
+ value = praseDate(value);
|
|
|
+ if (null == value) {
|
|
|
+ err.append("第" + (i + 3) + "行 " + set.getDescription() + " 日期格式不正确!<br/> ");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //如果为数字类型且为空默认赋值0,检测是否为数字类型
|
|
|
+ if ("number".equals(set.getType())) {
|
|
|
+ if (StringUtils.isEmpty(value)) {
|
|
|
+ value = "0";
|
|
|
+ } else {
|
|
|
+ Boolean numner = isNumner(value);
|
|
|
+ if (!numner) {
|
|
|
+ err.append("第" + (i + 3) + "行 " + set.getDescription() + " 数字格式不正确!<br/> ");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //拼从表数据
|
|
|
+ if (StringUtils.hasText(value) && !"0".equals(value)) {
|
|
|
+ detailData.put(set.getField(), value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (null != detailData && detailData.size() > 0) {
|
|
|
+ dd.setDd_codevalue(codeValue);
|
|
|
+ dd.setDd_detaildata(detailData.toJSONString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //排除编号未填写但是其他字段填写的情况 主表不为空且从表也不为空
|
|
|
+ if (StringUtils.hasText(codeValue) && StringUtils.hasText(dd.getDd_codevalue())) {
|
|
|
+ dataImportMapper.insertDetailSelective(dd);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //重复编号检测
|
|
|
+ List<String> samecode = getDuplicateElements(validateCode);
|
|
|
+ if (null != samecode && samecode.size() > 0) {
|
|
|
+ err.append("下列编号重复: " + printList(samecode) + " 请确认无误后再上传!");
|
|
|
+ }
|
|
|
+ //必填项检查
|
|
|
+ if (err.length() > 0) {
|
|
|
+ dataImportMapper.updateErr(err.toString(), keyvalue);
|
|
|
+ throw new BizException(123456789, err.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return keyvalue;
|
|
|
+ }
|
|
|
+
|
|
|
+ private <E> List<E> getDuplicateElements(List<E> list) {
|
|
|
+ return list.stream() // list 对应的 Stream
|
|
|
+ .collect(Collectors.toMap(e -> e, e -> 1, (a, b) -> a + b)) // 获得元素出现频率的 Map,键为元素,值为元素出现的次数
|
|
|
+ .entrySet().stream() // 所有 entry 对应的 Stream
|
|
|
+ .filter(entry -> entry.getValue() > 1) // 过滤出元素出现次数大于 1 的 entry
|
|
|
+ .map(entry -> entry.getKey()) // 获得 entry 的键(重复元素)对应的 Stream
|
|
|
+ .collect(Collectors.toList()); // 转化为 List
|
|
|
+ }
|
|
|
+
|
|
|
+ private String printList(List list) {
|
|
|
+ String result = null;
|
|
|
+ if (!StringUtils.isEmpty(list)) {
|
|
|
+ result = list.toString().substring(1, list.toString().length() - 1);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Boolean isNumner(String str) {
|
|
|
+ for(int i=str.length() - 1; i>=0; i--){
|
|
|
+ int chr=str.charAt(i);
|
|
|
+ if(chr < 48 || chr > 57) {
|
|
|
+ if (chr == 46) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private boolean validateDateFormat(String date) {
|
|
|
+ boolean flag = false;
|
|
|
+ if (StringUtils.isEmpty(date)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String regEx1 = "[0-9]{4}/[0-1][0-9]/[0-3][0-9]";
|
|
|
+ String regEX2 = "[0-9]{4}-[0-1][0-9]-[0-3][0-9]";
|
|
|
+ // 编译正则表达式
|
|
|
+ Pattern pattern1 = Pattern.compile(regEx1);
|
|
|
+ Pattern pattern2 = Pattern.compile(regEX2);
|
|
|
+ Matcher matcher = pattern1.matcher(date);
|
|
|
+ // 字符串是否与正则表达式相匹配
|
|
|
+ flag = matcher.matches();
|
|
|
+ if (!flag) {
|
|
|
+ flag = pattern2.matcher(date).matches();
|
|
|
+ }
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String praseDate(String num) {
|
|
|
+ try {
|
|
|
+ Integer days = Integer.valueOf(num);
|
|
|
+ return DateUtils.plusDay(days, "1899-12-30");
|
|
|
+ }catch (Exception e) {
|
|
|
+ return validateDateFormat(num) ? num : null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //保证先遍历主表
|
|
|
+ private List<String> SetToList(Set<String> sets) {
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ list.add("main");
|
|
|
+ for (String str : sets) {
|
|
|
+ if (!"main".equals(str)) {
|
|
|
+ list.add(str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Object getCellFormatValue(Cell cell){
|
|
|
+ Object cellValue = null;
|
|
|
+ if(cell != null){
|
|
|
+ //判断cell类型
|
|
|
+ switch(cell.getCellType()){
|
|
|
+ case Cell.CELL_TYPE_NUMERIC:{
|
|
|
+ cellValue = String.valueOf(cell.getNumericCellValue());
|
|
|
+ cellValue = RegexpUtils.replaceSpecialCharacterNotcodefield(cellValue);
|
|
|
+ //判断是否为INT类型
|
|
|
+ if (Double.valueOf(cellValue.toString()).intValue() - Double.valueOf(cellValue.toString()) == 0) {
|
|
|
+ return cellValue.toString().substring(0, cellValue.toString().indexOf("."));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case Cell.CELL_TYPE_FORMULA:{
|
|
|
+ //判断cell是否为日期格式
|
|
|
+ if(DateUtil.isCellDateFormatted(cell)){
|
|
|
+ //转换为日期格式YYYY-mm-dd
|
|
|
+ cellValue = cell.getDateCellValue();
|
|
|
+ cellValue = RegexpUtils.replaceSpecialCharacterNotcodefield(cellValue);
|
|
|
+ }else{
|
|
|
+ //数字
|
|
|
+ cellValue = String.valueOf(cell.getNumericCellValue());
|
|
|
+ cellValue = RegexpUtils.replaceSpecialCharacterNotcodefield(cellValue);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case Cell.CELL_TYPE_STRING:{
|
|
|
+ cellValue = cell.getRichStringCellValue().getString();
|
|
|
+ cellValue = RegexpUtils.replaceSpecialCharacterNotcodefield(cellValue);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ cellValue = "";
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ cellValue = "";
|
|
|
+ }
|
|
|
+ return cellValue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected Cell getCell(Sheet sheet, int row, int col) {
|
|
|
+ Row sheetRow = sheet.getRow(row);
|
|
|
+ if (sheetRow == null) {
|
|
|
+ sheetRow = sheet.createRow(row);
|
|
|
+ }
|
|
|
+ Cell cell = sheetRow.getCell(col);
|
|
|
+ if (cell == null) {
|
|
|
+ cell = sheetRow.createCell(col);
|
|
|
+ }
|
|
|
+ return cell;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成excel工作表
|
|
|
+ *
|
|
|
+ * 数据
|
|
|
+ */
|
|
|
+ private void createWorkbook(SXSSFWorkbook workbook, int sheetIdx, JSONArray cols, JSONArray datas, String remark){
|
|
|
+ Sheet sheet = workbook.createSheet("sheet" + sheetIdx);
|
|
|
+ CellStyle style = getCellStyle(workbook, true);
|
|
|
+ CellStyle detailStyle = getCellStyle(workbook, false);
|
|
|
+ //sheet.autoSizeColumn(2);
|
|
|
+ // sheet.createFreezePane(0, 1);// 固定列
|
|
|
+ Cell cell = null;
|
|
|
+ int rIdx = 0;
|
|
|
+ int cIdx = 0;
|
|
|
+ short width = 0;
|
|
|
+ DataFormat format = workbook.createDataFormat();
|
|
|
+ Row row = null;
|
|
|
+ if (remark != null) {
|
|
|
+ row = sheet.createRow(rIdx);
|
|
|
+ row.setHeightInPoints((short) 50);
|
|
|
+ sheet.setColumnWidth(cIdx, 500);
|
|
|
+ cell = getCell(sheet, rIdx, cIdx);
|
|
|
+ cell.setCellValue(remark);
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, cols.size() - 1));
|
|
|
+ rIdx++;
|
|
|
+ }
|
|
|
+ row = sheet.createRow(rIdx);
|
|
|
+ row.setHeightInPoints((short) 22);
|
|
|
+ List<String> keys = new ArrayList<String>();
|
|
|
+ if (!CollectionUtils.isEmpty(cols)) {
|
|
|
+ String value = "";
|
|
|
+ //列出主表的列
|
|
|
+ for (int i = 0; i < cols.size(); i++) {
|
|
|
+ JSONObject obj = (JSONObject) cols.get(i);
|
|
|
+ width = (short) (obj.get("width") == null ? 0 : (int)obj.get("width")*35.7);
|
|
|
+ width = width == 0 ? 4000 : width;
|
|
|
+ sheet.setColumnWidth(cIdx, width);
|
|
|
+ // sheet.createFreezePane(cols.size() + 1, 2);
|
|
|
+ cell = getCell(sheet, rIdx, cIdx);
|
|
|
+ value = obj.get("description") == null ? value : obj.get("description").toString();
|
|
|
+ if ("true".equals(obj.get("necessary"))) {
|
|
|
+ value = "*" + value;
|
|
|
+ }
|
|
|
+ if ("date".equals(obj.get("type"))) {
|
|
|
+ short df= workbook.createDataFormat().getFormat("yyyy-MM-dd");
|
|
|
+ style.setDataFormat(df);
|
|
|
+ }
|
|
|
+ if ("main".equals(obj.get("position"))) {
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ } else {
|
|
|
+ cell.setCellStyle(detailStyle);
|
|
|
+ }
|
|
|
+ cell.setCellValue(value);
|
|
|
+ keys.add(String.valueOf(obj.get("description")));
|
|
|
+ cIdx++;
|
|
|
+ value = "";
|
|
|
+ }
|
|
|
+ rIdx++;
|
|
|
+ if (!CollectionUtils.isEmpty(datas)) {
|
|
|
+ for(Object d :datas) {
|
|
|
+ JSONObject data = (JSONObject)d;
|
|
|
+ cIdx = 0;
|
|
|
+ row = sheet.createRow(rIdx);
|
|
|
+ row.setHeightInPoints((short) 20);
|
|
|
+ for (String key : keys) {
|
|
|
+ Cell c = getCell(sheet, rIdx, cIdx);
|
|
|
+ if (data.get(key) != null) {
|
|
|
+ String v = String.valueOf(data.get(key));
|
|
|
+ if ("".equals(v) || "null".equals(v)) {
|
|
|
+ c.setCellValue(0);
|
|
|
+ } else if (!v.matches("^-?[0-9]+(.[0-9]+)?")) {
|
|
|
+ c.setCellValue(v);
|
|
|
+ } else {
|
|
|
+ c.setCellValue(Double.parseDouble(v.replace(",", "")));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ c.setCellValue("");
|
|
|
+ }
|
|
|
+ cIdx++;
|
|
|
+ }
|
|
|
+ rIdx++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private CellStyle getCellStyle(SXSSFWorkbook workbook, boolean main) {
|
|
|
+ CellStyle style = workbook.createCellStyle();
|
|
|
+ Font font = workbook.createFont();
|
|
|
+ font.setFontName("仿宋_GB2312");// 字体
|
|
|
+ font.setFontHeightInPoints((short) 12);// 字号
|
|
|
+ font.setBold(true);
|
|
|
+ font.setColor((short)64);// 颜色
|
|
|
+ style.setFont(font);
|
|
|
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ style.setFillForegroundColor(main ? HSSFColor.LIGHT_YELLOW.index : HSSFColor.PALE_BLUE.index);
|
|
|
+ style.setBorderBottom(BorderStyle.MEDIUM);
|
|
|
+ style.setBorderLeft(BorderStyle.MEDIUM);
|
|
|
+ style.setBorderRight(BorderStyle.MEDIUM);
|
|
|
+ style.setBorderTop(BorderStyle.MEDIUM);
|
|
|
+ return style;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|