因为项目用到了Excel导入功能,已经实现该功能了,但是其中还有一个很严重的问题,就是每次导入excel后,如果excel里有重复的记录,添加记录到数据库后不会相加,提供图片及代码,清知道的人跟小女子分析下,感激不尽。Excel中的数据如下:
这是读Excel及写入数据库sqlserver2005的代码
public static TagInit processExcel(InputStream inputStream, EpcService epcService) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);//创建对Excel工作薄文件的引用
HSSFSheet sheet = workbook.getSheetAt(0);//创建对工作表的引用,也可用workbook.getSheet("sheetName");
TagInit master = new TagInit();
int totQty = 0;
int i = 1;//从第二行开始读数据
HSSFRow row = sheet.getRow(i);
String cell0 = row.getCell(0).getStringCellValue().trim();//款
String cell1= "";
// String cell1 = row.getCell(1).getStringCellValue().trim();//色 try {
cell1 = row.getCell(1).getStringCellValue().trim();;
} catch(Exception e) {
int temp = (int) row.getCell(1).getNumericCellValue();
if(temp>=10)
cell1 = ""+temp;
else cell1 = "0"+temp;
} //String cell2 = row.getCell(2).getStringCellValue().trim();//尺寸
String cell2 = "";
try {
cell2 = row.getCell(2).getStringCellValue();
} catch(Exception e) {
int temp = (int) row.getCell(2).getNumericCellValue();
if(temp>=10)
cell2 = ""+temp;
else cell2 = "0"+temp;
}
Integer qty = (int) row.getCell(3).getNumericCellValue();//数量
String makeOrderNo = row.getCell(4).getStringCellValue();//制单号
HashSet<TagInitDetail> details = new HashSet<TagInitDetail>();
while(!CommonUtil.isBlank(row) && (CommonUtil.isLetterOrNum(cell0)
&& CommonUtil.isLetterOrNum(cell1)
&& CommonUtil.isLetterOrNum(cell2)
&& CommonUtil.isLetterOrNum(""+qty))) {
TagInitDetail detail = new TagInitDetail();
detail.setStyleId(cell0);
detail.setColorId(cell1);
detail.setSizeId(cell2);
detail.setSkuNo(cell0+cell1+cell2);
detail.setExpStartSerialNumber(epcService.findMaxNoBySkuNo(detail.getSkuNo())+1);
detail.setExpEndSerialNumber(epcService.findMaxNoBySkuNo(detail.getSkuNo())+qty);
detail.setExpQty(qty);
detail.setMakeOrderNo(makeOrderNo);
detail.setJobStatus("-1");
detail.setOpStatus("0");
totQty = totQty+qty;
detail.setMaster(master);
details.add(detail);
i++;
row = sheet.getRow(i);
if(row != null)
if( row.getCell(0) != null && !CommonUtil.isBlank(row.getCell(0).getStringCellValue())) {
cell0 = row.getCell(0).getStringCellValue();
try {
cell1 = row.getCell(1).getStringCellValue();
} catch(Exception e) {
int temp = (int) row.getCell(1).getNumericCellValue();
if(temp>=10)
cell1 = ""+temp;
else cell1 = "0"+temp;
}
try {
cell2 = row.getCell(2).getStringCellValue();
} catch(Exception e) {
int temp = (int) row.getCell(2).getNumericCellValue();
if(temp>=10)
cell2 = ""+temp;
else cell2 = "0"+temp;
}
qty = (int) row.getCell(3).getNumericCellValue();
makeOrderNo = row.getCell(4).getStringCellValue();//制单号
} else {
break;
}
}
master.setTotExpQty(totQty);
master.setDetails(details);
master.setMakeOrderNo(makeOrderNo);
master.setTotSkuQty(i-1);
return master;
}
一个Excel插入相同数据后,数量没有相加。(该怎么判断呢?)如下图所示:
javaexcel数据库
这是读Excel及写入数据库sqlserver2005的代码
public static TagInit processExcel(InputStream inputStream, EpcService epcService) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);//创建对Excel工作薄文件的引用
HSSFSheet sheet = workbook.getSheetAt(0);//创建对工作表的引用,也可用workbook.getSheet("sheetName");
TagInit master = new TagInit();
int totQty = 0;
int i = 1;//从第二行开始读数据
HSSFRow row = sheet.getRow(i);
String cell0 = row.getCell(0).getStringCellValue().trim();//款
String cell1= "";
// String cell1 = row.getCell(1).getStringCellValue().trim();//色 try {
cell1 = row.getCell(1).getStringCellValue().trim();;
} catch(Exception e) {
int temp = (int) row.getCell(1).getNumericCellValue();
if(temp>=10)
cell1 = ""+temp;
else cell1 = "0"+temp;
} //String cell2 = row.getCell(2).getStringCellValue().trim();//尺寸
String cell2 = "";
try {
cell2 = row.getCell(2).getStringCellValue();
} catch(Exception e) {
int temp = (int) row.getCell(2).getNumericCellValue();
if(temp>=10)
cell2 = ""+temp;
else cell2 = "0"+temp;
}
Integer qty = (int) row.getCell(3).getNumericCellValue();//数量
String makeOrderNo = row.getCell(4).getStringCellValue();//制单号
HashSet<TagInitDetail> details = new HashSet<TagInitDetail>();
while(!CommonUtil.isBlank(row) && (CommonUtil.isLetterOrNum(cell0)
&& CommonUtil.isLetterOrNum(cell1)
&& CommonUtil.isLetterOrNum(cell2)
&& CommonUtil.isLetterOrNum(""+qty))) {
TagInitDetail detail = new TagInitDetail();
detail.setStyleId(cell0);
detail.setColorId(cell1);
detail.setSizeId(cell2);
detail.setSkuNo(cell0+cell1+cell2);
detail.setExpStartSerialNumber(epcService.findMaxNoBySkuNo(detail.getSkuNo())+1);
detail.setExpEndSerialNumber(epcService.findMaxNoBySkuNo(detail.getSkuNo())+qty);
detail.setExpQty(qty);
detail.setMakeOrderNo(makeOrderNo);
detail.setJobStatus("-1");
detail.setOpStatus("0");
totQty = totQty+qty;
detail.setMaster(master);
details.add(detail);
i++;
row = sheet.getRow(i);
if(row != null)
if( row.getCell(0) != null && !CommonUtil.isBlank(row.getCell(0).getStringCellValue())) {
cell0 = row.getCell(0).getStringCellValue();
try {
cell1 = row.getCell(1).getStringCellValue();
} catch(Exception e) {
int temp = (int) row.getCell(1).getNumericCellValue();
if(temp>=10)
cell1 = ""+temp;
else cell1 = "0"+temp;
}
try {
cell2 = row.getCell(2).getStringCellValue();
} catch(Exception e) {
int temp = (int) row.getCell(2).getNumericCellValue();
if(temp>=10)
cell2 = ""+temp;
else cell2 = "0"+temp;
}
qty = (int) row.getCell(3).getNumericCellValue();
makeOrderNo = row.getCell(4).getStringCellValue();//制单号
} else {
break;
}
}
master.setTotExpQty(totQty);
master.setDetails(details);
master.setMakeOrderNo(makeOrderNo);
master.setTotSkuQty(i-1);
return master;
}
一个Excel插入相同数据后,数量没有相加。(该怎么判断呢?)如下图所示:
javaexcel数据库
execl中数据—>删除重复项
或者开始->条件格式->突出显示单元格规则->重复值
我也是电商数据的,程序很容易出bug的,能手动解决而且不麻烦的话竟然还是手动解决。
百度一下 HashMap 的使用方法就行了,网上很多~
id 是 临时表的主键,
x,y 是要检查的字段
但EXCEL数据比较多,也可能包含多个A箱货,希望将Excel里重复的这个值相加,再插入数据库。就是这么回事
TagInitDetail 这个实体类里面请重写equals方法和hashCode()
搞定。
貌似可以直接先用excel对三个进行一个排序,之后同一类的在一起了,再导入进简单多了
导入前存在array中,再将此array最重要,再导入。此法效率极高,尽可能减少对数据库的操作。
法一:先插入第1条,再插入第2条时,要先查询一番,看有无重要。
导入前存在array中,再将此array除重复的,再导入。此法效率极高,尽可能减少对数据库的操作。
法一:先插入第1条,再插入第2条时,要先查询一番,看有无重要。
Dao层 Map map=new HashMap()
List list=xxx.findByGroupIndexOf();//表里的联合键 索引
=> 如: key=ID+NAME+deptNo+phone
再把 list赋值给map集合 //效率上可能是有点慢 当时没有想到更好办法
map.put(key,"key");
action层
调用map集合 与 导入集合(DRlist) 做对比
导入集合 DRlist
for(int i=0;i<DRlist.size();i++){
实体类 entity
String key=entity.ID+entity.NAME+entity.deptNo+entity.phone;
map =xxDao.findByGroupIndexOf();
if(map.contains(key)==true) continue; //说明存在
else 添加到数据库 如果你是批量添加 你就放在 一个空list里 完啦一次性添加
}//说了大致思路
在程序中写当然可以 不过如果导入的频繁的话难道每导入一次都要打开程序然后运行吗?
个人感觉不好 而且如果他人要用呢?安装个MyEclipse?写个exe或者python的脚步程序 以excle为数据库把每一行读到的3个字段和数据库中比较
select count(*) from tablename where 3个条件当然用and
从excle中读记录是一个方法 方法名 read_Excel_Method()
在read_Excel_Method()中调用验证方法 check_Mehthod(3个条件)
然后根据check_Mehthod(参数)返回类型去判断 是true或者false
如果是true那就是有 那就update tablename set 数量=数量+excle读到哪一行的数量 where 3个条件成立
如果是false 直接插入 insert into tablename
而且脚本可以定时运行 不用手动运行