Commit f089ede8 authored by liuxinben's avatar liuxinben

批量导入取消邮寄的信息

parent b63037ef
......@@ -3,6 +3,7 @@ package com.yxproject.start.api;
import com.yxproject.start.dto.ReadCardDto;
import com.yxproject.start.entity.PersonPostEntity;
import com.yxproject.start.service.PersonPostService;
import com.yxproject.start.utils.ExportExcel;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
......@@ -10,15 +11,22 @@ import org.apache.log4j.MDC;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
......@@ -90,13 +98,76 @@ public class PersonPostApi {
String name=noteMap.get("name").toString();
personPostService.cancelPostByAcceptNo(uploadNo,note,name);
}
return true;
} catch (Exception e) {
logger.error("备注信息:" + jsonArray);
logger.error("Exception 更改备注信息异常", e);
return false;
}
return true;
}
/**
* 导入证件追踪查询
*
* @return
*/
@RequestMapping("uploadCancelPost")
@Transactional
public boolean uploadCancelPost(HttpServletRequest requ) {
String remoteAddr = requ.getRemoteAddr();
MDC.put("ip", remoteAddr);
String filename = "";
String name = requ.getParameter("name");
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置缓冲区的大小为100KB,如果不指定,那么缓冲区的大小默认是10KB
factory.setSizeThreshold(1024 * 100);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
// 设置上传单个文件的大小的最大值,目前是设置为1024*1024*10字节,也就是10MB
upload.setFileSizeMax(1024 * 1024 * 10);
// 设置上传文件总量的最大值,最大值=同时上传的多个文件的大小的最大值的和,目前设置为4000MB
upload.setSizeMax(1024 * 1024 * 4000);
// 将普通属性存入map中,之后调用
List<Map<String, Object>> list = new ArrayList<>();
//将当前上下文初始化给 CommonsMutipartResolver (多部分解析器)
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
requ.getServletContext());
// 判断是否是多数据段提交格式
if (multipartResolver.isMultipart(requ)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) requ;
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
List<MultipartFile> fileRows = multiRequest.getFiles(iter.next().toString());
for (MultipartFile item : fileRows) {
// 如果fileitem中封装的是普通输入项的数据
// 如果fileitem中封装的是上传文件
// 得到上传的文件名称
filename = item.getOriginalFilename();
File file = null;
try {
file = File.createTempFile("prefix", "_" + item.getOriginalFilename());
item.transferTo(file);
list = ExportExcel.rearXlsAndXlsx(file, filename);
for (int i = 0; i < list.size(); i++) {
Map<String, Object> noteMap = list.get(i);
String uploadNo = noteMap.get("身份证受理号").toString();
String note = noteMap.get("审核错误类型").toString();
personPostService.cancelPostByAcceptNo(uploadNo, note, name);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
}
return false;
}
/**
* 按条件查询个人邮寄信息
*
......
package com.yxproject.start.utils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.util.ObjectUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
import static com.yxproject.start.utils.YXStringUtils.getCurrentDate2String;
......@@ -379,4 +377,71 @@ public class ExportExcel {
}
/**
* 解析excel
* @param file
* @return
*/
public static List<Map<String,Object>> rearXlsAndXlsx(File file, String filePath){
Workbook wb = readExcel(file,filePath);
List<Map<String,Object>> outerList= new ArrayList<>();
if (wb != null) {
int sheetSize = wb.getNumberOfSheets();
for (int index = 0; index < sheetSize; index++) {
// 每个页签创建一个Sheet对象
Sheet sheet = wb.getSheetAt(index);
int rowNum = sheet.getLastRowNum();
int headerNum = sheet.getRow(2).getLastCellNum();
String[] s = new String[headerNum];
for (int o = 0; o < headerNum; o++){
Cell headerCell = sheet.getRow(0).getCell(o);
headerCell.setCellType(CellType.STRING);
s[o] = headerCell.getStringCellValue();
}
// sheet.getRows()返回该页的总行数
for (int nowRowNum = 3; nowRowNum < rowNum-2 ; nowRowNum++) {
Map<String,Object> map = new HashMap<>();
Row row = sheet.getRow(nowRowNum);
// sheet.getColumns()返回该页的总列数
int columnNum = row.getLastCellNum();
for (int nowColumnNum = 0; nowColumnNum < columnNum; nowColumnNum++) {
Cell columnCell = sheet.getRow(nowRowNum).getCell(nowColumnNum);
columnCell.setCellType(CellType.STRING);
String cellinfo = columnCell.getStringCellValue();
if(cellinfo.isEmpty() || cellinfo.equals("")){
continue;
}else {
map.put(s[nowColumnNum],cellinfo);
}
}
if (!ObjectUtils.isEmpty(map)){
outerList.add(map);
}
}
}
}
return outerList;
}
private static Workbook readExcel(File file,String filePath){
if(filePath==null){
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
try {
@SuppressWarnings("resource")
InputStream is = new FileInputStream(file);
if(".xls".equals(extString)){
return new HSSFWorkbook(is);
}else if(".xlsx".equals(extString)){
throw new FileNotFoundException();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment