需求
最近在干一个系统dm改造,其中有一个问题,他的系统里面更新以及新增都是虚假的,听说是上一次数据库改造的时候没有处理这个问题,所以他们的处理数据库都是通过导入完成的,但是尴尬的是本地的是将系统合并到我们集团系统中,但是我们集团系统新的导出都是使用的easyexcel,旧的导出是使用的poi,然后我还看不懂他之前使用的导入,就导致尴尬的问题,旧的系统我看不懂使用,新的系统我们需要将easyexcel和poi合在一起,因为我们是使用poi将使用他们提供的模版,老大了并且很复杂,使用easyexcel的话将文件上传云端可以后台上传
源代码
源系统代码
java
package com.neusoft.np.rms.resource.importExcel;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import jxl.CellType;
import jxl.DateCell;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.neusoft.np.common.exception.ServiceException;
import com.neusoft.np.rms.collection.dwr.CollectionDwr;
import com.neusoft.np.rms.provincial.bo.ProvincialBO;
import com.neusoft.np.rms.resource.tool.ExcelAnalysisUtil;
import com.neusoft.np.rms.verify.dataservice.ServiceDAO;
import com.neusoft.np.rms.verify.global.Constants;
import com.neusoft.np.rms.verify.global.DataMap;
import com.neusoft.np.rms.verify.global.Pub;
import com.neusoft.np.util.DateUtil;
import com.neusoft.np.util.StringUtil;
public class ImportProvincialExcel {
protected Log logger = LogFactory.getLog(getClass());
private WritableCellFormat errorCellFormat = null;
private ProvincialBO provincialBO;
private CollectionDwr collectionDwr;
/**
* add by linjian@neusoft.com
*/
private ServiceDAO serviceDAO;
ArrayList<String> titleBaseAl = null;// 定义通用的标题及索引
HashMap<String, String> titleBaseDic = null;
private PrintWriter jspout;
public ProvincialBO getProvincialBO() {
return provincialBO;
}
public void setProvincialBO(ProvincialBO provincialBO) {
this.provincialBO = provincialBO;
}
public CollectionDwr getCollectionDwr() {
return collectionDwr;
}
public void setCollectionDwr(CollectionDwr collectionDwr) {
this.collectionDwr = collectionDwr;
}
/**
* 初始化表头
*/
private void initTitle() {
titleBaseAl = new ArrayList<String>();
titleBaseAl.add("ID");// id
titleBaseAl.add("PROV_CODE");// 所属省份ID
titleBaseAl.add("CITY_CODE");// 所属地市ID
titleBaseAl.add("ORIGIN");// 资产来源
titleBaseAl.add("ASSET_TAG_NO");// 资产标签号
titleBaseAl.add("O_ASSET_TAG_NO");// 原资产标签号
titleBaseAl.add("DEV_TYPE_ID");// 设备类型
titleBaseAl.add("DEVICE_LEVEL");// 设备级别
titleBaseAl.add("O_ENTITY_NAME");// 原实物名称
titleBaseAl.add("N_ENTITY_NAME");// 规范实物名称
titleBaseAl.add("LIFE_STATUS");// 实物状态
titleBaseAl.add("ENTITY_TYPE");// 实物分类
titleBaseAl.add("CITY_COMP_ID");// 公司编号
titleBaseAl.add("ACCOUNT_BOOK_CODE");// 资产账簿编号
titleBaseAl.add("BELONG_TO_MAJOR");// 资产归口管理专业线
//titleBaseAl.add("MAINTAIN_MAJOR");// 维护专业线
titleBaseAl.add("BELONG_TO_DEPARTMENT");// 专业归口管理部门
titleBaseAl.add("A_MAJOR_MANAGER_ID");// 资产专业管理责任人编号
titleBaseAl.add("A_MAJOR_MANAGER_NAME");// 资产专业管理责任人姓名
titleBaseAl.add("TOTAL_COST_MAJOR");// 全成本专业线
titleBaseAl.add("INDEPENDENT_SIGNS");// 独立实物标识
titleBaseAl.add("F_ASSET_TAG_NO");// 父资产标签号
titleBaseAl.add("APP_AREA");// 应用领域
titleBaseAl.add("ASSET_CODE_DIRECTORY");// 资产目录编码
titleBaseAl.add("ACCOUNTING_MAJOR");// 资产核算专业
titleBaseAl.add("ASSET_KEYWORD");// 资产关键字
titleBaseAl.add("KEYWORD_CODE");// 资产关键字编号
titleBaseAl.add("SYSTEM_PEOFESSION_TYPE");// 系统专业类型
titleBaseAl.add("PROVIDER_NAME");// 供应商名称
titleBaseAl.add("ORIG_FACTORY_NAME");// 生产厂商名称
titleBaseAl.add("O_STANDARD_MODEL");// 原规格型号
titleBaseAl.add("STANDARD_MODEL");// 规格型号
titleBaseAl.add("SERIAL_NUMBER");// 序列号
titleBaseAl.add("ASSET_COUNT");// 资产数量
titleBaseAl.add("ASSET_COUNT_UNIT");// 资产计量单位
titleBaseAl.add("AUXILIARY_COUNT");// 辅助数量
titleBaseAl.add("AUXILIARY_COUNT_UNIT");// 辅助计量单位
titleBaseAl.add("BUY_DATE");// 购买日期
titleBaseAl.add("START_USING_DATE");// 启用日期
titleBaseAl.add("USER_AGR_LIMIT");// 使用月数
titleBaseAl.add("WARRANTY_S_DATE");// 质保开始日期
titleBaseAl.add("WARRANTY_E_DATE");// 质保截止日期
titleBaseAl.add("PROJECT_CODE");// 项目编号
titleBaseAl.add("PROJECT_NAME");// 项目名称
titleBaseAl.add("PROJECT_SORT");// 项目分类
titleBaseAl.add("IF_EXPAND");// 是否扩容
titleBaseAl.add("DEPARTMENT_CODE");// 责任部门编码
titleBaseAl.add("DEPARTMENT_NAME");// 责任部门名称
titleBaseAl.add("BLAMER_ID");// 责任人员工编号
titleBaseAl.add("BLAMER_NAME");// 责任人姓名
titleBaseAl.add("BLAMER_EMAIL");// 责任人邮箱地址
titleBaseAl.add("MANAGER_ID");// 保管人员工编号
titleBaseAl.add("MANAGER_NAME");// 保管人姓名
titleBaseAl.add("MANAGER_EMAIL");// 保管人邮箱地址
titleBaseAl.add("MANAGER_BELONG_COM");// 保管人员所属单位
titleBaseAl.add("FA_ROOM_NAME");// FA地点编码
titleBaseAl.add("FA_DESCRIBE");// FA地点描述
titleBaseAl.add("STANDARD_ADDRESS");// 标准地址
titleBaseAl.add("ASSET_ORIN_VALUE");// 资产原值
titleBaseAl.add("ASSET_NET_AMOUNT");// 资产净额
titleBaseAl.add("MAINTAIN_WAY");// 维护方式
titleBaseAl.add("MAINTAIN_NAME");// 维护部门
titleBaseAl.add("MAINTAINER_NO");// 维护责任人员工编号
titleBaseAl.add("MAINTAINER_NAME");// 维护责任人员工姓名
titleBaseAl.add("MAINTAINER_EMAIL");// 维护人邮箱地址
titleBaseAl.add("IF_MAINTENANCE");// 是否有维保
titleBaseAl.add("ASSETS_BELONG_TO");// 资产归属
titleBaseAl.add("MAIN_CONF_DESC");// 主要配置描述
titleBaseAl.add("BELONG_TO_SYS");// 所属系统
titleBaseAl.add("ON_LINE_DATE");// 上线时间
titleBaseAl.add("OUT_LINE_DATE");// 下线时间
titleBaseAl.add("FIXED_POWER");// 额定功率千瓦)
titleBaseAl.add("SYS_HOST_NAME");// 对应的网管系统中的主机名称
titleBaseAl.add("D_BELONG_ROOM_PROPERTY");// 设备所在机房属性
titleBaseAl.add("START_U");// 起始U
titleBaseAl.add("INFR_HEIGHT");// 高度
titleBaseAl.add("OS_TYPE_VERSION");// OS类型及版本
titleBaseAl.add("PATCH_INFO");// 补丁信息
titleBaseAl.add("WIRELESS_MAC_ADDRESS");// 无线mac地址
titleBaseAl.add("ETHERNET_MAC_ADDRESS");// 有线mac地址
titleBaseAl.add("HOST_NAME");// 设备hostname
titleBaseAl.add("DEVICE_OID");// 设备OID
titleBaseAl.add("DEVICE_DESCRIPTION");// 设备Description
titleBaseAl.add("IP_INFO");// IP信息
titleBaseAl.add("UP_LINK_NET_DEV");// 上联网络设备
titleBaseAl.add("DEVICE_LAYER_LEVEL");// 设备层级
titleBaseAl.add("CARBINET_TYPE");// 机架类型
titleBaseAl.add("SUB_CARBINET_TYPE");// 子架类型
titleBaseAl.add("SUB_CARBINET_MOUNT");// 子架数量
titleBaseAl.add("CARBINET_S_D_TYPE");// 机架单双面类型
titleBaseAl.add("ENTITY_DESCRIPE");// 实物描述
titleBaseAl.add("TERMINAL_FUNC");// 终端用途
titleBaseAl.add("ADD_ASSET_SIGN_S");// 增资标识(北京公司专用)
titleBaseAl.add("REUSE_SIGN_S");// 可再利用标识(北京公司专用)
titleBaseAl.add("OUT_POWER_SIFN_S");// 下电标识(北京公司专用)
titleBaseAl.add("O_PRO_NO_NAME_S");// 原工程编号及名称(北京公司专用)
titleBaseAl.add("ACCEPTOR");// 验收人(北京公司专用)
titleBaseAl.add("ATT_FACILITY_AFFIX_S");// 附属设施及附件(北京公司专用)
titleBaseAl.add("REMAK_S");// 备注(北京公司专用)
titleBaseAl.add("DEVICE_STAYUS");// 设备状态(北京公司专用)
titleBaseAl.add("MAINTAIN_CASE");// 交维情况
titleBaseAl.add("ROOM_NAME");// 机房名称
titleBaseAl.add("CARBINET_CODE");// 机架行列号
titleBaseAl.add("REMAK1");// 备注1
titleBaseAl.add("REMAK2");// 备注2
titleBaseAl.add("REMAK3");// 备注3
titleBaseAl.add("REMAK4");// 备注4
titleBaseAl.add("REMAK5");// 备注5
titleBaseAl.add("SYS_LIFE_STATUS");// 设备生命周期
titleBaseAl.add("DATA_SOURCE_TYPE");// 数据来源分类
}
public boolean importProvincial(File sourceFile, File errorFile, PrintWriter out, String logon_account,
String region_id, String errPath) {
boolean ret = true;// 默认是成功的,如果失败一次就付值
boolean oneRet = false;
this.jspout = out;
initTitle();
// initDic();
errorCellFormat = new WritableCellFormat();
try {
errorCellFormat.setBackground(Colour.RED);
} catch (WriteException e) {
e.printStackTrace();
}
// 创建只读的Excel工作薄的对象
Workbook readBook;
// 创建可写入的Excel工作薄对象
WritableWorkbook writeBook;
try {
readBook = Workbook.getWorkbook(sourceFile);
writeBook = Workbook.createWorkbook(errorFile, readBook);
} catch (Exception e1) {
throw new ServiceException("打开文档失败:" + e1.getMessage());
}
// 读取工作表
WritableSheet sheet = null;
try {
sheet = writeBook.getSheet(0);
importData(sourceFile, sheet, logon_account, region_id, errPath);
// ret = (oneRet == false ? oneRet : ret);//
// 如果失败过一次,就认为整体失败,如果成功,仍记录原来的状态
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException("导入失败:" + e.getMessage());
} finally {
try {
// 写入Excel对象
// if (writeBook != null)
// writeBook.write();
// 关闭可写入的Excel对象
if (writeBook != null)
writeBook.close();
// 关闭只读的Excel对象
if (readBook != null)
readBook.close();
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException("导入失败:" + e.getMessage());
}
}
return ret;
}
/**
* 导入一个sheet页的文件
*
* @param sheet
* @return
*/
private void importData(File file, WritableSheet sheet, String logon_account, String region_id, String errPath) {
// int errorCount = 0;
// int rightCount = 0;
boolean isSave = true;
WritableCell tempCell = null;
String tempContent = null;
// List ADDRESS = null;
// List<HashMap> dataList = new ArrayList<HashMap>();
// List<Map> city_comp = collectionDwr.getCityCompInfo(region_id);
// String comp_number = collectionDwr.listToString(city_comp);
// 取得所有行数
int rows = ExcelAnalysisUtil.getRightRows(sheet);
String sheetName = sheet.getName();
// Map paramMap = new HashMap();
// paramMap.put("REGION_ID", region_id);
// Map prov_city_Map = collectionDwr.getProvCityInfo(paramMap);
// String prov_code = "";
// String city_code = "";
// if (prov_city_Map != null) {
// prov_code = (String) prov_city_Map.get("PARENT_ID");// 省分ID
// city_code = (String) prov_city_Map.get("REGION_ID");// 地市ID
// } else {
// prov_code = "";// 省分ID
// city_code = "";// 地市ID
// }
try {
jspout.println("<br>[" + sheetName + "]开始导入.........................<br>");
jspout.flush();
// ------------初始化记录
/*******************************************************************
* add by linjian@neusoft.com
******************************************************************/
Pub.init(logon_account, Constants.RES_KEY.IT.name(), jspout, rows - 11, errPath);
logger
.info("用户:" + logon_account + ",正在上传,上传总资产条数:" + (rows - 11) + ",资产类型:"
+ Constants.RES_KEY.IT.name());
for (int i = 11; i < rows; i++) {// 遍历所有行
DataMap<String, String> dataMap = new DataMap<String, String>();
// dataMap.put("PROV_CODE", prov_code);
// dataMap.put("CITY_CODE", city_code);
// HashMap<String, Object> dataMap = new HashMap<String,
// Object>();
// HashMap<String, String> dataMap = new HashMap<String,
// String>();
// dataMap.put("PROV_CODE", prov_code);
// dataMap.put("CITY_CODE", city_code);
// StringBuffer errorStr = new StringBuffer();
// String CHECK_INDEPENDENT = "";//独立实物标识条件验证父资产标签号
// String CHECK_LIFE_STATUS = "";//实物状态条件验证序列号
// String CHECK_SERIAL_NUMBER = "";//序列号
// String CHECK_DEV_TYPE = "";//设备类型验证对应的网管系统中的主机名称
for (int j = 1; j < titleBaseAl.size(); j++) {// 遍历列
tempCell = sheet.getWritableCell(j, i);
// tempContent =
// this.QBchange(tempCell.getContents()).trim();
tempContent = tempCell.getContents() == null ? "" : tempCell.getContents().trim();
if (tempCell.getType() == CellType.DATE) {// 如果是日期类型,
tempContent = this.FormateData(tempCell);// 需转化格式
}
// if (j == 56) {
// System.out.println(tempContent);
// }
// int index = tempContent.indexOf("笀");
// if (index >= 0) {
// tempContent = tempContent.replaceAll("笀", "寿");
// }
// index = tempContent.indexOf("鸀");
// if (index >= 0) {
// tempContent = tempContent.replaceAll("鸀", "绿");
// }
dataMap.put(titleBaseAl.get(j), tempContent);
dataMap.put("LAST_UPDATE_PERSON", logon_account);//记录最后修改人
}
/**
* add by linjian 查询盘亏数据和认领人信息
*/
String asset_tag_no = StringUtil.changeNull(dataMap.get("ASSET_TAG_NO"));
if (!asset_tag_no.equalsIgnoreCase("")) {
DataMap<String, String> queryMap = new DataMap<String, String>();
queryMap.put("tables", Constants.TABLES.IT.value);
queryMap.put("asset_tag_no", asset_tag_no);
//盘亏查询
String status = StringUtil.changeNull(serviceDAO.query("VERIFY.getLostAsset", queryMap));
if ("1".equalsIgnoreCase(status)) {
Pub.computeLoseAsset(logon_account, Constants.RES_KEY.IT.name());
continue;
}
}
/**
* add by yanbb 总部用户需要判断上传资产是否为本人资产
*/
String city_comp_id = StringUtil.changeNull(dataMap.get("CITY_COMP_ID"));
if (!city_comp_id.equalsIgnoreCase("")) {
DataMap<String, String> queryMap = new DataMap<String, String>();
queryMap.put("tables", Constants.TABLES.IT.value);
queryMap.put("asset_tag_no", asset_tag_no);
if("111001".equalsIgnoreCase(city_comp_id) || "121001".equalsIgnoreCase(city_comp_id)){
//查询是否本人资产本人账号上传
String claimperson = StringUtil.changeNull(serviceDAO.query("VERIFY.getClaimPerson", queryMap));
if (!"".equalsIgnoreCase(claimperson) && !logon_account.equalsIgnoreCase(claimperson)) {
Pub.computeOneseIfAsset(logon_account, Constants.RES_KEY.IT.name());
continue;
}
}
}
/**
* add by linjian 查询盘亏数据
*/
dataMap.put(Constants.TAG_ASSET_TYPE, Constants.RES_KEY.IT.name());
dataMap.put(Constants.TAG_USER_ACCOUNT, logon_account);
dataMap.put(Constants.TAG_ROW_ID, String.valueOf(i + 1));
dataMap.put(Constants.TAG_REGION, region_id);
dataMap.put(Constants.TAG_UNIQUE_ID, dataMap.get("ASSET_TAG_NO"));
Pub._analyseQueue.add(dataMap);
}
Pub._syncStatisticsMap.put(logon_account + "_" + Constants.RES_KEY.IT.name() + "_" + Constants.SUFFIX_ISOK,
3);
jspout.println("<br>[" + sheetName + "]解析完成,正在处理........................<br>");
jspout.flush();
} catch (Exception e) {
throw new ServiceException("解析用户:【" + logon_account + "】,资产类型:【" + Constants.RES_KEY.IT.name()
+ "】上报记录模板失败", e);
}
// return isSave;
}
// 处理日期格式数据
public String FormateData(WritableCell cell) {
try {
if (cell.getType() == CellType.DATE) {
Date mydate = null;
DateCell datecll = (DateCell) cell;
mydate = datecll.getDate();
long time = (mydate.getTime() / 1000) - 60 * 60 * 8;
mydate.setTime(time * 1000);
return DateUtil.dateToString(mydate, null);
}
} catch (Exception e) {
return null;
}
return null;
}
// public String dataCheck(String field,int j){
// String result = "";
// if("ORIGIN".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产来源不能为空; ";
// }
// }
// else if("FA_ID".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "FA资产标识不能为空; ";
// }
// if(!field.equals("是")&&!field.equals("否")){
// return "FA资产标识只能填写是与否; ";
// }
// }
// else if("ASSET_TAG_NO".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产标签号不能为空; ";
// }
// }
// else if("DEV_TYPE_ID".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "设备类型不能为空; ";
// }
// }
// else if("O_ENTITY_NAME".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "原实物名称不能为空; ";
// }
// }
// else if("N_ENTITY_NAME".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "规范实物名称不能为空; ";
// }
// }
// else if("LIFE_STATUS".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "实物状态不能为空; ";
// }
// }
// else if("ENTITY_TYPE".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "实物分类不能为空; ";
// }
// }
// else if("CITY_COMP_ID".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "公司编号不能为空; ";
// }
// }
// else if("ACCOUNT_BOOK_CODE".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产账簿编号不能为空; ";
// }
// }
// else if("BELONG_TO_MAJOR".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "归口管理专业线不能为空; ";
// }
// }
// else if("BELONG_TO_DEPARTMENT".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "专业归口管理部门不能为空; ";
// }
// }
// else if("A_MAJOR_MANAGER_ID".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产专业管理责任人编号不能为空; ";
// }
// }
// else if("A_MAJOR_MANAGER_NAME".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产专业管理责任人姓名不能为空; ";
// }
// }
// else if("TOTAL_COST_MAJOR".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "全成本专业线不能为空; ";
// }
// }
// else if("INDEPENDENT_SIGNS".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "独立实物标识不能为空; ";
// }
// }
// else if("APP_AREA".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "应用领域不能为空; ";
// }
// }
// else if("ASSET_CODE_DIRECTORY".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产目录编码不能为空; ";
// }
// }
// else if("ACCOUNTING_MAJOR".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产核算专业不能为空; ";
// }
// }
// else if("ASSET_KEYWORD".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产关键字描述不能为空; ";
// }
// }
// else if("KEYWORD_CODE".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产关键字不能为空; ";
// }
// }
// else if("ORIG_FACTORY_NAME".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "生产厂商名称不能为空; ";
// }
// }
// else if("STANDARD_MODEL".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "规格型号不能为空; ";
// }
// }
// else if("ASSET_COUNT".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产数量不能为空; ";
// }
// if(!regexNum(field)){
// return "资产数量只能填写数字; ";
// }
// }
// else if("ASSET_COUNT_UNIT".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产计量单位不能为空; ";
// }
// }
// else if("START_USING_DATE".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "启用日期不能为空; ";
// }
// }
// else if("USER_AGR_LIMIT".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "使用月数不能为空; ";
// }
// if(!regexNum(field)){
// return "使用月数只能填写数字; ";
// }
// }
// else if("START_USING_DATE".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "启用日期不能为空; ";
// }
// }
// else if("IF_EXPAND".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "是否扩容不能为空; ";
// }
// }
// else if("DEPARTMENT_CODE".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "责任部门编码不能为空; ";
// }
// }
// else if("DEPARTMENT_NAME".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "责任部门名称不能为空; ";
// }
// }
// else if("BLAMER_ID".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "责任人员工编号不能为空; ";
// }
// }
// else if("BLAMER_NAME".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "责任人姓名不能为空; ";
// }
// }
// else if("MANAGER_NAME".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "保管人姓名不能为空; ";
// }
// }
// else if("ASSET_ORIN_VALUE".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产原值不能为空; ";
// }
// if(!regexNum(field)){
// return "资产原值只能填写数字; ";
// }
// }
// else if("ASSET_NET_AMOUNT".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "资产净额不能为空; ";
// }
// if(!regexNum(field)){
// return "资产净额只能填写数字; ";
// }
// }
// else if("MAINTAIN_WAY".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "维护方式不能为空; ";
// }
// }
// else if("MAINTAIN_NAME".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "维护部门不能为空; ";
// }
// }
// else if("MAINTAINER_NO".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "维护责任人员工编号不能为空; ";
// }
// }
// else if("MAINTAINER_NAME".equals(titleBaseAl.get(j))){
// if(!isNullStr(field)){
// return "维护责任人员工姓名不能为空; ";
// }
// }
// return result;
// }
public boolean isNullStr(String str) {
if (str != null && str.length() > 0) {
return true;
} else {
return false;
}
}
/**
* 判断字符串是否为数字组成 param:string return:(boolean类型)true/false;
*/
public boolean regexNum(String str) {
java.util.regex.Pattern p = null; // 正则表达式
return p.matches("-?[0-9]*.?[0-9]*", str);
}
private void changeContent(WritableCell cell, String newContent) {
// 判断单元格的类型, 做出相应的转化
if (cell.getType() == CellType.LABEL) {
((Label) cell).setString(newContent);
}
// 设为红色
cell.setCellFormat(errorCellFormat);
}
// 全角转半角
public static final String QBchange(String QJstr) {
String outStr = "";
String Tstr = "";
byte[] b = null;
for (int i = 0; i < QJstr.length(); i++) {
try {
Tstr = QJstr.substring(i, i + 1);
b = Tstr.getBytes("unicode");
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
if (b[3] == -1) {
b[2] = (byte) (b[2] + 32);
b[3] = 0;
try {
outStr = outStr + new String(b, "unicode");
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
} else
outStr = outStr + Tstr;
}
return outStr;
}
/**
* add by linjian@neusoft.com
*/
public ServiceDAO getServiceDAO() {
return serviceDAO;
}
public void setServiceDAO(ServiceDAO serviceDAO) {
this.serviceDAO = serviceDAO;
}
/**
* add by linjian@neusoft.com
*/
}
这大概就是之前的代码,其中的initTitle方法,就是通过initTitle方法中的存储顺序来进行处理导入的顺序问题,其中他插入的逻辑在Pub._syncStatisticsMap.put这个里面,我是没看懂他是咋处理的,
java
package com.neusoft.np.rms.verify.global;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import com.neusoft.np.common.exception.ServiceException;
import com.neusoft.np.rms.verify.threadpool.VerifyThread;
import com.neusoft.np.util.StringUtil;
/**
* 全局内存结构
*
* @author linjian
*
*/
public final class Pub {
public static ThreadGroup thrGup;
// 配置规则刷新时间
public static int _regular_refresh_time = 0;
// CMDB和公司编号内存刷新时间
public static int _cmdb_refresh_time = 0;
public static int _comp_refresh_time = 0;
public static int _analyse_thread = 0;
public static int _indb_thread = 0;
public static int _err_thread = 0;
public static int _analyse_queue_size = 0;
public static int _indb_correct_queue_size = 0;
public static int _indb_error_queue_size = 0;
public static int _db_commit = 0;
/**
* 用户一些信息存在该Map,在并发的时候需要提取一些用户数据 可以的组织形式 用户名称_资产类型_以下后缀 例如:
* linjian@neusoft.com_COMMON_FILENAME 错误文件名称,例如linjian@neusoft.com_error
* linjian@neusoft.com_COMMON_TOTAL 50000 总的上传记录条数
* linjian@neusoft.com_COMMON_SUCCESS 10000 合法资产条数
* linjian@neusoft.com_COMMON_ERROR 40000 非法资产条数
* linjian@neusoft.com_COMMON_LOST 11 盘亏数据
* linjian@neusoft.com_COMMON_COMPUTE 已经处理的错误记录
* linjian@neusoft.com_COMMON_ISOK OK 处理完成 | NO 还未完成
*/
public static ConcurrentHashMap<String, Integer> _syncStatisticsMap = new ConcurrentHashMap<String, Integer>();
/**
* linjian@neusoft.com_IT_OUTER 该用户上传文件时创建的printwriter 对象,用户向页面输出错误结果
*/
public static ConcurrentHashMap<String, PrintWriter> _syncOuterMap = new ConcurrentHashMap<String, PrintWriter>();
/**
* 每个用户上报文件中的错误记录,需要生成到xls文件的句柄
*/
public static ConcurrentHashMap<String, Object> _syncXlsMap = new ConcurrentHashMap<String, Object>();
/**
* 校验分析队列,解析模板后将单个数据对象放入校验分析队列中
*/
public static ArrayBlockingQueue<DataMap<String, String>> _analyseQueue = null;
/**
* 校验通过的正确数据放入此队列入库
*/
public static ArrayBlockingQueue<DataMap<String, String>> _indb_correctQueue = null;
/**
* 校验未通过的数据放入次队列中
*/
public static ArrayBlockingQueue<DataMap<String, String>> _indb_errorQueue = null;
/**
* 所有类型资产校验规则
*/
public static ConcurrentHashMap<String, List<DataMap<String, String>>> _regular = new ConcurrentHashMap<String, List<DataMap<String, String>>>();
/**
* 数据库中cmdb表的所有枚举值,不包括人员枚举、地址枚举和部门枚举
*/
public static ConcurrentHashMap<String, List<DataMap<String, String>>> _cmdb = new ConcurrentHashMap<String, List<DataMap<String, String>>>();
/**
* 所有公司编号与区域之前的对照关系字典,key=region_id+comp_id value=comp_id
*/
public static ConcurrentHashMap<String, String> _comp = new ConcurrentHashMap<String, String>();
/**
* IT设备中"设备类型条件必填明细规范要求"规则
*/
public static ConcurrentHashMap<String, List<DataMap<String, String>>> _device_regular = new ConcurrentHashMap<String, List<DataMap<String, String>>>();
/**
* 通用软件Liscense类型映射规则
*/
public static ConcurrentHashMap<String, DataMap<String, String>> _common_regular = new ConcurrentHashMap<String,DataMap<String, String>>();
/**
* 初始化CMDB内存结构
*
* @param dataobj
* 从ibatis中查询的未整理的数据结构
*/
public static void initializeCmdb(List<DataMap<String, String>> dataobj) {
String oldKey = null;
List<DataMap<String, String>> tempList = null;
for (DataMap<String, String> map : dataobj) {
String key = map.get(Constants.KEY_CMDB);
if (!key.equalsIgnoreCase(oldKey)) {
oldKey = key;
tempList = null;
tempList = new ArrayList<DataMap<String, String>>();
tempList.add(map);
_cmdb.put(oldKey, tempList);
} else {
tempList.add(map);
}
}
}
/**
* 初始化校验规则内存结构
*
* @param dataobj
* 从ibatis中查询的未整理的数据结构
*/
public static void initializeRegular(List<DataMap<String, String>> dataobj) {
String oldKey = null;
List<DataMap<String, String>> tempList = null;
for (DataMap<String, String> map : dataobj) {
String key = map.get(Constants.KEY_REGULAR);
if (!key.equalsIgnoreCase(oldKey)) {
oldKey = key;
tempList = null;
tempList = new ArrayList<DataMap<String, String>>();
tempList.add(map);
_regular.put(oldKey, tempList);
} else {
tempList.add(map);
}
}
}
/**
* 初始化公司编号字典表
*
* @param dataObj
* 从Ibatis中提取未经过整理的字典表
*/
public static void initializeComp(List<DataMap<String, String>> dataObj) {
if (dataObj == null || dataObj.isEmpty())
return;
for (DataMap<String, String> one : dataObj) {
_comp.put(one.get("keysstr"), one.get("p_code"));
}
dataObj.clear();
dataObj = null;
}
/**
* 初始化"设备类型条件必填明细规范要求"规则
*
* @param dataObj
* 从Ibatis中提取未经过整理的字典表
*/
public static void initializeDeviceRegular(List<DataMap<String, String>> dataobj) {
String oldKey = null;
List<DataMap<String, String>> tempList = null;
for (DataMap<String, String> map : dataobj) {
String key = map.get(Constants.DEVICE_SORT_KEY);
if (!key.equalsIgnoreCase(oldKey)) {
oldKey = key;
tempList = null;
tempList = new ArrayList<DataMap<String, String>>();
tempList.add(map);
_device_regular.put(oldKey, tempList);
} else {
tempList.add(map);
}
}
}
/**
* 初始化通用软件Liscense类型映射规则
*
* @param dataObj
* 从Ibatis中提取未经过整理的字典表
*/
public static void initializeLiscenseTypeRegular(List<DataMap<String, String>> dataobj) {
if(dataobj == null)
return;
for(DataMap<String, String> one : dataobj){
_common_regular.put(one.get(Constants.DEVICE_SORT_KEY), one);
}
}
/**
* 通用软件使用
* @param sort_key
* @return
*/
public static String getNullableBySortKey(final String sort_key){
if(sort_key == null || sort_key.equalsIgnoreCase(""))
return null;
DataMap<String,String> regular = _common_regular.get(sort_key);
if(regular == null)
return null;
return regular.get("nullable");
}
/**
* 通用软件使用
* @param sort_key
* @param type
* @return
*/
public static String getLiscenseType(final String sort_key){
if(sort_key == null || sort_key.equalsIgnoreCase(""))
return null;
DataMap<String,String> regular = _common_regular.get(sort_key);
if(regular == null)
return null;
return regular.get("type");
}
public static String getNullableBySortKey(final String sort_key,final String sub_sort_key){
if(sort_key == null || sort_key.equalsIgnoreCase(""))
return null;
if(sub_sort_key == null || sub_sort_key.equalsIgnoreCase(""))
return null;
if(_device_regular == null || _device_regular.size() == 0 )
return null;
List<DataMap<String,String>> regular = _device_regular.get(sort_key);
if(regular == null)
return null;
for(DataMap<String,String> one : regular){
if(one.get("sub_sort_key").equalsIgnoreCase(sub_sort_key))
return one.get("nullable");
}
return null;
}
public static String getEntityTagBySortKey(final String sort_key,final String sub_sort_key,final String status){
if(sort_key == null || sort_key.equalsIgnoreCase(""))
return null;
if(sub_sort_key == null || sub_sort_key.equalsIgnoreCase(""))
return null;
if(status == null || status.equalsIgnoreCase(""))
return null;
if(_device_regular == null || _device_regular.size() == 0 )
return null;
List<DataMap<String,String>> regular = _device_regular.get(sort_key);
if(regular == null)
return null;
for(DataMap<String,String> one : regular){
if(!one.get("sub_sort_key").equalsIgnoreCase(sub_sort_key))
continue;
return one.get("status_"+status);
}
return null;
}
/**
* 根据res_key获取规则
*
* @param resKey
* 资产类型关键字
* @return 符合该资产类型的校验规则
*/
public static List<DataMap<String, String>> getRegular(final String resKey) {
if (resKey == null || resKey.equalsIgnoreCase(""))
return null;
if (_regular.get(resKey) == null || _regular.get(resKey).size() == 0)
return null;
return _regular.get(resKey);
}
/**
* 从cmdb数据字典内存中获取
*
* @param name
* MANAGE_NAME所对应的值
* @param sort
* 分类字段,表示哪一类枚举
* @return name 所对应的值
*/
public static String getValueByName(final String name, final String sort) {
if (sort == null || sort.equalsIgnoreCase(""))
return null;
if (name == null || name.equalsIgnoreCase(""))
return null;
if (_cmdb == null || _cmdb.size() == 0)
return null;
List<DataMap<String, String>> oneData = _cmdb.get(sort);
if(oneData == null)
return null;
for (DataMap<String, String> oneMap : oneData) {
if (oneMap.get(Constants.MANAGE_NAME).equalsIgnoreCase(name))
return oneMap.get(Constants.MANAGE_VALUE);
}
return null;
}
/**
* 从cmdb数据字典内存中获取
*
* @param name
* MANAGE_NAME所对应的值
* @param sort
* 分类字段,表示哪一类枚举
* @return name 所对应的值
*/
public static String getNameByValue(final String value, final String sort) {
if (sort == null || sort.equalsIgnoreCase(""))
return null;
if (value == null || value.equalsIgnoreCase(""))
return null;
if (_cmdb == null || _cmdb.size() == 0)
return null;
List<DataMap<String, String>> oneData = _cmdb.get(sort);
if(oneData == null)
return null;
for (DataMap<String, String> oneMap : oneData) {
if (oneMap.get(Constants.MANAGE_VALUE).equalsIgnoreCase(value))
return oneMap.get(Constants.MANAGE_NAME);
}
return null;
}
/**
* 重载方法,当同一个枚举区分两种数据种类,例如区分集团和省份的数据,在校验的时候需要根据不同的标识到不同的数据中进行获取值
*
* @param name
* Manage_name所对应的值
* @param sort
* 数据字典分类对应 manage_sort
* @param key
* 数据区分标识
* @param sep_value
* 特殊值,当sep_value=null字符串的时候,按照Key值映射last_manage_value,从Key分类出的Manage_value和Manage_name中取值
* 当sep_value!=null字符串的时候,当Key=sep_value的时候,从last_manage_value中过滤sep_value分类的Manage_valu和Manage_name,进行取值,否则从非sep_value对应的Manage_valu和Manage_name中取值;
* @return 获取到的值
*/
public static String getValueByName(final String name, final String sort,
final String key, final String sep_value) {
if (sort == null || sort.equalsIgnoreCase(""))
return null;
if (name == null || name.equalsIgnoreCase(""))
return null;
if (_cmdb == null || _cmdb.size() == 0)
return null;
List<DataMap<String, String>> oneData = _cmdb.get(sort);
if(oneData == null)
return null;
if (StringUtil.changeNull(sep_value).equalsIgnoreCase("null")
|| StringUtil.changeNull(sep_value).equalsIgnoreCase("")) {
//boolean bFind = false;
for (DataMap<String, String> oneMap : oneData) {
if(!oneMap.get(Constants.LAST_MANAGE_VALUE).equalsIgnoreCase(key))
continue;
//bFind = true;
if (oneMap.get(Constants.MANAGE_NAME).equalsIgnoreCase(name))
return oneMap.get(Constants.MANAGE_VALUE);
}
// if(bFind )
// return null;
// return "NOKEY";
} else {
for (DataMap<String, String> oneMap : oneData) {
if (key.equalsIgnoreCase(sep_value)) {
if (!oneMap.get(Constants.LAST_MANAGE_VALUE)
.equalsIgnoreCase(sep_value))
continue;
if (oneMap.get(Constants.MANAGE_NAME).equalsIgnoreCase(name))
return oneMap.get(Constants.MANAGE_VALUE);
} else {
if (oneMap.get(Constants.LAST_MANAGE_VALUE).equalsIgnoreCase(sep_value))
continue;
if (oneMap.get(Constants.MANAGE_NAME).equalsIgnoreCase(name))
return oneMap.get(Constants.MANAGE_VALUE);
}
}
}
return null;
}
/**
* 同上
*/
public static String getNameByValue(final String value, final String sort,
final String key, final String sep_value) {
if (sort == null || sort.equalsIgnoreCase(""))
return null;
if (value == null || value.equalsIgnoreCase(""))
return null;
if (_cmdb == null || _cmdb.size() == 0)
return null;
List<DataMap<String, String>> oneData = _cmdb.get(sort);
if(oneData == null)
return null;
if (StringUtil.changeNull(sep_value).equalsIgnoreCase("null")
|| StringUtil.changeNull(sep_value).equalsIgnoreCase("")) {
// boolean bFind = false;
for (DataMap<String, String> oneMap : oneData) {
if(!oneMap.get(Constants.LAST_MANAGE_VALUE).equalsIgnoreCase(key))
continue;
//bFind = true;
if (oneMap.get(Constants.MANAGE_VALUE).equalsIgnoreCase(value))
return oneMap.get(Constants.MANAGE_NAME);
}
// if(bFind)
// return null;
// return "NOKEY";
}
else{
for (DataMap<String, String> oneMap : oneData) {
if (key.equalsIgnoreCase(sep_value)) {
if (!oneMap.get(Constants.LAST_MANAGE_VALUE)
.equalsIgnoreCase(sep_value))
continue;
if (oneMap.get(Constants.MANAGE_VALUE).equalsIgnoreCase(value))
return oneMap.get(Constants.MANAGE_NAME);
} else {
if (oneMap.get(Constants.LAST_MANAGE_VALUE).equalsIgnoreCase(sep_value))
continue;
if (oneMap.get(Constants.MANAGE_VALUE).equalsIgnoreCase(value))
return oneMap.get(Constants.MANAGE_NAME);
}
}
}
return null;
}
/**
* 资源释放
*/
public static void destory() {
stopAllThread();
if (_analyseQueue != null) {
_analyseQueue.clear();
_analyseQueue = null;
}
if (_indb_correctQueue != null) {
_indb_correctQueue.clear();
_indb_correctQueue = null;
}
if (_indb_errorQueue != null) {
_indb_errorQueue.clear();
_indb_errorQueue = null;
}
if (_regular != null && !_regular.isEmpty())
_regular.clear();
if (_cmdb != null && !_cmdb.isEmpty())
_cmdb.clear();
if (_comp != null && !_comp.isEmpty())
_comp.clear();
if (_syncStatisticsMap != null && !_syncStatisticsMap.isEmpty())
_syncStatisticsMap.clear();
if (_syncOuterMap != null && !_syncOuterMap.isEmpty())
_syncOuterMap.clear();
if (_syncXlsMap != null && !_syncXlsMap.isEmpty())
_syncXlsMap.clear();
}
/**
* 同步计算一个用户一次上传模板导入错误的数据量
*
* @param account
* 用户账号
* @param asset_tag
* 上传资产类型标识
*/
public synchronized static void computeImportError(final String account,
final String asset_tag) {
try {
int curnum = _syncStatisticsMap.get(account + "_" + asset_tag + "_"
+ Constants.SUFFIX_ERROR);
curnum++;
_syncStatisticsMap.put(account + "_" + asset_tag + "_"
+ Constants.SUFFIX_ERROR, curnum);
} catch (Exception e) {
//System.out.println(account + "_________" + asset_tag);
throw new ServiceException("computeImportError 异常!", e);
}
}
/**
* 计算盘亏数据
*/
public synchronized static void computeLoseAsset(final String account,final String asset_tag){
try{
int loseNum = _syncStatisticsMap.get(account + "_" + asset_tag + "_"
+ Constants.SUFFIX_LOSE);
loseNum ++;
_syncStatisticsMap.put(account + "_" + asset_tag + "_"
+ Constants.SUFFIX_LOSE, loseNum);
}
catch(Exception e){
throw new ServiceException("computeLoseAsset 异常!", e);
}
}
/**
* 计算是否本人资产数据
*/
public synchronized static void computeOneseIfAsset(final String account,final String asset_tag){
try{
int oneseNum = _syncStatisticsMap.get(account + "_" + asset_tag + "_"
+ Constants.SUFFIX_ONESE);
oneseNum ++;
_syncStatisticsMap.put(account + "_" + asset_tag + "_"
+ Constants.SUFFIX_ONESE, oneseNum);
}
catch(Exception e){
throw new ServiceException("computeOneseIfAsset 异常!", e);
}
}
/**
* 同步计算一个用户一次上传模板导入 成功的数量
*
* @param account
* 用户账号
* @param asset_tag
* 上传资产类型标识
*/
public synchronized static void computeSuccess(final String account,
final String asset_tag) {
int curnum = _syncStatisticsMap.get(account + "_" + asset_tag + "_"
+ Constants.SUFFIX_SUCCESS);
curnum++;
_syncStatisticsMap.put(account + "_" + asset_tag + "_"
+ Constants.SUFFIX_SUCCESS, curnum);
}
/**
* 计算已经处理完成的错误数量
*
* @param key
* account+"_"+asset_tag 用户账号与资产类型的组合字段
* @return 是否已经处理完成全部的错误,针对一个用户一种资产上传来说。
* @throws ServiceException
*/
private synchronized static boolean computeDisposeError(final String key)
throws ServiceException {
String disposErrKey = key + "_" + Constants.SUFFIX_COMPUTE;
String successKey = key + "_" + Constants.SUFFIX_SUCCESS;
String total = key + "_" + Constants.SUFFIX_TOTAL;
String loseKey = key+"_" + Constants.SUFFIX_LOSE;
String oneseKey = key+"_" + Constants.SUFFIX_ONESE;
// String isOk = key + Constants.SUFFIX_ISOK;
if (!_syncStatisticsMap.containsKey(disposErrKey))
throw new ServiceException("未找到key[" + disposErrKey
+ "]在syncStatisticsMap中对应的值");
int number = _syncStatisticsMap.get(disposErrKey);
number++;
_syncStatisticsMap.put(disposErrKey, number);
int totalError = _syncStatisticsMap.get(disposErrKey);
// return number == total ? true: false;
// System.out.println("已经处理的错误数:" + totalError + ";;;;成功资产数:"
// + _syncStatisticsMap.get(successKey) + ";;;;;总资产数:"
// + _syncStatisticsMap.get(total) + ";;;;;总错误数:"
// + _syncStatisticsMap.get(key + "_" + Constants.SUFFIX_ERROR)
// + ";;;;;;;状态:"
// + _syncStatisticsMap.get(key + "_" + Constants.SUFFIX_ISOK)
// + ";;;;;目前处理过的总资产:"
// + (totalError + _syncStatisticsMap.get(successKey)));
return (totalError + _syncStatisticsMap.get(successKey)+_syncStatisticsMap.get(loseKey)+_syncStatisticsMap.get(oneseKey)) == _syncStatisticsMap
.get(total)
&& _syncStatisticsMap.get(key + "_" + Constants.SUFFIX_ISOK) == 3 ? true
: false;
}
/**
* 停止所有的线程
*/
public static void stopAllThread() {
if (thrGup == null)
return;
Thread[] tlist = new Thread[thrGup.activeCount()];
thrGup.enumerate(tlist);
if (tlist.length > 0) {
for (int i = 0; i < tlist.length; i++) {
if (tlist[i] instanceof VerifyThread) {
((VerifyThread) tlist[i]).interruptFlag();
} else {
tlist[i].interrupt();
}
}
}
}
/**
* 写文件
*
* @param account
* @param assetTag
* @param writeData
*/
public synchronized static boolean write(final String account,
final String assetTag, final DataMap<String, String> writeData) {
FileOutputStream fos = null;
HSSFSheet sheet = null;
HSSFWorkbook wb = null;
try {
if (!_syncXlsMap.containsKey(account + "_" + assetTag + "_os")
|| !_syncXlsMap.containsKey(account + "_" + assetTag
+ "_wb")) {
// _syncStatisticsMap.put(account+"_"+assetTag +
// Constants.SUFFIX_ISOK,2);
if (computeDisposeError(account + "_" + assetTag)) {
_syncStatisticsMap.put(account + "_" + assetTag + "_"
+ Constants.SUFFIX_ISOK, 1);
}
return true;
}
sheet = (HSSFSheet) _syncXlsMap.get(account + "_" + assetTag
+ "_sheet");
int rownum = _syncStatisticsMap.get(account + "_" + assetTag + "_"
+ Constants.SUFFIX_COMPUTE);
Row row = sheet.createRow(rownum + 1);
// 0模板行号
Cell rowCell = row.createCell(0);
rowCell.setCellValue(writeData.get(Constants.TAG_ROW_ID));
// 1 唯一标示
Cell assetCell = row.createCell(1);
assetCell.setCellValue(StringUtil.changeNull(writeData
.get(Constants.TAG_UNIQUE_ID)));
// 2 必填项
Cell nullCell = row.createCell(2);
nullCell.setCellValue(StringUtil.changeNull(writeData
.get(Constants.ERR_TYPE.NULL_ERR.value)));
// 3 条件必填
Cell condCell = row.createCell(3);
condCell.setCellValue(StringUtil.changeNull(writeData
.get(Constants.ERR_TYPE.COND_ERR.value)));
// 4 固定值
Cell fixCell = row.createCell(4);
fixCell.setCellValue(StringUtil.changeNull(writeData
.get(Constants.ERR_TYPE.FIX_ERR.value)));
// 5 枚举
Cell enumCell = row.createCell(5);
enumCell.setCellValue(StringUtil.changeNull(writeData
.get(Constants.ERR_TYPE.ENUM_ERR.value)));
// 6 键值不符
Cell mappingCell = row.createCell(6);
mappingCell.setCellValue(StringUtil.changeNull(writeData
.get(Constants.ERR_TYPE.MAPPING_ERR.value)));
// 6 值类型错误
Cell typeCell = row.createCell(7);
typeCell.setCellValue(StringUtil.changeNull(writeData
.get(Constants.ERR_TYPE.TYPE_ERR.value)));
// 6 值类型错误
Cell compCell = row.createCell(8);
compCell.setCellValue(StringUtil.changeNull(writeData
.get(Constants.ERR_TYPE.COMP_ERR.value)));
if (computeDisposeError(account + "_" + assetTag)) {
_syncStatisticsMap.put(account + "_" + assetTag + "_"
+ Constants.SUFFIX_ISOK, 1);
fos = (FileOutputStream) Pub._syncXlsMap.get(account + "_"
+ assetTag + "_os");
wb = (HSSFWorkbook) Pub._syncXlsMap.get(account + "_"
+ assetTag + "_wb");
if (fos != null && wb != null) {
wb.write(fos);
fos.flush();
fos.close();
}
// Pub._syncXlsMap.remove(account+"_"+assetTag+"_os");
// Pub._syncXlsMap.remove(account+"_"+assetTag+"_wb");
// Pub._syncXlsMap.remove(account+"_"+assetTag+"_sheet");
}
return true;
} catch (Exception e) {
// _syncXlsMap.remove(account + "_"
// + assetTag + "_os");
// _syncXlsMap.remove(account + "_"
// + assetTag + "_wb");
// _syncXlsMap.remove(account + "_"
// + assetTag + "_sheet");
// _syncStatisticsMap.put(account+"_"+assetTag +
// Constants.SUFFIX_ISOK, 2);
throw new ServiceException("写入Excel文件异常," + account + "_"
+ assetTag, e);
} finally {
// compute(account+"_"+assetTag);
if (_syncStatisticsMap.get(account + "_" + assetTag + "_"
+ Constants.SUFFIX_ISOK) == 1) {
Pub._syncXlsMap.remove(account + "_" + assetTag + "_os");
Pub._syncXlsMap.remove(account + "_" + assetTag + "_wb");
Pub._syncXlsMap.remove(account + "_" + assetTag + "_sheet");
}
}
}
/**
* 释放用户本次上传操作的资源
*
* @param account
* 用户账号
* @param asset_tag
* 资产类型
*/
public synchronized static void destoryUserResource(final String account,
final String asset_tag) {
try {
FileOutputStream fos = (FileOutputStream) _syncXlsMap.get(account
+ "_" + asset_tag + "_os");
HSSFWorkbook wb = (HSSFWorkbook) _syncXlsMap.get(account + "_"
+ asset_tag + "_wb");
if (fos != null && wb != null) {
wb.write(fos);
fos.flush();
fos.close();
}
} catch (Exception e) {
} finally {
_syncXlsMap.remove(account + "_" + asset_tag + "_os");
_syncXlsMap.remove(account + "_" + asset_tag + "_wb");
_syncXlsMap.remove(account + "_" + asset_tag + "_sheet");
_syncOuterMap.remove(account + "_" + asset_tag);
}
// Pub._syncStatisticsMap.remove(account + "_" + asset_tag + "_"
// + Constants.SUFFIX_TOTAL);
// Pub._syncStatisticsMap.remove(account + "_" + asset_tag + "_"
// + Constants.SUFFIX_ERROR);
// Pub._syncStatisticsMap.remove(account + "_" + asset_tag + "_"
// + Constants.SUFFIX_COMPUTE);
// Pub._syncStatisticsMap.remove(account + "_" + asset_tag + "_"
// + Constants.SUFFIX_ISOK);
// Pub._syncStatisticsMap.remove(account + "_" + asset_tag + "_"
// + Constants.SUFFIX_SUCCESS);
// }
}
/**
* 资源初始化。在用户接入系统准备进行资产模板上传时进行相关全局变量的初始化
*
* @param logon_account
* 用户账号
* @param asset_tag
* 资产类型
* @param outer
* 该用户的printwirter句柄
* @param rows
* 本次上传总资产记录条数
* @param errPath
* 该用户错误记录文件路径
*/
public synchronized static void init(final String logon_account, final String asset_tag,
final PrintWriter outer, final int rows, final String errPath) {
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_TOTAL, rows);
Pub._syncOuterMap.put(logon_account + "_" + asset_tag, outer);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_ISOK, 2);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_SUCCESS, 0);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_ERROR, 0);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_COMPUTE, 0);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_LOSE, 0);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_ONESE, 0);
String errFile = errPath + File.separator + logon_account + "_"
+ asset_tag + "_错误记录.xls";
if (!fileExists(errFile)) {
throw new ServiceException("初始化用户:【" + logon_account + "】,资产类型:【"
+ asset_tag + "】删除原有错误文件失败");
}
try {
FileOutputStream fos = new FileOutputStream(errFile);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet wbsheet = wb.createSheet("错误记录");
// wbsheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 8));
wbsheet.createFreezePane(0, 1);
wbsheet.setColumnWidth((short) 0, (short) (80 * 35.7));
wbsheet.setColumnWidth((short) 1, (short) (115 * 35.7));
wbsheet.setColumnWidth((short) 2, (short) (240 * 35.7));
wbsheet.setColumnWidth((short) 3, (short) (165 * 35.7));
wbsheet.setColumnWidth((short) 4, (short) (85 * 35.7));
wbsheet.setColumnWidth((short) 5, (short) (180 * 35.7));
wbsheet.setColumnWidth((short) 6, (short) (140 * 35.7));
wbsheet.setColumnWidth((short) 7, (short) (150 * 35.7));
wbsheet.setColumnWidth((short) 8, (short) (200 * 35.7));
/*******************************************************************
* 设置样式
*/
HSSFCellStyle commStyle = wb.createCellStyle();
HSSFCellStyle errStyle = wb.createCellStyle();
commStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
errStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 9); // 字体高度
// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
font.setFontName("微软雅黑"); // 字体
font.setItalic(true);
commStyle.setFont(font);
errStyle.setFont(font);
commStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());// 设置背景色
commStyle.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
commStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
commStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
commStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
commStyle.setBottomBorderColor(HSSFColor.BLACK.index);
commStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
commStyle.setLeftBorderColor(HSSFColor.BLACK.index);
commStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
commStyle.setRightBorderColor(HSSFColor.BLACK.index);
commStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
commStyle.setTopBorderColor(HSSFColor.BLACK.index);
errStyle.setFillForegroundColor(IndexedColors.RED.getIndex());// 设置背景色
errStyle.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
errStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
errStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
errStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
errStyle.setBottomBorderColor(HSSFColor.BLACK.index);
errStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
errStyle.setLeftBorderColor(HSSFColor.BLACK.index);
errStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
errStyle.setRightBorderColor(HSSFColor.BLACK.index);
errStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
errStyle.setTopBorderColor(HSSFColor.BLACK.index);
/*******************************************************************
* 设置样式结束
*/
// Constants.REGULAR_TIP t = Constants.REGULAR_TIP.valueOf(asset_tag);
// HSSFRow tiprow = wbsheet.createRow(0);
// Cell cell_tip = tiprow.createCell(0);
// tiprow.setHeight((short) (15.625 * 190));
// cell_tip.setCellStyle(commStyle);
// cell_tip.setCellValue(t.value);
HSSFRow row = wbsheet.createRow(0);
Cell cell_no = row.createCell(0);
Cell cell_asset = row.createCell(1);
Cell cell_1 = row.createCell(2);
Cell cell_2 = row.createCell(3);
Cell cell_3 = row.createCell(4);
Cell cell_4 = row.createCell(5);
Cell cell_5 = row.createCell(6);
Cell cell_6 = row.createCell(7);
Cell cell_7 = row.createCell(8);
cell_no.setCellStyle(commStyle);
cell_asset.setCellStyle(commStyle);
cell_1.setCellStyle(errStyle);
cell_2.setCellStyle(errStyle);
cell_3.setCellStyle(errStyle);
cell_4.setCellStyle(errStyle);
cell_6.setCellStyle(errStyle);
cell_5.setCellStyle(errStyle);
cell_5.setCellStyle(errStyle);
cell_7.setCellStyle(errStyle);
cell_no.setCellValue(Constants.ERR_NO);
cell_asset.setCellValue(Constants.ERR_UNIQUE_ID);
cell_1.setCellValue(Constants.ERR_1);
cell_2.setCellValue(Constants.ERR_2);
cell_3.setCellValue(Constants.ERR_3);
cell_4.setCellValue(Constants.ERR_4);
cell_5.setCellValue(Constants.ERR_5);
cell_6.setCellValue(Constants.ERR_6);
cell_7.setCellValue(Constants.ERR_7);
Pub._syncXlsMap.put(logon_account + "_" + asset_tag + "_os", fos);
Pub._syncXlsMap.put(logon_account + "_" + asset_tag + "_wb", wb);
Pub._syncXlsMap.put(logon_account + "_" + asset_tag + "_sheet",
wbsheet);
} catch (Exception e) {
throw new ServiceException("初始化用户:【" + logon_account + "】,资产类型:【"
+ asset_tag + "】错误XLS记录模板失败", e);
}
}
/**
* 资源初始化。在用户接入系统准备进行资产模板上传时进行相关全局变量的初始化
*
* @param logon_account
* 用户账号
* @param asset_tag
* 资产类型
* @param outer
* 该用户的printwirter句柄
* @param rows
* 本次上传总资产记录条数
* @param errPath
* 该用户错误记录文件路径
*/
public synchronized static void init_page(final String logon_account, final String asset_tag,
final PrintWriter outer, final int rows, final String errPath, final String pageNum) {
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_TOTAL, rows);
//Pub._syncOuterMap.put(logon_account + "_" + asset_tag, outer);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_ISOK, 2);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_SUCCESS, 0);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_ERROR, 0);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_COMPUTE, 0);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_LOSE, 0);
Pub._syncStatisticsMap.put(logon_account + "_" + asset_tag + "_"
+ Constants.SUFFIX_ONESE, 0);
String errFile = errPath + File.separator + logon_account + "_"
+ asset_tag + "_错误记录" + pageNum + ".xls";
if (!fileExists(errFile)) {
throw new ServiceException("初始化用户:【" + logon_account + "】,资产类型:【"
+ asset_tag + "】删除原有错误文件失败");
}
try {
FileOutputStream fos = new FileOutputStream(errFile);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet wbsheet = wb.createSheet("错误记录");
// wbsheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 8));
wbsheet.createFreezePane(0, 1);
wbsheet.setColumnWidth((short) 0, (short) (80 * 35.7));
wbsheet.setColumnWidth((short) 1, (short) (115 * 35.7));
wbsheet.setColumnWidth((short) 2, (short) (240 * 35.7));
wbsheet.setColumnWidth((short) 3, (short) (165 * 35.7));
wbsheet.setColumnWidth((short) 4, (short) (85 * 35.7));
wbsheet.setColumnWidth((short) 5, (short) (180 * 35.7));
wbsheet.setColumnWidth((short) 6, (short) (140 * 35.7));
wbsheet.setColumnWidth((short) 7, (short) (150 * 35.7));
wbsheet.setColumnWidth((short) 8, (short) (200 * 35.7));
/*******************************************************************
* 设置样式
*/
HSSFCellStyle commStyle = wb.createCellStyle();
HSSFCellStyle errStyle = wb.createCellStyle();
commStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
errStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 9); // 字体高度
// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
font.setFontName("微软雅黑"); // 字体
font.setItalic(true);
commStyle.setFont(font);
errStyle.setFont(font);
commStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());// 设置背景色
commStyle.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
commStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
commStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
commStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
commStyle.setBottomBorderColor(HSSFColor.BLACK.index);
commStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
commStyle.setLeftBorderColor(HSSFColor.BLACK.index);
commStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
commStyle.setRightBorderColor(HSSFColor.BLACK.index);
commStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
commStyle.setTopBorderColor(HSSFColor.BLACK.index);
errStyle.setFillForegroundColor(IndexedColors.RED.getIndex());// 设置背景色
errStyle.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
errStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
errStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
errStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
errStyle.setBottomBorderColor(HSSFColor.BLACK.index);
errStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
errStyle.setLeftBorderColor(HSSFColor.BLACK.index);
errStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
errStyle.setRightBorderColor(HSSFColor.BLACK.index);
errStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
errStyle.setTopBorderColor(HSSFColor.BLACK.index);
/*******************************************************************
* 设置样式结束
*/
// Constants.REGULAR_TIP t = Constants.REGULAR_TIP.valueOf(asset_tag);
// HSSFRow tiprow = wbsheet.createRow(0);
// Cell cell_tip = tiprow.createCell(0);
// tiprow.setHeight((short) (15.625 * 190));
// cell_tip.setCellStyle(commStyle);
// cell_tip.setCellValue(t.value);
HSSFRow row = wbsheet.createRow(0);
Cell cell_no = row.createCell(0);
Cell cell_asset = row.createCell(1);
Cell cell_1 = row.createCell(2);
Cell cell_2 = row.createCell(3);
Cell cell_3 = row.createCell(4);
Cell cell_4 = row.createCell(5);
Cell cell_5 = row.createCell(6);
Cell cell_6 = row.createCell(7);
Cell cell_7 = row.createCell(8);
cell_no.setCellStyle(commStyle);
cell_asset.setCellStyle(commStyle);
cell_1.setCellStyle(errStyle);
cell_2.setCellStyle(errStyle);
cell_3.setCellStyle(errStyle);
cell_4.setCellStyle(errStyle);
cell_6.setCellStyle(errStyle);
cell_5.setCellStyle(errStyle);
cell_5.setCellStyle(errStyle);
cell_7.setCellStyle(errStyle);
cell_no.setCellValue(Constants.ERR_NO);
cell_asset.setCellValue(Constants.ERR_UNIQUE_ID);
cell_1.setCellValue(Constants.ERR_1);
cell_2.setCellValue(Constants.ERR_2);
cell_3.setCellValue(Constants.ERR_3);
cell_4.setCellValue(Constants.ERR_4);
cell_5.setCellValue(Constants.ERR_5);
cell_6.setCellValue(Constants.ERR_6);
cell_7.setCellValue(Constants.ERR_7);
Pub._syncXlsMap.put(logon_account + "_" + asset_tag + "_os", fos);
Pub._syncXlsMap.put(logon_account + "_" + asset_tag + "_wb", wb);
Pub._syncXlsMap.put(logon_account + "_" + asset_tag + "_sheet",
wbsheet);
} catch (Exception e) {
throw new ServiceException("初始化用户:【" + logon_account + "】,资产类型:【"
+ asset_tag + "】错误XLS记录模板失败", e);
}
}
/**
* 判断文件是否存在
*
* @param path
* @return
*/
private static boolean fileExists(String path) {
File file = null;
try {
file = new File(path);
if (file.exists())
file.delete();
return true;
} catch (Exception e) {
return false;
} finally {
file = null;
}
}
/**
* 私有构造,防止实例化
*/
private Pub() {
}
public static void main(String args[]) {
Pub._syncStatisticsMap.put("林剑_" + Constants.RES_KEY.APPLY.name() + "_"
+ Constants.SUFFIX_TOTAL, 0);
for (int i = 0; i < 1000; i++) {
Pub._syncStatisticsMap.put("林剑_" + Constants.RES_KEY.APPLY.name()
+ "_" + Constants.SUFFIX_TOTAL, Pub._syncStatisticsMap
.get("林剑_" + Constants.RES_KEY.APPLY.name() + "_"
+ Constants.SUFFIX_TOTAL) + 1);
}
System.out.println(Pub._syncStatisticsMap
.get("林剑_" + Constants.RES_KEY.APPLY.name() + "_"
+ Constants.SUFFIX_TOTAL));
}
}
真的有能力的大佬看看给评论一下,并且可以明显看到,上次更新之后去除了一些检验的方式,这是原本的代码
java
@Override
public void dodownload(ProvincialEntity provincialEntity) {
try {
NowUser nowUser = WebUtil.getNowUser();
//开始时间记录
long start = System.currentTimeMillis();
//条数
int count = 0;
Date nowTime = new Date();
// 往数据库存储的路径(不含配置的前缀路径)
String dbSavePath = "export" + File.separator + new SimpleDateFormat("yyyy").format(nowTime)
+ File.separator + new SimpleDateFormat("MM").format(nowTime)
+ File.separator + new SimpleDateFormat("dd").format(nowTime)
+ File.separator;
// 临时文件实际保存路径
String fileSavePath = ExportExcelUtils.getImplementTemplateNotRequest() + dbSavePath;
// 获取保存路径,没有对应目录的话自行创建
String savePath = FileUtil.createDir(fileSavePath);
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");
String dateNowStr = sdf.format(d);
String fileType="";
String fileName="IT设备管理_"+dateNowStr;
//导出临时文件的命名
String filePathFront= savePath + "IT设备管理_" + dateNowStr;
// 数据库里存的文件路径即上传到云平台的文件命名
Date date = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf1.format(date);
//初次插入文件对象
AssetExportFile exportFile = exportDetailViewListPeriodNameInsertFile(fileName, filePathFront, nowUser, format);
String exportFileId = exportFile.getId();
//1、根据查询条件获取集合-erp
List<String> result=new ArrayList<>();
try{
//1、根据查询条件获取集合-erp
result = queryViewListForExportCount(provincialEntity);
}
catch(Exception exception){
log.error("导出数据异常:" + exception);
}
if(null!=result&&!result.isEmpty()) {
//1、根据查询条件获取集合-erp
count=Integer.valueOf(result.get(0));
if(count> FlowUtils.COUNT_FOR_TYPE){
//生成zip格式
fileName=fileName+".zip";
fileType=FlowUtils.ZIP_TYPE;
}else{
// 生成xlsx格式
fileName= fileName+".xlsx";
fileType=FlowUtils.XLSX_TYPE;
}
// 导出临时文件最终命名和路径
String finalFilePath = filePathFront + "." + fileType;
//上传到服务器的实际文件的命名
String finalFileName = dbSavePath + "IT设备管理_" + dateNowStr + "." + fileType;
//一个excel的总数据量
int oneExcelSize = FlowUtils.ONE_EXCEL_SIZE;
//计算需要导出的excel的个数
int excelCount = count%oneExcelSize==0?count/oneExcelSize:(count/oneExcelSize +1);
//声明fileList,大小为excelCount
final List<File> resultFileList = new ArrayList<>(excelCount);
EasyExcelExportUtils easyExcelExportUtils = new EasyExcelExportUtils();
if (excelCount>1){
for(int i=0;i<excelCount;i++){
// 按每个excel的数据量大小来切分查询结果list
List<ProvincialPreviewDataExcel> res = queryViewListForExport(oneExcelSize,i * oneExcelSize, provincialEntity);;
//文件命名
String excelFileName = savePath+"IT设备管理_"+dateNowStr+"-"+String.valueOf(i+1)+".xlsx";
File file = new File(excelFileName);
resultFileList.add(file);
EasyExcel.write(excelFileName, ProvincialPreviewDataExcel.class)
.sheet("sheet").doWrite(res);
}
// 压缩文件
final File file = easyExcelExportUtils.compressFile(resultFileList, true, exportFileId, filePathFront);
} else {
List<ProvincialPreviewDataExcel> res = queryViewListForExport(oneExcelSize, 0, provincialEntity);
EasyExcel.write(finalFilePath, ProvincialPreviewDataExcel.class)
.sheet("sheet").doWrite(res);
}
CephUtil.uploadExportFile(finalFileName, finalFilePath);
FileUtil.executeFile(finalFilePath);
//更新导出文件记录
exportDetailViewListPeriodNameUpdateFile(exportFile,fileName,fileType,finalFileName,exportFileId);
long end = System.currentTimeMillis();
log.info("导出,耗时---------:" + (end - start) / 1000 + " s");
}
} catch (Exception e) {
log.error("exception's message:{}",e.getStackTrace());
}
}
这是easyexcel的导入,因为我最开始没有想到他们的导入还能用,实话说我是没有想到他真的能用,真的,贼尴尬就,他们的测试环境老就没有人维护,生产环境不敢测试加上他们的导入我校验一直过不去,我也没有办法测试
java
public void dodownloadPoi(ProvincialEntity provincialEntity,HttpServletRequest request, HttpServletResponse response) {
FileOutputStream fos = null;
try {
String path = ExportExcelUtils.getTzImplementTemplate(request);
String fileName = "台账-软件停用信息.xlsx";
File newFile = ExcelUtils.createNewPayFile(path + "provincialManageExportExcel.xlsx", path + "temp");
//获取Workbook
org.apache.poi.ss.usermodel.Workbook wb = ExcelUtils.openExportExcelStream(request, response, fileName, path, newFile);
//获取第一个sheet
Sheet sheet = wb.getSheetAt(0);
if (sheet == null) {
throw new RuntimeException("出错");
}
fos = new FileOutputStream(newFile);
//根据编号查询当前转移单所选择的部门内转移资产明细信息
List<ProvincialPreviewDataExcel> list = queryViewListForExport(10,0, provincialEntity);;
if (null != list && list.size() > 0) {
Row row;
int rowIndex = 11;
for (int i = 0; i < list.size(); i++) {
row = sheet.getRow(rowIndex);
if (row == null) {
row = sheet.createRow(rowIndex);
}
//序号
ExportExcelUtils.setCellValue(row, 0, 1, String.valueOf(rowIndex));
rowIndex++;
}
}
//关闭文件输出流
ExcelUtils.closeExportExcelStream(response, wb, fos, newFile);
//删除文件
ExcelUtils.deleteFile(newFile);
log.info("信息写入完成");
} catch (Exception e) {
log.error("exception's message:{}",e.getStackTrace());
}finally {
try {
if (fos != null) {
fos.close();
}
}catch (Exception e){
log.error("exception's message:{}",e.getStackTrace());
}
}
}
这是poi的方式,读取我的模版文件,然后变成了如何将读取的文件,找了ai,然后ai给出了好几版方案都因为ai给出的实体类涉及到的依赖不对导致没有办法使用直接报错,这种找错就是很麻烦,包括直接把报错给了ai,ai也是屎上雕花,老难了,然后只能提供一些局部的问题处理,不能全体的找ai处理,这种局部的实话说还是处理的挺好的,然后我找ai处理了一下局部的问题然后就成了
java
public void dodownload1(ProvincialEntity provincialEntity) {
try {
NowUser nowUser = WebUtil.getNowUser();
//开始时间记录
long start = System.currentTimeMillis();
//条数
int count = 0;
Date nowTime = new Date();
// 往数据库存储的路径(不含配置的前缀路径)
String dbSavePath = "export" + File.separator + new SimpleDateFormat("yyyy").format(nowTime)
+ File.separator + new SimpleDateFormat("MM").format(nowTime)
+ File.separator + new SimpleDateFormat("dd").format(nowTime)
+ File.separator;
// 临时文件实际保存路径
String fileSavePath = ExportExcelUtils.getImplementTemplateNotRequest() + dbSavePath;
// 获取保存路径,没有对应目录的话自行创建
String savePath = FileUtil.createDir(fileSavePath);
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");
String dateNowStr = sdf.format(d);
String fileType = "";
String fileName = "IT设备管理_" + dateNowStr;
//导出临时文件的命名
String filePathFront = savePath + "IT设备管理_" + dateNowStr;
// 数据库里存的文件路径即上传到云平台的文件命名
Date date = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf1.format(date);
//初次插入文件对象
AssetExportFile exportFile = exportDetailViewListPeriodNameInsertFile(fileName, filePathFront, nowUser, format);
String exportFileId = exportFile.getId();
//1、根据查询条件获取集合-erp
List<String> result = new ArrayList<>();
try {
//1、根据查询条件获取集合-erp
result = queryViewListForExportCount(provincialEntity);
} catch (Exception exception) {
log.error("导出数据异常:" + exception);
}
if (null != result && !result.isEmpty()) {
//1、根据查询条件获取集合-erp
count = Integer.valueOf(result.get(0));
if (count > FlowUtils.COUNT_FOR_TYPE) {
//生成zip格式
fileName = fileName + ".zip";
fileType = FlowUtils.ZIP_TYPE;
} else {
// 生成xlsx格式
fileName = fileName + ".xlsx";
fileType = FlowUtils.XLSX_TYPE;
}
// 导出临时文件最终命名和路径
String finalFilePath = filePathFront + "." + fileType;
//上传到服务器的实际文件的命名
String finalFileName = dbSavePath + "IT设备管理_" + dateNowStr + "." + fileType;
//一个excel的总数据量
int oneExcelSize = FlowUtils.ONE_EXCEL_SIZE;
//计算需要导出的excel的个数
int excelCount = count % oneExcelSize == 0 ? count / oneExcelSize : (count / oneExcelSize + 1);
//声明fileList,大小为excelCount
final List<File> resultFileList = new ArrayList<>(excelCount);
EasyExcelExportUtils easyExcelExportUtils = new EasyExcelExportUtils();
if (excelCount > 1) {
for (int i = 0; i < excelCount; i++) {
// 按每个excel的数据量大小来切分查询结果list
List<ProvincialPreviewDataExcel> res = queryViewListForExport(oneExcelSize, i * oneExcelSize, provincialEntity);
//文件命名
String excelFileName = savePath + "IT设备管理_" + dateNowStr + "-" + String.valueOf(i + 1) + ".xlsx";
File file = new File(excelFileName);
resultFileList.add(file);
EasyExcel.write(excelFileName, ProvincialPreviewDataExcel.class)
.sheet("sheet").doWrite(res);
}
// 压缩文件
final File file = easyExcelExportUtils.compressFile(resultFileList, true, exportFileId, filePathFront);
} else {
List<ProvincialPreviewDataExcel> res = queryViewListForExport(oneExcelSize, 0, provincialEntity);
// EasyExcel.write(finalFilePath, ProvincialPreviewDataExcel.class)
// .sheet("sheet").doWrite(res);
InputStream templateStream = getClass().getClassLoader().getResourceAsStream("file/ams/provincialManageExportExcel.xlsx");
HSSFWorkbook workbook = new HSSFWorkbook(templateStream);
Sheet sheet = workbook.getSheetAt(0);
// 填充数据
int rowIndex = 11;
for (ProvincialPreviewDataExcel item : res) {
Row row = sheet.createRow(rowIndex++);
//序号
ExportExcelUtils.setCellValue(row, 0, 1, String.valueOf(item.getRankNo()));
ExportExcelUtils.setCellValue(row, 1, 1, item.getProvCodeC());
ExportExcelUtils.setCellValue(row, 2, 1, item.getCityCodeC()); // 原为 getCITY_CODE_C()
ExportExcelUtils.setCellValue(row, 3, 1, item.getOriginC()); // 原为 getORIGIN_C()
ExportExcelUtils.setCellValue(row, 4, 1, item.getData7());
ExportExcelUtils.setCellValue(row, 5, 1, item.getOAssetTagNo()); // 原为 getO_ASSET_TAG_NO()
ExportExcelUtils.setCellValue(row, 6, 1, item.getData8());
ExportExcelUtils.setCellValue(row, 7, 1, item.getData9());
ExportExcelUtils.setCellValue(row, 8, 1, item.getOEntityName()); // 原为 getO_ENTITY_NAME()
ExportExcelUtils.setCellValue(row, 9, 1, item.getData10());
ExportExcelUtils.setCellValue(row, 10, 1, item.getData11());
ExportExcelUtils.setCellValue(row, 11, 1, item.getData12());
ExportExcelUtils.setCellValue(row, 12, 1, item.getData13());
ExportExcelUtils.setCellValue(row, 13, 1, item.getData14());
ExportExcelUtils.setCellValue(row, 14, 1, item.getData15());
ExportExcelUtils.setCellValue(row, 15, 1, item.getData17());
ExportExcelUtils.setCellValue(row, 16, 1, item.getAMajorManagerId()); // 原为 getA_MAJOR_MANAGER_ID()
ExportExcelUtils.setCellValue(row, 17, 1, item.getData18());
ExportExcelUtils.setCellValue(row, 18, 1, item.getData19());
ExportExcelUtils.setCellValue(row, 19, 1, item.getIndependentSignsC()); // 原为 getINDEPENDENT_SIGNS_C()
ExportExcelUtils.setCellValue(row, 20, 1, item.getFAssetTagNo()); // 原为 getF_ASSET_TAG_NO()
ExportExcelUtils.setCellValue(row, 21, 1, item.getAppAreaC()); // 原为 getAPP_AREA_C()
ExportExcelUtils.setCellValue(row, 22, 1, item.getAssetCodeDirectoryC()); // 原为 getASSET_CODE_DIRECTORY_C()
ExportExcelUtils.setCellValue(row, 23, 1, item.getAccountingMajorC()); // 原为 getACCOUNTING_MAJOR_C()
ExportExcelUtils.setCellValue(row, 24, 1, item.getAssetKeywordC()); // 原为 getASSET_KEYWORD_C()
ExportExcelUtils.setCellValue(row, 25, 1, item.getData20());
ExportExcelUtils.setCellValue(row, 26, 1, item.getSystemPeofessionTypeC()); // 原为 getSYSTEM_PEOFESSION_TYPE_C()
ExportExcelUtils.setCellValue(row, 27, 1, item.getProviderName()); // 原为 getPROVIDER_NAME()
ExportExcelUtils.setCellValue(row, 28, 1, item.getData21());
ExportExcelUtils.setCellValue(row, 29, 1, item.getOStandardModel()); // 原为 getO_STANDARD_MODEL()
ExportExcelUtils.setCellValue(row, 30, 1, item.getData22());
ExportExcelUtils.setCellValue(row, 31, 1, item.getSerialNumber()); // 原为 getSERIAL_NUMBER()
ExportExcelUtils.setCellValue(row, 32, 1, item.getAssetCount()); // 原为 getASSET_COUNT()
ExportExcelUtils.setCellValue(row, 33, 1, item.getAssetCountUnitC()); // 原为 getASSET_COUNT_UNIT_C()
ExportExcelUtils.setCellValue(row, 34, 1, item.getAuxiliaryCount()); // 原为 getAUXILIARY_COUNT()
ExportExcelUtils.setCellValue(row, 35, 1, item.getAuxiliaryCountUnitC()); // 原为 getAUXILIARY_COUNT_UNIT_C()
ExportExcelUtils.setCellValue(row, 36, 1, item.getBuyDate()); // 原为 getBUY_DATE()
ExportExcelUtils.setCellValue(row, 37, 1, item.getData23());
ExportExcelUtils.setCellValue(row, 38, 1, item.getUserAgrLimit()); // 原为 getUSER_AGR_LIMIT()
ExportExcelUtils.setCellValue(row, 39, 1, item.getWarrantySDate()); // 原为 getWARRANTY_S_DATE()
ExportExcelUtils.setCellValue(row, 40, 1, item.getWarrantyEDate()); // 原为 getWARRANTY_E_DATE()
ExportExcelUtils.setCellValue(row, 41, 1, item.getProjectCode()); // 原为 getPROJECT_CODE()
ExportExcelUtils.setCellValue(row, 42, 1, item.getProjectName()); // 原为 getPROJECT_NAME()
ExportExcelUtils.setCellValue(row, 43, 1, item.getProjectSort()); // 原为 getPROJECT_SORT()
ExportExcelUtils.setCellValue(row, 44, 1, item.getIfExpandC()); // 原为 getIF_EXPAND_C()
ExportExcelUtils.setCellValue(row, 45, 1, item.getDepartmentCode()); // 原为 getDEPARTMENT_CODE()
ExportExcelUtils.setCellValue(row, 46, 1, item.getData24());
ExportExcelUtils.setCellValue(row, 47, 1, item.getBlamerId()); // 原为 getBLAMER_ID()
ExportExcelUtils.setCellValue(row, 48, 1, item.getData25());
ExportExcelUtils.setCellValue(row, 49, 1, item.getBlamerEmail()); // 原为 getBLAMER_EMAIL()
ExportExcelUtils.setCellValue(row, 50, 1, item.getManagerId()); // 原为 getMANAGER_ID()
ExportExcelUtils.setCellValue(row, 51, 1, item.getData26());
ExportExcelUtils.setCellValue(row, 52, 1, item.getManagerEmail()); // 原为 getMANAGER_EMAIL()
ExportExcelUtils.setCellValue(row, 53, 1, item.getManagerBelongCom()); // 原为 getMANAGER_BELONG_COM()
ExportExcelUtils.setCellValue(row, 54, 1, item.getFaRoomName()); // 原为 getFA_ROOM_NAME()
ExportExcelUtils.setCellValue(row, 55, 1, item.getData27());
ExportExcelUtils.setCellValue(row, 56, 1, item.getStandardAddress()); // 原为 getSTANDARD_ADDRESS()
ExportExcelUtils.setCellValue(row, 57, 1, item.getAssetOrinValue()); // 原为 getASSET_ORIN_VALUE()
ExportExcelUtils.setCellValue(row, 58, 1, item.getAssetNetAmount()); // 原为 getASSET_NET_AMOUNT()
ExportExcelUtils.setCellValue(row, 59, 1, item.getMaintainWayC()); // 原为 getMAINTAIN_WAY_C()
ExportExcelUtils.setCellValue(row, 60, 1, item.getData28());
ExportExcelUtils.setCellValue(row, 61, 1, item.getMaintainerNo()); // 原为 getMAINTAINER_NO()
ExportExcelUtils.setCellValue(row, 62, 1, item.getData29());
ExportExcelUtils.setCellValue(row, 63, 1, item.getMaintainerEmail()); // 原为 getMAINTAINER_EMAIL()
ExportExcelUtils.setCellValue(row, 64, 1, item.getIfMaintenanceC()); // 原为 getIF_MAINTENANCE_C()
ExportExcelUtils.setCellValue(row, 65, 1, item.getAssetsBelongToC()); // 原为 getASSETS_BELONG_TO_C()
ExportExcelUtils.setCellValue(row, 66, 1, item.getMainConfDesc()); // 原为 getMAIN_CONF_DESC()
ExportExcelUtils.setCellValue(row, 67, 1, item.getBelongToSys()); // 原为 getBELONG_TO_SYS()
ExportExcelUtils.setCellValue(row, 68, 1, item.getOnLineDate()); // 原为 getON_LINE_DATE()
ExportExcelUtils.setCellValue(row, 69, 1, item.getOutLineDate()); // 原为 getOUT_LINE_DATE()
ExportExcelUtils.setCellValue(row, 70, 1, item.getFixedPower()); // 原为 getFIXED_POWER()
ExportExcelUtils.setCellValue(row, 71, 1, item.getSysHostName()); // 原为 getSYS_HOST_NAME()
ExportExcelUtils.setCellValue(row, 72, 1, item.getDBelongRoomPropertyC()); // 原为 getD_BELONG_ROOM_PROPERTY_C()
ExportExcelUtils.setCellValue(row, 73, 1, item.getStartU()); // 原为 getSTART_U()
ExportExcelUtils.setCellValue(row, 74, 1, item.getInfrHeight()); // 原为 getINFR_HEIGHT()
ExportExcelUtils.setCellValue(row, 75, 1, item.getOsTypeVersion()); // 原为 getOS_TYPE_VERSION()
ExportExcelUtils.setCellValue(row, 76, 1, item.getPatchInfo()); // 原为 getPATCH_INFO()
ExportExcelUtils.setCellValue(row, 77, 1, item.getWirelessMacAddress()); // 原为 getWIRELESS_MAC_ADDRESS()
ExportExcelUtils.setCellValue(row, 78, 1, item.getEthernetMacAddress()); // 原为 getETHERNET_MAC_ADDRESS()
ExportExcelUtils.setCellValue(row, 79, 1, item.getHostName()); // 原为 getHOST_NAME()
ExportExcelUtils.setCellValue(row, 80, 1, item.getDeviceOid()); // 原为 getDEVICE_OID()
ExportExcelUtils.setCellValue(row, 81, 1, item.getDeviceDescription()); // 原为 getDEVICE_DESCRIPTION()
ExportExcelUtils.setCellValue(row, 82, 1, item.getIpInfo()); // 原为 getIP_INFO()
ExportExcelUtils.setCellValue(row, 83, 1, item.getUpLinkNetDev()); // 原为 getUP_LINK_NET_DEV()
ExportExcelUtils.setCellValue(row, 84, 1, item.getDeviceLayerLevelC()); // 原为 getDEVICE_LAYER_LEVEL_C()
ExportExcelUtils.setCellValue(row, 85, 1, item.getCarbinetTypeC()); // 原为 getCARBINET_TYPE_C()
ExportExcelUtils.setCellValue(row, 86, 1, item.getSubCarbinetType()); // 原为 getSUB_CARBINET_TYPE()
ExportExcelUtils.setCellValue(row, 87, 1, item.getSubCarbinetMount()); // 原为 getSUB_CARBINET_MOUNT()
ExportExcelUtils.setCellValue(row, 88, 1, item.getCarbinetSDTypeC()); // 原为 getCARBINET_S_D_TYPE_C()
ExportExcelUtils.setCellValue(row, 89, 1, item.getEntityDescripe()); // 原为 getENTITY_DESCRIPE()
ExportExcelUtils.setCellValue(row, 90, 1, item.getTerminalFuncC()); // 原为 getTERMINAL_FUNC_C()
ExportExcelUtils.setCellValue(row, 91, 1, item.getAddAssetSignS()); // 原为 getADD_ASSET_SIGN_S()
ExportExcelUtils.setCellValue(row, 92, 1, item.getReuseSignS()); // 原为 getREUSE_SIGN_S()
ExportExcelUtils.setCellValue(row, 93, 1, item.getOutPowerSifnS()); // 原为 getOUT_POWER_SIFN_S()
ExportExcelUtils.setCellValue(row, 94, 1, item.getOProNoNameS()); // 原为 getO_PRO_NO_NAME_S()
ExportExcelUtils.setCellValue(row, 95, 1, item.getAcceptor()); // 原为 getACCEPTOR()
ExportExcelUtils.setCellValue(row, 96, 1, item.getAttFacilityAffixS()); // 原为 getATT_FACILITY_AFFIX_S()
ExportExcelUtils.setCellValue(row, 97, 1, item.getRemakS()); // 原为 getREMAK_S()
ExportExcelUtils.setCellValue(row, 98, 1, item.getDeviceStayus()); // 原为 getDEVICE_STAYUS()
ExportExcelUtils.setCellValue(row, 99, 1, item.getMaintainCase()); // 原为 getMAINTAIN_CASE()
ExportExcelUtils.setCellValue(row, 100, 1, item.getRoomName()); // 原为 getROOM_NAME()
ExportExcelUtils.setCellValue(row, 101, 1, item.getCarbinetCode()); // 原为 getCARBINET_CODE()
ExportExcelUtils.setCellValue(row, 102, 1, item.getRemak1()); // 原为 getREMAK1()
ExportExcelUtils.setCellValue(row, 103, 1, item.getRemak2()); // 原为 getREMAK2()
ExportExcelUtils.setCellValue(row, 104, 1, item.getRemak3()); // 原为 getREMAK3()
ExportExcelUtils.setCellValue(row, 105, 1, item.getRemak4()); // 原为 getREMAK4()
ExportExcelUtils.setCellValue(row, 106, 1, item.getRemak5()); // 原为 getREMAK5()
ExportExcelUtils.setCellValue(row, 107, 1, item.getSysLifeStatusC()); // 原为 getSYS_LIFE_STATUS_C()
ExportExcelUtils.setCellValue(row, 108, 1, item.getDataSourceTypeC()); // 原为 getDATA_SOURCE_TYPE_C()
rowIndex++;// 只添加这一个字段
}
// 写入文件
FileOutputStream fos = new FileOutputStream(finalFilePath);
workbook.write(fos);
fos.close();
workbook.close();
}
CephUtil.uploadExportFile(finalFileName, finalFilePath);
FileUtil.executeFile(finalFilePath);
//更新导出文件记录
exportDetailViewListPeriodNameUpdateFile(exportFile, fileName, fileType, finalFileName, exportFileId);
long end = System.currentTimeMillis();
log.info("导出,耗时---------:" + (end - start) / 1000 + " s");
}
} catch (Exception e) {
log.error("exception's message:{}", e.getStackTrace());
}
}
没有全部完成,但是已经完成了大部分了的了剩下的就是小修小补了。