比较两个Excle表格的修改内容
//输入参数为文件输入流
public static Map<String, List<String>> excelColumn2maplist(InputStream is)
{
//存储某一列数据
Map<String, List<String>> maplist = new HashMap<>();
//存储表头字段
List<String> tableHeadfields = new ArrayList<>();
XSSFWorkbook workBook = null;
try
{
workBook = new XSSFWorkbook(is);
}
catch (IOException e)
{
logger.error("读取工作簿出错" + e.getMessage());
} Sheet sheet = workBook.getSheetAt(0);
Row tableHeadRow = sheet.getRow(0); for (int i = 0; i < tableHeadRow.getLastCellNum(); i++)
{
tableHeadfields.add(getValue(tableHeadRow, i));
}
//所有表头字段所在列的索引
int severNameIndex = tableHeadfields.indexOf("severName");
int fileNameIndex = tableHeadfields.indexOf("fileName");
int keyIndex = tableHeadfields.indexOf("key");
int valueIndex = tableHeadfields.indexOf("value");
int versionIndex = tableHeadfields.indexOf("version");
int sysEnvRelatedIndex = tableHeadfields.indexOf("sysEnvRelated");
int markIndex = tableHeadfields.indexOf("mark");
int typeIndex = tableHeadfields.indexOf("type");
int validate_resultIndex = tableHeadfields.indexOf("validate_result");
// int tableHeadfieldsSize = tableHeadfields.size(); //将数据列放进map,规则是同一行,后一个字段包括前一个字段作为前缀,以分割符Constants.FieldSeparator(<SpS>) 进行分隔
List<String> severNameList = new ArrayList<>();
List<String> fileNameList = new ArrayList<>();
List<String> keyList = new ArrayList<>();
List<String> valueList = new ArrayList<>();
List<String> versionList = new ArrayList<>();
List<String> sysEnvRelatedList = new ArrayList<>();
List<String> markList = new ArrayList<>();
List<String> typeList = new ArrayList<>();
List<String> validate_resultList = new ArrayList<>(); StringBuilder sb = new StringBuilder();
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++)
{
Row row = sheet.getRow(rowNum);
if (row == null)
{
continue;
}
sb.append(getValue(row, severNameIndex)).append(Constants.FieldSeparator);
severNameList.add(sb.toString());
sb.append(getValue(row, fileNameIndex)).append(Constants.FieldSeparator);
fileNameList.add(sb.toString());
sb.append(getValue(row, keyIndex)).append(Constants.FieldSeparator);
keyList.add(sb.toString());
sb.append(getValue(row, valueIndex)).append(Constants.FieldSeparator);
valueList.add(sb.toString());
sb.append(getValue(row, versionIndex)).append(Constants.FieldSeparator);
versionList.add(sb.toString());
sb.append(getValue(row, sysEnvRelatedIndex)).append(Constants.FieldSeparator);
sysEnvRelatedList.add(sb.toString());
sb.append(getValue(row, markIndex)).append(Constants.FieldSeparator);
markList.add(sb.toString());
sb.append(getValue(row, typeIndex)).append(Constants.FieldSeparator);
typeList.add(sb.toString());
sb.append(getValue(row, validate_resultIndex)).append(Constants.FieldSeparator);
validate_resultList.add(sb.toString());
sb.delete(0, sb.length()); }
maplist.put("severName", severNameList);
maplist.put("fileName", fileNameList);
maplist.put("key", keyList);
maplist.put("value", valueList);
maplist.put("version", versionList);
maplist.put("sysEnvRelated", sysEnvRelatedList);
maplist.put("mark", markList);
maplist.put("type", typeList);
maplist.put("validate_result", validate_resultList); return maplist;
} private static String getValue(Row row, int cellNum)
{
if (row.getCell(cellNum) == null)
{
return "";
}
if (row.getCell(cellNum).getCellType() == row.getCell(cellNum).CELL_TYPE_BOOLEAN)
{
return String.valueOf(row.getCell(cellNum).getBooleanCellValue()).trim();
}
else if (row.getCell(cellNum).getCellType() == row.getCell(cellNum).CELL_TYPE_NUMERIC)
{
return String.valueOf(row.getCell(cellNum).getNumericCellValue()).trim();
}
else
{
return String.valueOf(row.getCell(cellNum).getStringCellValue()).trim();
}
} public static void compare2MapList(Map<String, List<String>> refMaplist, Map<String, List<String>> compareMaplist,
List<String> newAddlist, List<String> removelist, List<ArrayList<String>> updatelist)
{
/* Map<String, List<String>> difflist = new HashMap<>();
List<String> newAddlist = new ArrayList<>();
List<String> removelist = new ArrayList<>();
List<ArrayList<String>> updatelist =new ArrayList<ArrayList<String>>();
difflist.put("newAdd", newAddlist);
difflist.put("remove", removelist);*/ for (Map.Entry<String, List<String>> entry : refMaplist.entrySet())
{
//得到字段名
String reffieldName = entry.getKey();
//得到字段名对应的参考源列表
List<String> reffieldList = entry.getValue();
//得到字段名对应的比较源列表
List<String> comparefieldList = compareMaplist.get(reffieldName); //1.相对于参考源新添加的字段
for (int i = 0; i < comparefieldList.size(); i++)
{
Boolean IsExist = reffieldList.contains(comparefieldList.get(i));
if (IsExist == false)
{
newAddlist.add(comparefieldList.get(i));
}
} //2.相对于参考源移除的变量
for (int i = 0; i < reffieldList.size(); i++)
{
Boolean IsExist = comparefieldList.contains(reffieldList.get(i));
if (IsExist == false)
{
removelist.add(reffieldList.get(i));
}
} //3.参考源,比较源中更改过的 //参考源中存在,比较源中不存在
List<String> AdiffBlist = getDiffABList(reffieldList, comparefieldList);
//比较源中存在,参考源中不存在
List<String> BdiffAlist = getDiffBAList(reffieldList, comparefieldList); for (int i = 0; i < BdiffAlist.size(); i++)
{
String originBStr = getBeforeLastSeparatorStr(BdiffAlist.get(i));
for (int j = 0; j < AdiffBlist.size(); j++)
{
String originAStr = getBeforeLastSeparatorStr(AdiffBlist.get(j));
if (originBStr.equals(originAStr))
{
ArrayList<String> updateFieldlist = new ArrayList<String>();
//第一个为更改后的
updateFieldlist.add(BdiffAlist.get(i));
//第一个为参考源的
updateFieldlist.add(AdiffBlist.get(j));
updatelist.add(updateFieldlist);
}
} } } } public static String getBeforeLastSeparatorStr(String str)
{
String[] strings = str.split(Constants.FieldSeparator);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strings.length - 2; i++)
{
sb.append(strings[i]);
}
return sb.toString();
} public static List<String> getDiffABList(List<String> listA, List<String> listB)
{
List<String> retainAllResult = new ArrayList<String>();
retainAllResult.addAll(listA);
retainAllResult.retainAll(listB);
List<String> diffABResult = new ArrayList<String>();
diffABResult.addAll(listA);
diffABResult.removeAll(retainAllResult);
return diffABResult;
} public static List<String> getDiffBAList(List<String> listA, List<String> listB)
{
List<String> retainAllResult = new ArrayList<String>();
retainAllResult.addAll(listB);
retainAllResult.retainAll(listA);
List<String> diffABResult = new ArrayList<String>();
diffABResult.addAll(listB);
diffABResult.removeAll(retainAllResult);
return diffABResult;
} public static List<String> getRetainABList(List<String> listA, List<String> listB)
{
List<String> retainAllResult = new ArrayList<String>();
retainAllResult.addAll(listA);
retainAllResult.retainAll(listB); return retainAllResult;
}
public static void main(String[] args)
throws Exception
{
File file = new File("D:\\TestFile\\input.xlsx");
File file1 = new File("D:\\TestFile\\input1.xlsx");
InputStream inputStream = null;
InputStream inputStream1 = null;
try
{
inputStream = new FileInputStream(file);
inputStream1 = new FileInputStream(file1);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
Map<String, List<String>> refMaplist = excelColumn2maplist(inputStream);
Map<String, List<String>> compareMaplist = excelColumn2maplist(inputStream1);
List<String> newAddlist = new ArrayList<>();
List<String> removelist = new ArrayList<>();
List<ArrayList<String>> updatelist = new ArrayList<>();
compare2MapList(refMaplist, compareMaplist, newAddlist, removelist, updatelist);
System.out.println(newAddlist);
System.out.println("*************************************");
System.out.println(removelist);
System.out.println("*************************************");
for (int i = 0; i < updatelist.size() - 1; i++)
{
System.out.println(updatelist.get(i));
} String str1 =
"aggregation-mmpsourceservice<SpS>source_config.properties<SpS>youku.mobile.import.subtypes<SpS>大<SpS>";
String str2 =
"aggregation-mmpsourceservice<SpS>source_config.properties<SpS>youku.mobile.import.subtypes<SpS>大<SpS>";
String str11 = getBeforeLastSeparatorStr(str1);
String str22 = getBeforeLastSeparatorStr(str2);
System.out.println("---------------------------------");
System.out.println(str11);
System.out.println("---------------------------------");
System.out.println(str22);
}
比较两个Excle表格的修改内容的更多相关文章
- java导入excle表格,并且对表格进行相应的修改,并对表格数据进行整理,最后导出本地表格等一系列操作
1.首先创建一个java项目 完成效果如下图所示 2.导入以下jar包 3.代码如下 其中行和列的操作是根据需求自动划分的 public class auto_date { private stati ...
- 根据excel表格中的内容更新Sql数据库
关于[无法创建链接服务器 "(null)" 的 OLE DB 访问接口 SQL Server 2008读取EXCEL数据时,可能会报这个错误:无法创建链接服务器 "(nu ...
- ANT 的Table表格样式修改方法
注:(大家在给页面添加参数或者方法的时候,记得写上注释,方便别人查看) 1.表格行选中样式添加:(可以去beijing,精子库质控统计查看例子) (咱们以前页面上的表格都是在hover时显示选中效果, ...
- React+后端实现导出Excle表格的功能
最近在做一个基于React+antd前端框架的Excel导出功能,我主要在后端做了处理,根据以下步骤,可以很容易就实现导出Excel表格数据的功能. 在做这类导出文件的功能,其实,在后端进行处理,会更 ...
- javascript生成表格增删改查 JavaScript动态改变表格单元格内容 动态生成表格 JS获取表格任意单元格 javascript如何动态删除表格某一行
jsp页面表格布局Html代码 <body onload="show()"> <center> <input type="text" ...
- JavaScript动态改变表格单元格内容的方法
本文实例讲述了JavaScript动态改变表格单元格内容的方法.分享给大家供大家参考.具体如下: JavaScript动态改变表格单元格的内容,下面的代码通过修改单元格的innerHTML来修改单元格 ...
- 采用DOM进行表格的修改操作
2015-08-31 <html> <head> <title>采用DOM进行表格的修改操作</title> <script language=& ...
- git教程: 查看文件状态与修改内容
转载:时光机穿梭 我们已经成功地添加并提交了一个readme.txt文件,现在,是时候继续工作了,于是,我们继续修改readme.txt文件,改成如下内容: Git is a distributed ...
- html怎样让表格里面的内容居中
html怎样让表格里面的内容居中 text-align:center; 在表格td中,有两个属性控制居中显示 align——表示左右居中——left,center,right valign——控制上下 ...
随机推荐
- hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...
- javascript 高级程序设计 八
function 类型: 1.ECMAscript中函数和类C语言的函数有这很多不同.其中之一就是js的函数没有重载.并且多次定义一个同名的函数,当调用这个函数的时候, 会调用最后一次定义的函数. 2 ...
- Centos Raid0 与Raid1 的备注
http://www.360doc.com/content/13/1209/21/14661619_335823338.shtml raid0 如果坏了一块硬盘.那么数据就无法读取了 raid1 如果 ...
- Python之路(第十篇)迭代器协议、for循环机制、三元运算、列表解析式、生成器
一.迭代器协议 a迭代的含义 迭代器即迭代的工具,那什么是迭代呢? #迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 b为何要有迭代器? 对于序列类型:字符串.列表 ...
- 【jdbcTemplate】baseDao书写规范
今天加班,为了下个月的北京之行,希望父亲身体安康,一切顺利: 老大今天发出来同事的代码,并标记了jdbcTemplate的书写规范,此处查询数据库之前声明对象时,不用new出来,因为在底层源码中已经给 ...
- EasyUITree设置节点选中
function callback1(){ reloadMind(); LoadTree0($("); setTimeout(function(){ var node = $('#tt1') ...
- Permutation Sequence LT60
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- Lecture 5
- 2018.10.30 NOIP模拟 排列树(树形dp+组合数学)
传送门 考试的时候乱搞过了. 其实题目就是让你求拓扑排序方案数. 直接树形dpdpdp然后组合数转移一下就行了. 乱搞代码
- js生成条形码插件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...