/**
* 添加单位工程信息
*
* @param ids
* @return
*/
@RequestMapping(params = "doSaveExcel")
@ResponseBody
public Map<String, Object> doSaveExcel(HttpServletRequest request) throws IllegalStateException, IOException{
Map<String, Object> returnMap = new HashMap<String, Object>();
//判断日志文件夹是否存在
File files =new File(request.getSession().getServletContext().getRealPath("/upload/log/"));
//如果文件夹不存在则创建
if (!files .exists() && !files .isDirectory())
{
System.out.println("//不存在");
files .mkdir();
} else
{
System.out.println("//目录存在");
}
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
/**页面控件的文件流**/
MultipartFile multipartFile = multipartRequest.getFile("file");
// String fb = multipartRequest.getParameter("fb");
if(multipartFile!=null)
{
String savePath = request.getSession().getServletContext().getRealPath("/upload/excel/");
File targetFile = new File(savePath);
//判断上传文件的保存目录是否存在
if (!targetFile.exists() && !targetFile.isDirectory()) {
System.out.println(savePath+"目录不存在,需要创建");
//创建目录
targetFile.mkdirs();
}
String path=savePath+"/"+multipartFile.getOriginalFilename();
File file = new File(path);
//判断文件是否存在,存在则删除
if (file.exists()) {
file.delete();
System.out.println("文件成功删除");
}
//上传
multipartFile.transferTo(new File(path));
ArrayList<String> list = new ArrayList<String>();
try {
ReadExcelUtils excelReader = new ReadExcelUtils(path);
// 对读取Excel表格内容测试
Map<Integer, Map<Integer,Object>> map = excelReader.readExcelContent();
String select = "select count(id) as id from t_dygcgl_dygcxx";
Map<String, Object> mapselect = systemService.findOneForJdbc(select);
int is= 0;
for (int i = 1; i <= map.size(); i++) {
//单位工程
String str = map.get(i).get(0).toString();
String mv0 = str.toString().replaceAll("(", "(");
mv0 = mv0.replaceAll(")", ")");
mv0 = mv0.replaceAll("\\s*", "");
String sql1 = String.format("select id,fsdwgcname from t_dwgc where fsdwgcname= '%s'",mv0);
Map<String, Object> map1 = systemService.findOneForJdbc(sql1);
//部位
String str1 = map.get(i).get(1).toString();
String mv1 = str1.toString().replaceAll("(", "(");
mv1 = mv1.replaceAll(")", ")");
mv1 = mv1.replaceAll("\\s*", "");
String sqla = String.format("select fsbdid,fsname from t_bw where fsname= '%s'",mv1);
Map<String, Object> map2 = systemService.findOneForJdbc(sqla);
//分部
String str2 = map.get(i).get(2).toString();
String mv2 = str2.toString().replaceAll("(", "(");
mv2 = mv2.replaceAll(")", ")");
mv2 = mv2.replaceAll("\\s*", "");
String sqls = String.format("select id,fsfbgcname from t_fbgc where fsfbgcname= '%s'",mv2);
Map<String, Object> map3 = systemService.findOneForJdbc(sqls);
//分项
String str3 = map.get(i).get(3).toString();
String mv3 = str3.toString().replaceAll("(", "(");
mv3 = mv3.replaceAll(")", ")");
mv3 = mv3.replaceAll("\\s*", "");
String sqlss1 = String.format("select id,fsfxgcname from t_fxgc where fsfxgcname= '%s'",mv3);
Map<String, Object> ma4 = systemService.findOneForJdbc(sqlss1);
//单元工程编码
String str4 = map.get(i).get(4).toString();
String mv4 = str4.toString().replaceAll("(", "(");
mv4 = mv4.replaceAll(")", ")");
mv4 = mv4.replaceAll("\\s*", "");
String sql4 = String.format("select id,FSDYGCBM from t_dygcgl_dygcxx where FSDYGCBM='%s'",mv4);
Map<String, Object> map4 = systemService.findOneForJdbc(sql4);
//单元工程名称
String str5 = map.get(i).get(5).toString();
String mv5 = str5.toString().replaceAll("(", "(");
mv5 = mv5.replaceAll(")", ")");
mv5 = mv5.replaceAll("\\s*", "");
String sql5 = String.format("select id,FSDYGCMC from t_dygcgl_dygcxx where FSDYGCMC='%s'",mv5);
Map<String, Object> map5 = systemService.findOneForJdbc(sql5);
if(map4 == null && map5 == null){
if (map1 !=null && map2 !=null && map3 !=null && ma4 !=null) {
String saveid=PageOfficeUtil.getUUID();
saveOrupdate(map.get(i),saveid);
is++;
// saveOrupdateConfig(map.get(i),saveid);
}
}
/* if (map4 !=null && map5 != null) {
if (map1 !=null && map2 !=null && map3 !=null && ma4 !=null) {
String saveid=PageOfficeUtil.getUUID();
saveOrupdate(map.get(i),saveid);
saveOrupdateConfig(map.get(i),saveid);
}
}*/
StringBuffer tempStr = new StringBuffer();
tempStr = tempStr.append("单位工程:"+map.get(i).get(0)+",").append("部位:"+map.get(i).get(1)+",")
.append("部位:"+map.get(i).get(1)+",").append("分部工程:"+map.get(i).get(2)+",")
.append("分项工程:"+map.get(i).get(3)+",").append("单位工程编码:"+map.get(i).get(4)+",")
.append("单位工程名称:"+map.get(i).get(5)+",");
if(map4 != null && map5 == null){
tempStr = tempStr.append("导入失败原因:单元工程编码重复。");
list.add(tempStr.toString());
}else if(map4 == null && map5 != null){
tempStr = tempStr.append("导入失败原因:单元工程名称重复。");
list.add(tempStr.toString());
}else if(map1 == null) {
tempStr = tempStr.append("导入失败原因:单位名称不存在。");
list.add(tempStr.toString());
}else if(map2 ==null){
tempStr = tempStr.append("导入失败原因:部位名称不存在。");
list.add(tempStr.toString());
}else if(map3 ==null){
tempStr = tempStr.append("导入失败原因:分部名称不存在。");
list.add(tempStr.toString());
}else if(ma4 ==null){
tempStr = tempStr.append("导入失败原因:分项名称不存在。");
list.add(tempStr.toString());
}else if(map4 !=null && map5 != null){
tempStr = tempStr.append("导入失败原因:单元工程名称和单元工程编码与原有数据重复。");
list.add(tempStr.toString());
}
}
int Increase= is;
int Total=map.size();
int Failure = Total - Increase;
//每次导入的TXT文件
SimpleDateFormat simpleDateFormat;
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String str = simpleDateFormat.format(date);
File filelj=new File(request.getSession().getServletContext().getRealPath("/upload/log/"+str+".txt"));
returnMap.put("Total", Total);
returnMap.put("Increase", Increase);
returnMap.put("Failure", Failure);
returnMap.put("str", str);
if(!filelj.exists())
{
try {
filelj.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(filelj), "UTF-8"));
for(int i = 0; i < list.size(); i++){
try {
bw.write(list.get(i));
bw.newLine();
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
bw.close();
message = "导入成功";
} catch (FileNotFoundException e) {
System.out.println("未找到指定路径的文件!");
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
return returnMap;
}
/**
* 日志下载跳转
*
* @return
*/
@RequestMapping(params = "golog")
public void golog(HttpServletRequest request,HttpServletResponse response)throws Exception {
String filename = request.getParameter("filename")+".txt";
//创建要下载的文件对象
String path = request.getSession().getServletContext().getRealPath("upload/log/"+ filename);
//设置文件MIME类型
response.setContentType(request.getSession().getServletContext().getMimeType(filename));
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
//System.out.println(fullFileName);
//读取文件
InputStream in = new FileInputStream(path);
OutputStream out = response.getOutputStream();
//写文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
out.close();
}
导入Excel
最新推荐文章于 2021-11-08 15:07:27 发布