poi横纵动态导入
dao层
<insert id ="saveInTarget" parameterType="java.util.List" >
INSERT INTO t_target (quantity,tz_quantity,target_id,class_id,datadate,
country,h_quantity,h_tz_quantity,type,product_model)
values
<foreach collection ="list" item="reddemCode" index= "index" separator =",">
(#{reddemCode.quantity},#{reddemCode.tzQuantity},#{reddemCode.targetId},
#{reddemCode.classId},
#{reddemCode.datadate},#{reddemCode.countryId},#{reddemCode.hQuantity},
#{reddemCode.hTzQuantity},#{reddemCode.type},#{reddemCode.model})
</foreach >
</insert > <delete id="deleteInTarget" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="" close="" separator=";">
DELETE FROM t_target
WHERE class_id= AND DATE_FORMAT(datadate,'%Y%m')=DATE_FORMAT(#{item.datadate},'%Y%m')
AND target_id=#{item.targetId} AND type=#{item.type} </foreach> </delete> <delete id="deleteInShopTarget" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="" close="" separator=";">
DELETE FROM t_target
WHERE class_id= AND DATE_FORMAT(datadate,'%Y%m')=DATE_FORMAT(#{item.datadate},'%Y%m')
AND target_id=#{item.targetId} AND type=#{item.type} </foreach> </delete>
servie层
public String readIndExcel(File file, String fileName,String targetType) throws IOException;
public String readInd2007Excel(File file,String targetType) throws IOException;
service.impl
public String readIndExcel(File file, String fileName, String targetType)
throws IOException {
String extension = fileName.lastIndexOf(".") == - ? "" : fileName
.substring(fileName.lastIndexOf(".") + );
if ("xls".equals(extension)) {
//return read2003Excel(file,targetType);
throw new IOException("Unsupported file type,the suffix name should be xlsx!");
} else if ("xlsx".equals(extension)) {
return readInd2007Excel(file,targetType);
} else {
//throw new IOException("不支持的文件类型");
throw new IOException("Unsupported file type,the suffix name should be xlsx!");
}
}
@Override
public String readInd2007Excel(File file, String targetType)
throws IOException {
StringBuffer msg = new StringBuffer();
List<List<Object>> list = new LinkedList<List<Object>>();
try {
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
// 读取第一章表格内容
XSSFSheet sheet = xwb.getSheetAt();
Object value = null; XSSFRow ro = sheet.getRow();
XSSFCell cells = null;
List<HashMap<String, Object>> modelList = new LinkedList<HashMap<String, Object>>();
List<HashMap<String, Object>> shopList = new LinkedList<HashMap<String, Object>>(); Set<String> setLine = new HashSet<String>(); for (int j = 3; j < ro.getLastCellNum(); j++) {
HashMap<String, Object> hashMap = new HashMap<>();
cells = ro.getCell(j);
if (cells == null) {
continue;
} System.out.println(cells.getCellType()+"-------1-------------");
System.out.println(getCellValueByCell(cells)+"-------2-------------");
if (cells != null && cells.getCellType() != HSSFCell.CELL_TYPE_BLANK
&& getCellValueByCell(cells) != null && !getCellValueByCell(cells).equals("")) { boolean one = setLine.add(getCellValueByCell(cells));
if (!one) {//判断是否有重复数据
msg.append(getText("sale.error.row") + DateUtil.getExcelColumnLabel(j + 1) + " "
+ getText("excel.error.lineRE") + " (" + getCellValueByCell(cells) + ") "
+ "<br/>");
} String productModel = getCellValueByCell(cells);
String model = saleTargetDao.getProductModel(productModel, WebPageUtil.getLoginedUser().getPartyId());
if(model==null || model.equals("")){
msg.append(getText("sale.error.row") + 2 + "(" + getCellValueByCell(cells) + ") "+
getText("sale.error.model") + "<br/>");
}
String keyModel = saleTargetDao.getKeyModel(WebPageUtil.getLoginedUser().getPartyId(), productModel);
if(keyModel==null){
msg.append(getText("sale.error.row") + 2 + "(" + getCellValueByCell(cells) + ") "+
getText("sale.error.keymodel") + "<br/>");
} hashMap.put("line", getCellValueByCell(cells)); modelList.add(hashMap);
}
} System.out.println("========lineList========" + modelList); List<HashMap<String, Object>> allModelList = new LinkedList<HashMap<String, Object>>(); for (int i = ; i <= sheet.getLastRowNum(); i++) {
XSSFRow row = null;
XSSFCell cell = null;
row = sheet.getRow(i);
if(row == null){
break;
} HashMap<String, Object> storeMap = new HashMap<String, Object>();//只能放在循环内,拼接存放数据 String shopName="";
Shop shop=null;
String countryId="";
shopName = row.getCell().getStringCellValue();
shop = shopDao.getShopByNames(shopName);
if (shop == null) {
msg.append(getText("sale.error.row") + (i+)
+ getText("sale.error.shop") +getText("sale.error.cell")+()+" ("+shopName+")"+"<br/>");
} if(row.getCell()!=null && row.getCell().getCellType()!=HSSFCell.CELL_TYPE_BLANK){ if("".equals(targetType)){//7识别为门店
shopName = row.getCell().getStringCellValue();
shop = shopDao.getShopByNames(shopName);
if (shop != null) {
storeMap.put("Store", shop.getShopId() == null ? null
: shop.getShopId());
storeMap.put("StoreName", shopName);
}
}
} SimpleDateFormat dfd = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
SimpleDateFormat format = new SimpleDateFormat("MM/yyyy");
Date date = new Date();
String dt = dfd.format(date);
Date dt1 = dfd.parse(dt);
Date dt2;
String rowDate = ""; if(row.getCell()!=null && row.getCell().getCellType()!=HSSFCell.CELL_TYPE_BLANK){
if(row.getCell().getStringCellValue().length()>){
msg.append(getText("sale.error.row") + (i+)+getText("sale.error.cell")+(+)
+ getText("sale.error.dateTime") + "<br/>");
}else{
try{
format.setLenient(false); date = format.parse(row.getCell().getStringCellValue());//有异常要捕获
dfd.setLenient(false);
String newD = dfd.format(date);
date = dfd.parse(newD);//有异常要捕获
dt2 =dfd.parse(newD);
rowDate = newD;
// if (dt1.getTime() < dt2.getTime()) {
// msg.append(getText("sale.error.row") + (i+1)+getText("sale.error.cell")+(1+1)
// + getText("sale.error.time") + "<br/>");
// }
}catch(Exception e){
msg.append(getText("sale.error.row") + (i+)+getText("sale.error.cell")+(+)
+ getText("sale.error.dateTime") + "<br/>");
}
} }else{
msg.append(getText("sale.error.row") + (i + )+getText("sale.error.cell")+(+)
+ getText("sale.error.dateNo") + "<br/>");
} // XSSFCell cell2 = row.getCell(2); //把excel中的单元格数据转化为String类型
// cell2.setCellType(XSSFCell.CELL_TYPE_STRING);
// storeMap.put("quantity", cell2.getStringCellValue()); if(row.getCell()!=null && row.getCell().getCellType()!=HSSFCell.CELL_TYPE_BLANK ){
try{
format.setLenient(false);
date = format.parse(row.getCell().getStringCellValue());//有异常要捕获
dfd.setLenient(false);
String newD = dfd.format(date);
date = dfd.parse(newD);//有异常要捕获
dt2 =dfd.parse(newD); // if (dt1.getTime() < dt2.getTime()) {
//
// } else {
// storeMap.put("datadate", newD);
// }
storeMap.put("datadate", newD);
}catch(Exception e){ }
} if (row.getCell() != null && row.getCell().getCellType() != HSSFCell.CELL_TYPE_BLANK
&& row.getCell() != null && row.getCell().getCellType() != HSSFCell.CELL_TYPE_BLANK) {
if (row.getCell() != null && row.getCell().getCellType() != HSSFCell.CELL_TYPE_BLANK) {
switch (row.getCell().getCellType()) { case HSSFCell.CELL_TYPE_STRING:
msg.append(getText("sale.error.row") + (i + ) + " " + getText("sale.error.cell")
+ DateUtil.getExcelColumnLabel() + " " + getText("excel.error.num") + "<br/>");
break; case HSSFCell.CELL_TYPE_FORMULA: msg.append(getText("sale.error.row") + (i + ) + " " + getText("sale.error.cell")
+ DateUtil.getExcelColumnLabel() + " " + getText("excel.error.num") + "<br/>"); break; case HSSFCell.CELL_TYPE_NUMERIC: storeMap.put("targetQty", (int) row.getCell().getNumericCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: break; }
} else {
msg.append(getText("sale.error.row") + (i + ) + " " + getText("sale.error.cell")
+ DateUtil.getExcelColumnLabel() + " " + getText("excel.error.quantityNull") + "<br/>"); }
} String type="";
List<Object> linked = new LinkedList<Object>();
for (int m = ; m < modelList.size(); m++) { if (row.getCell(m + ) != null && row.getCell(m + ).getCellType() != HSSFCell.CELL_TYPE_BLANK ) {
switch (row.getCell(m + ).getCellType()) { case HSSFCell.CELL_TYPE_STRING:
msg.append(getText("excel.error.row") + (i + ) + " " + getText("excel.error.cell")
+ DateUtil.getExcelColumnLabel(m + + ) + " " + getText("excel.error.num")
+ "<br/>");
break; case HSSFCell.CELL_TYPE_FORMULA: msg.append(getText("excel.error.row") + (i + ) + " " + getText("excel.error.cell")
+ DateUtil.getExcelColumnLabel(m + + ) + " " + getText("excel.error.num")
+ "<br/>"); break; case HSSFCell.CELL_TYPE_NUMERIC: HashMap<String, Object> modelMap = new HashMap<String, Object>();
modelMap.put("Line", modelList.get(m).get("line")); countryId = WebPageUtil.getLoginedUser().getPartyId();
modelMap.put("Country", countryId == null ? null : countryId); if (rowDate != null && !rowDate.equals("")) { modelMap.put("datadate", rowDate); } if (type != null && !type.equals("")) { modelMap.put("Type", type); } shopName = row.getCell().getStringCellValue();
shop = shopDao.getShopByNames(shopName);
if (shop != null) {
modelMap.put("Store", shop.getShopId() == null ? null
: shop.getShopId());
modelMap.put("StoreName", shopName);
} // if(row.getCell(2)!=null && row.getCell(2).getCellType()!=HSSFCell.CELL_TYPE_BLANK){
// modelMap.put("quantity", row.getCell(2).getNumericCellValue());
// }else{
// modelMap.put("quantity", 0);
// } modelMap.put("quantity", row.getCell(m+).getNumericCellValue());
allModelList.add(modelMap); break; case HSSFCell.CELL_TYPE_ERROR: break; } }else {
HashMap<String, Object> modelMap = new HashMap<String, Object>();
modelMap.put("Line", modelList.get(m).get("line")); countryId = WebPageUtil.getLoginedUser().getPartyId();
modelMap.put("Country", countryId == null ? null : countryId); if (rowDate != null && !rowDate.equals("")) { modelMap.put("datadate", rowDate); } if (type != null && !type.equals("")) { modelMap.put("Type", type); } shopName = row.getCell().getStringCellValue();
shop = shopDao.getShopByNames(shopName);
if (shop != null) {
modelMap.put("Store", shop.getShopId() == null ? null
: shop.getShopId());
modelMap.put("StoreName", shopName);
} // if(row.getCell(2)!=null && row.getCell(2).getCellType()!=HSSFCell.CELL_TYPE_BLANK){
// modelMap.put("quantity", row.getCell(2).getNumericCellValue());
// }else{
// modelMap.put("quantity", 0);
// }
modelMap.put("quantity", ); allModelList.add(modelMap);
} } shopList.add(storeMap); for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell == null) {
continue;
}
DecimalFormat df = new DecimalFormat("");// 格式化 number
// String 字符
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
DecimalFormat nf = new DecimalFormat("");// 格式化数字
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
if ("@".equals(cell.getCellStyle()
.getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = nf.format(cell.getNumericCellValue());
} else {
value = sdf.format(HSSFDateUtil.getJavaDate(cell
.getNumericCellValue()));
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
value = "";
break;
default:
value = cell.toString();
}
if (value == null || "".equals(value)) {
continue;
}
linked.add(value); }
list.add(linked);
} System.out.println("=====size=============" + allModelList.size());
System.out.println("=====allModelList=============" + allModelList); List<SaleTarget> saleTargetList = new ArrayList<SaleTarget>();
List<SaleTarget> deleteTargetList = new ArrayList<SaleTarget>(); List<SaleTarget> shopTargetList = new ArrayList<SaleTarget>(); if(msg.length()<=){
for (int i = ; i < allModelList.size(); i++) {
String shopName="";
String datadate="";
String type="";
System.out.println(allModelList.get(i).get("Line")+"----------Line-----------------");
SaleTarget excel = new SaleTarget();
if(allModelList.get(i).get("StoreName")!=null && allModelList.get(i).get("datadate")!=null
&& "".equals(msg.toString()) && allModelList.get(i).get("Line") != null && allModelList.get(i).get("Line") != ""){ type = "";
excel.setType(); if(allModelList.get(i).get("Store")!=null){
excel.setTargetId((allModelList.get(i)
.get("Store").toString()));
} if(allModelList.get(i).get("datadate")!=null){
datadate = allModelList.get(i).get("datadate")
.toString();
excel.setDatadate(datadate);
} if(allModelList.get(i).get("StoreName")!=null){
shopName = allModelList.get(i).get("StoreName")
.toString();
} excel.setCountryId(WebPageUtil.getLoginedUser().getPartyId()); excel.setModel(allModelList.get(i).get("Line")
.toString()); excel.setClassId(targetType); System.out.println(allModelList.get(i)
.get("quantity").toString()+"----------------------quantity");
BigDecimal bqt = new BigDecimal(allModelList.get(i)
.get("quantity").toString());
excel.setQuantity(Long.parseLong(bqt.setScale(,BigDecimal.ROUND_HALF_UP).toString())); excel.sethQuantity(Integer.parseInt(bqt.setScale(,BigDecimal.ROUND_HALF_UP).toString())); excel.setTzQuantity(Long.parseLong("")); saleTargetList.add(excel);
deleteTargetList.add(excel);
} } System.out.println("=====size=============" + shopList.size());
System.out.println("=====allModelList=============" + shopList);
for (int i = ; i < shopList.size(); i++) { String datadate="";
String type=""; SaleTarget excel = new SaleTarget();
if(shopList.get(i).get("StoreName")!=null && shopList.get(i).get("datadate")!=null
&& "".equals(msg.toString()) ){ type = "";
excel.setType(); if(shopList.get(i).get("Store")!=null){
excel.setTargetId((shopList.get(i)
.get("Store").toString()));
} if(shopList.get(i).get("datadate")!=null){
datadate = shopList.get(i).get("datadate")
.toString();
excel.setDatadate(datadate);
} excel.setCountryId(WebPageUtil.getLoginedUser().getPartyId()); excel.setClassId(""); System.out.println(shopList.get(i)
.get("targetQty").toString()+"----------------------targetQty");
BigDecimal bqt = new BigDecimal(shopList.get(i)
.get("targetQty").toString());
excel.setQuantity(Long.parseLong(bqt.setScale(,BigDecimal.ROUND_HALF_UP).toString())); excel.sethQuantity(Integer.parseInt(bqt.setScale(,BigDecimal.ROUND_HALF_UP).toString())); excel.setTzQuantity(Long.parseLong("")); shopTargetList.add(excel);
deleteTargetList.add(excel);
} } } if(saleTargetList.size()>){
String [] beginDate=saleTargetList.get().getDatadate().split("-");
String [] endDate=saleTargetList.get(saleTargetList.size()-).getDatadate().split("-"); String beg=beginDate[]+"-"+beginDate[]+"-01";
String end=endDate[]+"-"+endDate[]+"-31"; System.out.println(beg+"---------------------beg-------");
System.out.println(end+"---------------------end-------"); saleTargetDao.deleteInTarget(deleteTargetList);
saleTargetDao.saveInTarget(saleTargetList);
} if(shopTargetList.size()>){
String [] beginDate=shopTargetList.get().getDatadate().split("-");
String [] endDate=shopTargetList.get(shopTargetList.size()-).getDatadate().split("-"); String beg=beginDate[]+"-"+beginDate[]+"-01";
String end=endDate[]+"-"+endDate[]+"-31"; System.out.println(beg+"---------------------beg-------");
System.out.println(end+"---------------------end-------"); saleTargetDao.deleteInShopTarget(deleteTargetList);
saleTargetDao.saveInTarget(shopTargetList);
} System.out.println(allModelList);
if (msg.length() > ) {
return msg.toString();
} else {
return "";
} } catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
return msg.toString();
}
}
action层
//导入印尼门店目标
public void importIndStore(){
String targetType = request.getParameter("targetType");
try {
String errorMsg = saleService.readIndExcel(uploadExcel,
uploadExcelFileName,targetType); if ("".equals(errorMsg)) {
WebPageUtil.writeBack("success");
} else {
WebPageUtil.writeBack(errorMsg);
}
} catch (Exception e) {
String errorMsg = e.getMessage();
if (null == errorMsg || "".equals(errorMsg)) {
errorMsg = this.getText("import.error.exist");
}
log.error(e.getStackTrace());
e.printStackTrace();
WebPageUtil.writeBack(errorMsg);
}
}

poi横纵动态导入的更多相关文章
- Python基础篇【第3篇】: Python异常处理、反射、动态导入、利用反射的web框架
异常处理 什么是异常? 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行. 一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误.当P ...
- Python之路-python(面向对象进阶(模块的动态导入、断言、Socket Server))
模块的动态导入 断言 Socket Server 一.模块的动态导入 class C(object): def __init__(self): self.name = "zhangsan&q ...
- JSP-讲解(生成java类、静态导入与动态导入)
一.JSP技术简介 JSP是Java Server Page的缩写,它是Servlet的扩展,它的作用是简化网站的创建和维护. JSP是HTML代码与Java代码的混合体. JSP文件通常以JSP或J ...
- Jsp的include指令静态导入和动态导入的区别
1.什么是静态导入? 静态导入指的是,将一个外部文件嵌入到当前JSP文件中,同时解析这个页面的JSP语句,它会把目标页面的其他编译指令也包含进来. include的静态导入指令使用语法: <%@ ...
- Python 动态导入模块
动态导入模块 目录结构: zhangsandeMacBook-Air:1110 zhangsan$ tree . . ├── lib │ └── aa.py ├── test1.py lib目录下 ...
- JAVA-----基于POI实现对Excel导入
在日常项目开发中, 数据录入和导出是十分普遍的需求,因此,导入导出也成为了开发中一个经典的功能.数据导出的格式一般是excel或者pdf,而批量导入的信息一般是借助excel来减轻工作量,提高效率. ...
- Python 实现接口类的两种方式+邮件提醒+动态导入模块+反射(参考Django中间件源码)
实现接口类的两种方式 方式一 from abc import ABCMeta from abc import abstractmethod class BaseMessage(metaclass=AB ...
- python importlib动态导入模块
一般而言,当我们需要某些功能的模块时(无论是内置模块或自定义功能的模块),可以通过import module 或者 from * import module的方式导入,这属于静态导入,很容易理解. 而 ...
- java的poi技术读取和导入Excel实例
本篇文章主要介绍了java的poi技术读取和导入Excel实例,报表输出是Java应用开发中经常涉及的内容,有需要的可以了解一下. 报表输出是Java应用开发中经常涉及的内容,而一般的报表往往缺乏通用 ...
随机推荐
- latex中使用listings显示代码
\documentclass[12pt,a4paper]{article}\usepackage{ctex}\usepackage{listings}\usepackage{xcolor}\begin ...
- css样式问题解决
1.关于滚动条 (1)布局后由于写了 overflow-y: scroll; 在内容还没有超出就出现了滚动条. 我的解决方法是直接去掉了滚动条: .class::-webkit-scrollbar { ...
- ulimit系统资源的设定
使用ulimit -a 可以查看系统使用的资源 core file size 设定core文件的最大值,单位为区块,如果指定为0,不会产生core文件 data seg size 设定数据段的最大值, ...
- springBoot的数据库操作
一:操作数据库起步 1.Spring-Data-Jpa JPA定义了对象持久化的标准. 目前实现了有Hibernate,TopLink 2.pom添加依赖 <dependency> < ...
- 获取China大陆IP段的范围
这里有几个网站提供了大陆的IP段范围.别问我要这个列表干什么,我也不知道. http://www.ip2location.com/blockvisitorsbycountry.aspx老牌网站,国内很 ...
- 绑定属性 - v-bind
未绑定 <!DOCTYPE html><html><head> <meta charset="utf-8"> <t ...
- 《DSP using MATLAB》Problem 7.9
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- InnoDB引擎体系架构
InnoDB引擎架构介绍 innodb存储引擎的体系架构,可简单划分成三层: 数据文件 :磁盘上的数据文件 内存池:缓存磁盘上的数据,方便读取,同时在对磁盘文件数据修改之前在这里缓存,然后按一定规刷新 ...
- jquery,attr,prop,checkbox标签已有checked=checked但是不显示勾选
最近在做项目的过程中碰到了这样的一个问题:在使用bootstrap模态框的过程中,在模态框中有一个checkbox标签,一开始是为选中的,当点击触发模态框按钮,选中chcekbox时,会显示勾选,这个 ...
- <Numerical Analysis>(by Timothy Sauer) Notes
2ed, by Timothy Sauer DEFINITION 1.3A solution is correct within p decimal places if the error is l ...