Commit cedc0461 authored by 廖旭伟's avatar 廖旭伟

证照目录excel导入模板增加对图片的导入

parent 037a27d2
......@@ -8,9 +8,11 @@ import com.mortals.xhx.module.record.model.ApplyLogEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
......@@ -153,6 +155,7 @@ public class ImportExcelUtil {
}
}
log.info("have data col:{}", colNum);
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = startRow; i <= rowNum; i++) {
row = sheet.getRow(i);
......@@ -203,6 +206,59 @@ public class ImportExcelUtil {
return returnList;
}
public static Map<String, PictureData> getPictures(HSSFSheet sheet) {
Map<String, PictureData> map = new HashMap();
if (sheet.getDrawingPatriarch() == null) {
return null;
} else {
List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
Iterator var4 = list.iterator();
while(var4.hasNext()) {
HSSFShape shape = (HSSFShape)var4.next();
if (shape instanceof HSSFPicture) {
HSSFPicture picture = (HSSFPicture)shape;
HSSFClientAnchor cAnchor = (HSSFClientAnchor)picture.getAnchor();
HSSFPictureData pdata = picture.getPictureData();
String key = cAnchor.getRow1() + "-" + cAnchor.getCol1();
map.put(key, pdata);
}
}
return map;
}
}
public static Map<String, PictureData> getPictures(XSSFSheet sheet) {
Map<String, PictureData> map = new HashMap();
List<POIXMLDocumentPart> list = sheet.getRelations();
Iterator var4 = list.iterator();
while(true) {
POIXMLDocumentPart part;
do {
if (!var4.hasNext()) {
return map;
}
part = (POIXMLDocumentPart)var4.next();
} while(!(part instanceof XSSFDrawing));
XSSFDrawing drawing = (XSSFDrawing)part;
List<XSSFShape> shapes = drawing.getShapes();
Iterator var8 = shapes.iterator();
while(var8.hasNext()) {
XSSFShape shape = (XSSFShape)var8.next();
XSSFPicture picture = (XSSFPicture)shape;
XSSFClientAnchor anchor = picture.getPreferredSize();
CTMarker marker = anchor.getFrom();
String key = marker.getRow() + "-" + marker.getCol();
map.put(key, picture.getPictureData());
}
}
}
public static void main(String[] args){
String filepath = "D:\\mortals\\app\\data\\cpm\\excel\\test3.xls";
try {
......
package com.mortals.xhx.common.utils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.PictureData;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
public class ReadExcelPictureUtil {
/**
* 每个单元格单张图片
* @param uploadFile
* @return
* @throws IOException
*/
public static Map<String, PictureData> readExcelPictureMap( MultipartFile uploadFile) throws IOException {
//获取所有图片
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(uploadFile.getInputStream());// 工作簿
HSSFSheet sheet = hssfWorkbook.getSheetAt(0);// 工作表
// int lastRowNum = sheet.getLastRowNum();// 获取最后一行序号,从零开始
Map<String, PictureData> picMap = new HashMap<>();// 存储图片信息和坐标
// List<HSSFShape> list = Optional.ofNullable(sheet.getDrawingPatriarch().getChildren()).orElse(null);
List<HSSFShape> list = Optional.ofNullable(sheet).map(HSSFSheet::getDrawingPatriarch).map(HSSFPatriarch::getChildren).orElse(null);
if (list != null && list.size() > 0) {// 处理获取图片信息和坐标
list = list.stream().filter(item -> item instanceof HSSFPicture).collect(Collectors.toList());// 过滤出图片的数据
for (HSSFShape hssfShape : list) {
HSSFPicture hSSFPicture = (HSSFPicture) hssfShape;
HSSFClientAnchor hSSFClientAnchor = (HSSFClientAnchor) hSSFPicture.getAnchor();
PictureData pictureData = hSSFPicture.getPictureData();
if(pictureData!=null) {
String point = hSSFClientAnchor.getRow1() + "," + hSSFClientAnchor.getCol1();
picMap.put(point, pictureData);
}
}
}
return picMap;//图片map集合
}
//将上面所有的图片map集合循环,每次循环调用这个方法,进行每条数据的多张图片进行保存,返回图片的路径集合
public static Map<String, String> savePicture(String root, Map<String, PictureData> picMap){
Map<String,String> fileMap = new HashMap<>();
Iterator<Map.Entry<String, PictureData>> it = picMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, PictureData> entry = it.next();
//System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
PictureData pictureData = entry.getValue();
File picDirF = null;
String imageName = new Date().getTime() + ".png";
String imagePath = "/file/uploadfile/"+imageName;
String filePath = root + imagePath;
FileOutputStream fout;
try {
fout = new FileOutputStream(filePath);
fout.write(pictureData.getData());
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
fileMap.put(entry.getKey(),imagePath);
}
return fileMap;//返回图片路径集合
}
public static void main(String[] args) {
String filepath = "D:\\mortals\\app\\data\\cpm\\excel\\test3.xls";
try {
InputStream is = new FileInputStream(filepath);
//获取所有图片
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);// 工作簿
HSSFSheet sheet = hssfWorkbook.getSheetAt(0);// 工作表
// int lastRowNum = sheet.getLastRowNum();// 获取最后一行序号,从零开始
Map<String, String> picMap = new HashMap<>();// 存储图片信息和坐标
// List<HSSFShape> list = Optional.ofNullable(sheet.getDrawingPatriarch().getChildren()).orElse(null);
List<HSSFShape> list = Optional.ofNullable(sheet).map(HSSFSheet::getDrawingPatriarch).map(HSSFPatriarch::getChildren).orElse(null);
if (list != null && list.size() > 0) {// 处理获取图片信息和坐标
list = list.stream().filter(item -> item instanceof HSSFPicture).collect(Collectors.toList());// 过滤出图片的数据
for (HSSFShape hssfShape : list) {
HSSFPicture hSSFPicture = (HSSFPicture) hssfShape;
HSSFClientAnchor hSSFClientAnchor = (HSSFClientAnchor) hSSFPicture.getAnchor();
PictureData pictureData = hSSFPicture.getPictureData();
if (pictureData != null) {
String point = hSSFClientAnchor.getRow1() + "," + hSSFClientAnchor.getCol1();
String imageName = new Date().getTime() + ".png";
String imagePath = "/file/uploadfile/"+imageName;
String filePath = "D:/mortals/app/data/cpm" + imagePath;
FileOutputStream fout;
try {
fout = new FileOutputStream(filePath);
fout.write(pictureData.getData());
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(imagePath);
picMap.put(point, imagePath);
}
}
}
System.out.println(picMap);
}catch (Exception e){
}
}
}
......@@ -45,11 +45,12 @@ public interface ApplyLogService extends ICRUDService<ApplyLogEntity,Long>{
/**
* 批量导入
* @param dataList
* @param fileMap 图片
* @param catalogId
* @param context
* @return
*/
String importData(List<Map<Integer, Object>> dataList,Long catalogId,Context context) throws AppException;
String importData(List<Map<Integer, Object>> dataList,Map<String,String> fileMap,Long catalogId,Context context) throws AppException;
/**
* api接口提交数据
......
......@@ -376,7 +376,7 @@ public class ApplyLogServiceImpl extends AbstractCRUDServiceImpl<ApplyLogDao, Ap
}
@Override
public String importData(List<Map<Integer, Object>> dataList, Long catalogId, Context context) throws AppException {
public String importData(List<Map<Integer, Object>> dataList, Map<String,String> fileMap, Long catalogId, Context context) throws AppException {
if(catalogId==null){
throw new AppException("目录ID不能为空");
}
......@@ -387,7 +387,7 @@ public class ApplyLogServiceImpl extends AbstractCRUDServiceImpl<ApplyLogDao, Ap
JSONObject formContent = JSONObject.parseObject(catalog.getFormContent());
JSONArray formList = formContent.getJSONArray("list");
Map<Integer, Object> header = dataList.get(0);
Map<Integer, Object> header = dataList.get(0); //表头
List<ApplyLogEntity> applyLogList = new ArrayList<>();
for (int r=1;r<dataList.size();r++){
Map<Integer, Object> row = dataList.get(r);
......@@ -417,12 +417,21 @@ public class ApplyLogServiceImpl extends AbstractCRUDServiceImpl<ApplyLogDao, Ap
JSONObject formJson = new JSONObject();
int formSize = row.size();
for(int i = 11;i<formSize;i++) {
formJson.put(String.valueOf(header.get(i)), String.valueOf(row.get(i)));
String key = String.valueOf(header.get(i));
String value = String.valueOf(row.get(i));
if (key.indexOf("@image") != -1){
int rowNum = r + 1; //excel表头是从第二行开始读,索引需要加1
String imageKey = rowNum + "," + i;
String imagePath = fileMap.get(imageKey);
if(StringUtils.isNotEmpty(imagePath)) {
value = imagePath;
}
}
formJson.put(key, value);
for (int j = 0; j < formList.size(); j++) {
JSONObject jsonObject = formList.getJSONObject(j);
if(jsonObject.getString("_id").equals(String.valueOf(header.get(i)))){
jsonObject.put("value",String.valueOf(row.get(i)));
jsonObject.put("value",value);
break;
}
}
......@@ -491,7 +500,7 @@ public class ApplyLogServiceImpl extends AbstractCRUDServiceImpl<ApplyLogDao, Ap
}
String imageName = new Date().getTime() + ".png";
String imagePath = "/file/uploadfile/"+imageName;
String filePath = this.filePath.endsWith("/") ? this.filePath : this.filePath + imagePath;
String filePath = this.filePath.endsWith("/") ? this.filePath : this.filePath + "/" + imagePath;
try{
ImageBase64.convertImg(base64.trim(),filePath);
formMap.put(key,imagePath);
......
package com.mortals.xhx.module.record.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.util.HttpUtil;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.key.ParamKey;
import com.mortals.xhx.module.certificate.model.CertificateCatalogEntity;
import com.mortals.xhx.module.certificate.model.CertificateClassifyEntity;
import com.mortals.xhx.module.certificate.model.CertificateClassifyQuery;
......@@ -9,9 +11,7 @@ import com.mortals.xhx.module.certificate.service.CertificateCatalogService;
import com.mortals.xhx.module.certificate.service.CertificateClassifyService;
import com.mortals.xhx.module.record.dao.PrintLogDao;
import com.mortals.xhx.module.record.dao.RetainLogDao;
import com.mortals.xhx.module.record.model.ApplyLogEntity;
import com.mortals.xhx.module.record.model.PrintLogEntity;
import com.mortals.xhx.module.record.model.RetainLogEntity;
import com.mortals.xhx.module.record.model.*;
import com.mortals.xhx.module.record.service.PrintLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -20,7 +20,6 @@ import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.record.dao.PrintWaitQueueDao;
import com.mortals.xhx.module.record.model.PrintWaitQueueEntity;
import com.mortals.xhx.module.record.service.PrintWaitQueueService;
import java.util.Date;
......@@ -56,6 +55,8 @@ public class PrintWaitQueueServiceImpl extends AbstractCRUDServiceImpl<PrintWait
private CertificateCatalogService certificateCatalogService;
@Autowired
private CertificateClassifyService certificateClassifyService;
@Autowired
private ParamService paramService;
@Override
protected void findAfter(PrintWaitQueueEntity entity, Context context, List<PrintWaitQueueEntity> list) throws AppException {
......@@ -98,7 +99,7 @@ public class PrintWaitQueueServiceImpl extends AbstractCRUDServiceImpl<PrintWait
waitQueueEntity.setPreviewUrl(applyLogEntity.getPreviewUrl());
waitQueueEntity.setCertificateUrl(applyLogEntity.getCertificateUrl());
waitQueueEntity.setPrintStatus(YesNoEnum.NO.getValue());
waitQueueEntity.setTotal(1);
waitQueueEntity.setTotal(paramService.getParamIntValue(ParamKey.ALLOW_PRINT_MAX_COUNT));
waitQueueEntity.setCreateUserId(applyLogEntity.getCreateUserId());
waitQueueEntity.setCreateTime(new Date());
return waitQueueEntity;
......@@ -110,15 +111,19 @@ public class PrintWaitQueueServiceImpl extends AbstractCRUDServiceImpl<PrintWait
if(waitQueueEntity==null){
throw new AppException("数据不存在或已修改");
}
PrintWaitQueueEntity data = new PrintWaitQueueEntity();
PrintWaitQueueQuery data = new PrintWaitQueueQuery();
data.setId(id);
data.setPrintStatus(YesNoEnum.YES.getValue());
if(printStatus==PrintStatus.SUCCESS.getValue()){
int total = waitQueueEntity.getTotal();
total--;
doPrintSuccess(waitQueueEntity);
data.setPrintStatus(YesNoEnum.YES.getValue());
if(total==0){
data.setPrintStatus(YesNoEnum.YES.getValue());
}
data.setTotalIncrement(-1);
}else {
doPrintFail(waitQueueEntity,statusRemark);
data.setPrintStatus(-1);
//data.setPrintStatus(-1);
}
data.setUpdateTime(new Date());
dao.update(data);
......
......@@ -9,6 +9,8 @@ import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.utils.ImportExcelUtil;
import com.mortals.xhx.common.utils.ReadExcelPictureUtil;
import org.apache.poi.ss.usermodel.PictureData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -42,8 +44,8 @@ import static com.mortals.framework.ap.SysConstains.*;
@RequestMapping("apply/log")
public class ApplyLogController extends BaseCRUDJsonBodyMappingController<ApplyLogService,ApplyLogEntity,Long> {
@Autowired
private ParamService paramService;
@Value("${upload.path}")
private String filePath;
public ApplyLogController(){
super.setModuleDesc( "证照申请");
......@@ -138,7 +140,9 @@ public class ApplyLogController extends BaseCRUDJsonBodyMappingController<ApplyL
try {
String fileName = file.getOriginalFilename();
List<Map<Integer, Object>> returnList = ImportExcelUtil.readExcelContent(fileName,file.getInputStream(),1,null,null);
String message = this.service.importData(returnList, catalogId, context);
Map<String, PictureData> pictureDataMap = ReadExcelPictureUtil.readExcelPictureMap(file);
Map<String,String> fileMap = ReadExcelPictureUtil.savePicture(this.filePath,pictureDataMap);
String message = this.service.importData(returnList, fileMap, catalogId, context);
model.put("message_info", message);
this.recordSysLog(this.request, busiDesc + " 【成功】");
} catch (Exception var13) {
......
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