项目中使用Excel建立任务,然后上传到fastdfs,此处只摘出Excel解析的逻辑以及分析后失败结果回写的逻辑。
/*** Bestpay.com.cn Inc.* Copyright (c) 2011-2016 All Rights Reserved.*/
package com.bestpay.messagecenter.product.manager.model;/*** @author linxing* @version Id: TaskCounter.java, v 0.1 2017/9/26 15:18 linxing Exp $$*/
public class TaskCounter {/*** 任务号--打日志*/private Integer taskId;/*** 消息条数*/private int     totalCount;/*** 有效数*/private int     validCount;/*** 重复消息条数* */private int     repeatCount;/*** 校验失败数*/private int     errorCount;/*** 运营商过滤数*/private int     filterCount;/*** 获取 totalCount* return totalCount*/public Integer getTotalCount() {return totalCount;}/*** 设置 totalCount* return*/public void setTotalCount(Integer totalCount) {this.totalCount = totalCount;}/*** 获取 validCount* return validCount*/public Integer getValidCount() {return validCount;}/*** 设置 validCount* return*/public void setValidCount(Integer validCount) {this.validCount = validCount;}/*** 获取 repeatCount* return repeatCount*/public Integer getRepeatCount() {return repeatCount;}/*** 设置 repeatCount* return*/public void setRepeatCount(Integer repeatCount) {this.repeatCount = repeatCount;}/*** 获取 errorCount* return errorCount*/public Integer getErrorCount() {return errorCount;}/*** 设置 errorCount* return*/public void setErrorCount(Integer errorCount) {this.errorCount = errorCount;}/*** 获取 filterCount* return filterCount*/public Integer getFilterCount() {return filterCount;}/*** 设置 filterCount* return*/public void setFilterCount(Integer filterCount) {this.filterCount = filterCount;}/*** 获取 taskId* return taskId*/public Integer getTaskId() {return taskId;}/*** 设置 taskId* return*/public void setTaskId(Integer taskId) {this.taskId = taskId;}@Overridepublic String toString() {return "TaskCounter{" + "taskId=" + taskId + ", totalCount=" + totalCount + ", validCount="+ validCount + ", repeatCount=" + repeatCount + ", errorCount=" + errorCount+ ", filterCount=" + filterCount + '}';}
}
package com.bestpay.messagecenter.product.manager.model;import java.util.List;/*** 文件信息类* @author linxing* @version Id: FileParserHelper.java, v 0.1 2017/3/17 13:06 linxing Exp $$*/
public class FileInfo {/*** 数据*/private List<String> data;/*** 当前行号*/private int          line;/*** 获取 data* return data*/public List<String> getData() {return data;}/*** 设置 data* return*/public void setData(List<String> data) {this.data = data;}/*** 获取 line* return line*/public int getLine() {return line;}/*** 设置 line* return*/public void setLine(int line) {this.line = line;}
}
/*** 文件解析类* @author linxing* @version Id: FileParserHelper.java, v 0.1 2017/3/17 13:06 linxing Exp $$*/
@Slf4j
public class FileParserHelper {/*** private constructor*/private FileParserHelper() {}private LinkedList<List<String>> lists;/*** @return*/public static FileParserHelper getInstance() {return new FileParserHelper();}/*** 输入流----fastdfs* @param inputStream* @return*/public FileParserHelper readExcel(InputStream inputStream) {long start = System.currentTimeMillis();log.info("解析任务中,从流中读取数据开始");try (Workbook workbook = WorkbookFactory.create(inputStream)) {Sheet sheet = workbook.getSheetAt(0);int rowCount = sheet.getLastRowNum();lists = new LinkedList();for (int r = 0; r <= rowCount; r++) {Row row = sheet.getRow(r);if (row == null) {continue;}int cellCount = row.getLastCellNum();List<String> list = new ArrayList<>();for (int c = 0; c < cellCount; c++) {Cell cell = row.getCell(c, Row.CREATE_NULL_AS_BLANK);cell.setCellType(Cell.CELL_TYPE_STRING);list.add(cell.getStringCellValue());}filterBlankCell(list);fixDataWithSpecialSign(list);lists.add(list);}} catch (Exception e) {log.error("解析Excel文件出错:{}", Throwables.getStackTraceAsString(e));throw new MessageCenterProductFault(MessageCenterProductErrorCode.TASK_FILE_ANALYSIS_FAIL, "解析Excel文件失败,格式有误");}log.info("解析任务中,从流中读取数据结束,耗时:{}", System.currentTimeMillis() - start);return this;}/*** 过滤两端的空单元格* @param list*/private void filterBlankCell(List<String> list) {if (CollectionUtil.isEmpty(list)) {return;}//如果全部是空值,这里不处理,去重的时候过滤if (!isNotBlankRow(list)) {return;}while (StringUtils.isEmpty(list.get(0))) {list.remove(0);}while (StringUtils.isEmpty(list.get(list.size() - 1))) {list.remove(list.size() - 1);}}/*** 替换特殊字符方法 目前处理换行和空格* @param list*/private void fixDataWithSpecialSign(List<String> list) {if (CollectionUtil.isEmpty(list)) {return;}for (int i = 0; i < list.size(); i++) {list.set(i, PropertUtil.fillNull(list.get(i)).replaceAll("\r\n|\r|\n", "").trim());}}/*** 去重(统计总数,重复数)* @param countDO 统计的DO* @param indexs  校验重复值所需的索引 比如:如果只需要用手机号判断重复,只传手机号所在的索引 如果有多列逗号分隔* @return*/public Map<String, FileInfo> uniqueDate(TaskCounter countDO, String indexs) {long start = System.currentTimeMillis();log.info("解析任务中,去重开始,taskId:{}", countDO.getTaskId());int[] indexForUnique = initUniqueIndex(indexs);Map<String, FileInfo> uniqueMap = new LinkedHashMap<>();int repeatCount = 0;int totalCount = 0;int index = 0;for (List<String> line : lists) {index++;//filter blank rowif (!isNotBlankRow(line)) {log.info("第" + (index) + "行出现空行,已过滤");continue;}totalCount++;FileInfo fileInfo = new FileInfo();String key = "";for (int eachIndex : indexForUnique) {key = key.concat(PropertUtil.fillNull(line.get(eachIndex)));}if (uniqueMap.containsKey(key)) {repeatCount++;continue;}fileInfo.setData(line);fileInfo.setLine(index);uniqueMap.put(key, fileInfo);}countDO.setRepeatCount(repeatCount);countDO.setTotalCount(totalCount);log.info("解析任务中,去重结束,taskId:{},耗时:{}", countDO.getTaskId(),System.currentTimeMillis() - start);return uniqueMap;}private static boolean isNotBlankRow(List<String> rowData) {boolean result = false;for (String eachColumn : rowData) {if (!StringUtils.isEmpty(eachColumn)) {return true;}}return result;}private int[] initUniqueIndex(String indexs) {//用于区分数据是否重复 传列索引 比如 1,2 表示1 2列的值相同 这行数据重复if (StringUtils.isEmpty(indexs)) {throw new MessageCenterProductFault(MessageCenterProductErrorCode.TASK_FILE_ANALYSIS_FAIL, "解析Excel文件失败,没有定义区分重复值的条件");}int[] indexForUnique;if (indexs.indexOf(",") < 0) {indexForUnique = new int[] { Integer.parseInt(indexs) };return indexForUnique;}String[] indexStr = indexs.split(",");indexForUnique = new int[indexStr.length];for (int i = 0; i < indexStr.length; i++) {indexForUnique[i] = Integer.parseInt(indexStr[i]);}return indexForUnique;}
}
/*** Bestpay.com.cn Inc.* Copyright (c) 2011-2016 All Rights Reserved.*/
package com.bestpay.messagecenter.product.manager;import java.io.InputStream;
import java.util.List;
import java.util.Map;import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;import com.bestpay.messagecenter.product.manager.model.AppFileItem;
import com.bestpay.messagecenter.product.manager.model.FileItem;
import com.google.common.base.Throwables;import lombok.extern.slf4j.Slf4j;/*** @author linxing* @version Id: FileResultUploadHelper.java, v 0.1 2017/5/12 9:41 linxing Exp $$*/
@Slf4j
public class FileResultUploadHelper {private FileResultUploadHelper() {}/*** 上传短信任务解析结果文件* @param inputStream* @param result*/public static byte[] uploadResultFileForSms(InputStream inputStream,List<FileItem> result) {try (Workbook workbook = WorkbookFactory.create(inputStream);ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {Sheet sheet = workbook.getSheetAt(0);Font font = workbook.createFont();font.setColor(Font.COLOR_RED);font.setBoldweight(Font.BOLDWEIGHT_BOLD);CellStyle cellStyle = workbook.createCellStyle();cellStyle.setFont(font);for (FileItem each : result) {if (!StringUtils.isEmpty(each.getErrorInfo())) {Row row = sheet.getRow(each.getLine() - 1);if (row == null) {continue;}//因为Excel样式问题,目前连续超过三个空单元格即视为到达列末尾Cell cell = row.createCell(getLastNoEmptyColNo(row) - 1);cell.setCellStyle(cellStyle);cell.setCellValue(each.getErrorInfo());}}workbook.write(outputStream);outputStream.flush();return outputStream.toByteArray();} catch (Exception e) {log.error("上传任务解析结果文件出错:{}", Throwables.getStackTraceAsString(e));}return null;}private static int getLastNoEmptyColNo(Row row) {boolean isFind = false;int col = 0;while (!isFind) {Cell c1 = row.getCell(col, Row.CREATE_NULL_AS_BLANK);Cell c2 = row.getCell(col + 1, Row.CREATE_NULL_AS_BLANK);Cell c3 = row.getCell(col + 2, Row.CREATE_NULL_AS_BLANK);if (StringUtils.isEmpty(String.valueOf(getCellValue(c1)))&& StringUtils.isEmpty(String.valueOf(getCellValue(c2)))&& StringUtils.isEmpty(String.valueOf(getCellValue(c3)))) {isFind = true;}col++;}return col;}private static Object getCellValue(Cell cell) {int cellType = cell.getCellType();switch (cellType) {case Cell.CELL_TYPE_STRING:return cell.getStringCellValue();case Cell.CELL_TYPE_BOOLEAN:return cell.getBooleanCellValue();case Cell.CELL_TYPE_FORMULA:return cell.getCellFormula().trim();case Cell.CELL_TYPE_NUMERIC:return cell.getNumericCellValue();case Cell.CELL_TYPE_BLANK:default:return "";}}
}

Excel文件解析和结果回写相关推荐

  1. java实现Excel文件解析---apache POI以及把汉字转化为拼音

    java实现Excel文件解析----apache  POI以及把汉字转化为拼音 1.POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供给Java程序对Microso ...

  2. jxls使用excel公司_使用jXLS将Excel文件解析为JavaBeans

    jxls使用excel公司 这篇文章展示了如何使用jXLS将Excel文件解析为JavaBeans列表. 这是我编写的通用实用程序方法: /** * Parses an excel file into ...

  3. 使用jXLS将Excel文件解析为JavaBeans

    这篇文章展示了如何使用jXLS将Excel文件解析为JavaBeans列表. 这是我编写的通用实用程序方法: /** * Parses an excel file into a list of bea ...

  4. 在线Excel文件解析转换成JSON格式

    在线Excel文件解析转换成JSON格式 在线Excel文件解析转换成JSON格式 本工具可以将上传的Excel文件解析转换成JSON格式,支持下载 本工具可以将上传的Excel文件解析转换成JSON ...

  5. Excel文件解析性能对比(POI,easyexcel,xlsx-streamer)

    问题: 在解析excel内容时,由于用户误操作,偶尔会误录入大量空行,如果代码处置不当,进行文档内容解析时,就会占用大量时间和内存,甚至引起服务器内存消耗殆尽,导致服务无法访问,或者引起OOM. 解决 ...

  6. java中 Excel文件解析及超大Excel文件读写

    本文主要对Excel中数据的解析和生成进行总结 前言 在应用程序的开发过程中,我们经常要用到Excel进行数据的导入或导出.所以,在通过Java语言实现此类需求时,通常会对Excel文件进行解析或生成 ...

  7. EXCEL通过配置导入规则,实现任意格式EXCEL文件解析

    EXCEL文件导入到数据库,是一种常见的文件导入格式. 本文实现的EXCEL文件解析方法,优点是适合任意格式的EXCEL文件,缺点是每一张表单都需要配置对应的导入规则,在字段数量多时,配置导入规则就尴 ...

  8. 浅谈Excel文件解析

    日常生活中,我们经常会遇到一些Excel文件,一般我们只会去用,不会去想他到底是怎样进行数据导入和导出的,今天,我们看看在java应用开发过程中,Excel文件的解析过程. 1. 使用-XSSF解析E ...

  9. Java进度条(excel文件解析)的实现

    文件上传页面 <!doctype html> <html> <head> <meta charset="utf-8"> <ti ...

最新文章

  1. grunt 打包前端代码
  2. java中Object.equals()简单用法
  3. Bailian4068 判断是否可以构成等差数列【字符串流+排序】
  4. C语言小游戏,编程入门必看,初级扫雷
  5. f分布表完整图_如何用Excel制作频率(频数)分布表(图)?
  6. ICT项目管理与实施体系
  7. 小程序样式写了没有用,或许你就差一行代码
  8. 大写加下划线转换驼峰规则
  9. dedecms 安装后 管理后台ie假死 无响应的解决方法
  10. 利用Python3实现:非常“实用”的身高计算器(源代码)
  11. 文献(8): 单细胞和空间分析揭示FAP+成纤维细胞和SPP1+巨噬细胞在结直肠癌中的相互作用
  12. 【软件侠】公认最常用的20个函数,案例详解
  13. 自定义画圆进度条,带波浪动态效果
  14. javascript小技巧
  15. 易地推招生拓客分享:如何让社群招生成为培训机构招生利器?
  16. xsl:apply-templates和xsl:call-template的区别
  17. 微信网页开发(8)--地理位置接口
  18. iOS开发xcode报错:xxxxxxhas been modified since the precompiled header was built
  19. gitee搭建图床流程
  20. 吴恩达 CS230 官方指南:CNN、RNN 及使用技巧速查手册

热门文章

  1. android改变系统语言,Android 9.0设置系统语言
  2. python-regex-thread-localhost
  3. 什么是数字孪生?数字孪生技术有没有真正的实用价值?
  4. 关于ubuntu浏览器模糊不清的解决方法
  5. 排序相关算法在计算机程序设计竞赛中的研究
  6. 自动摘要生成(二):由PageRank转变而来的TextRank算法
  7. 用Dijkstra算法找到图上两点之间的最短路径
  8. 父母脾气暴躁对孩子有哪些影响
  9. lisp语言画地物符号_LISP语言在CAD工程制图中的应用_谢威
  10. 程序员公众号编辑神器-mdnice