合并分支 'dev_third_stage' 到 'master'

Dev third stage

查看合并请求 laws-nio/laws-weilai!106
This commit is contained in:
李雪涛
2022-07-24 17:33:13 +08:00
2 changed files with 291 additions and 12 deletions
@@ -1,7 +1,5 @@
package com.jero.modules.cert.report.service.impl;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ZipUtil;
@@ -30,6 +28,7 @@ import com.jero.modules.cert.template.enums.ControlTypeEnum;
import com.jero.modules.ocr.util.LineHumpUtil;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.project.util.ExcelUtil;
import com.jero.modules.project.util.WordUtil;
import com.jero.modules.split.common.ReadExcel;
import com.jero.modules.system.entity.SysDictItem;
@@ -37,7 +36,6 @@ import com.jero.modules.system.service.ISysDictItemService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.*;
import org.aspectj.util.FileUtil;
@@ -753,6 +751,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
paramsReportExportHistoryEO.setExportTime(new Date());
paramsReportExportHistoryEOService.add(paramsReportExportHistoryEO);
uploadFileio.close();
os.flush();
os.close(); // 后开先关
fis.close(); // 先开后关
@@ -795,7 +794,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
// 查询导出数据
List<Map> allParamsInfoList = queryForExportCustom(paramsReportDetailVO);
Map<String, Object> params = allParamsInfoList.get(0);
HashMap<String, String> params = (HashMap<String, String>) allParamsInfoList.get(0);
String templateUrl = "";
ParamsExportTemplateEO paramsExportTemplateEO = paramsExportTemplateEOService.getById(paramsReportDetailVO.getExportTemplateId());
@@ -807,25 +806,37 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
// 替换模板中的占位符
try {
//下载关联文件内容
List<OSSFile> allRelevFileList = (List<OSSFile>) allParamsInfoList.get(2).get("fileListAll");
if (allRelevFileList != null && !allRelevFileList.isEmpty()) {
allRelevFileList = allRelevFileList.stream().distinct().collect(Collectors.toList());
downLoadFileList(allRelevFileList,fileNowPath + File.separator + "导出文件");
}
// 拿取模板
String repFileName = fileName.replaceAll("/","_");
wordOS = new FileOutputStream(fileNowPath + File.separator + repFileName);
if (CosBootUtil.doesObjectExist(templateUrl)) {
InputStream wordTemplate = CosBootUtil.download(templateUrl);
com.qcloud.cos.utils.IOUtils.copy(wordTemplate, wordOS);
// com.qcloud.cos.utils.IOUtils.copy(wordTemplate, wordOS);
ExcelUtil.variableReplace(wordTemplate, wordOS, params);
}
// 替换占位符内容
TemplateExportParams templateExportParams = new TemplateExportParams();
templateExportParams.setTemplateUrl(fileNowPath + File.separator + repFileName);
Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, params);
// poi方式
// TemplateExportParams templateExportParams = new TemplateExportParams();
// templateExportParams.setTemplateUrl(fileNowPath + File.separator + repFileName);
// Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, params);
OutputStream word = new FileOutputStream(fileNowPath + File.separator + repFileName);
workbook.write(word);
// OutputStream word = new FileOutputStream(fileNowPath + File.separator + repFileName);
// workbook.write(word);
wordOS.flush();
wordOS.close();
word.flush();
word.close();
// word.flush();
// word.close();
// 打包导出压缩包
response.setHeader("Content-Disposition",
@@ -0,0 +1,268 @@
package com.jero.modules.project.util;
import org.apache.commons.io.FileUtils;
import org.docx4j.XmlUtils;
import org.docx4j.dml.*;
import org.docx4j.dml.spreadsheetdrawing.*;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
import org.docx4j.openpackaging.parts.DrawingML.Drawing;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.SpreadsheetML.JaxbSmlPart;
import org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.relationships.Relationship;
import javax.xml.bind.JAXBException;
import javax.xml.bind.util.JAXBResult;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
public class ExcelUtil {
public static void main(String[] args) throws Exception {
String inputfilepath = "D:\\text\\072001自定义模板.xlsx";
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.load(new File(inputfilepath));
WorksheetPart worksheet = pkg.getWorkbookPart().getWorksheet(0);
JAXBResult result = XmlUtils.prepareJAXBResult(worksheet.getJAXBContext());
Map<String, Object> transformParameters = new HashMap<>();
transformParameters.put("NIO 07200103", "dfasdfas");
// InputStream smlPart = pkg.getParts().get(new PartName("/xl/sharedStrings.xml")); // 获取所有sheet
Source xsltSource = new StreamSource(new File("D:\\text\\sharedStrings.xml"));
TransformerFactory transFact = TransformerFactory.newInstance();
Templates cachedXSLT = transFact.newTemplates(xsltSource);
pkg.getWorkbookPart().transform(cachedXSLT,transformParameters,result);
// variableReplace();
// xlsx4JAddImage();
}
public static void xlsx4JAddImage() throws Exception {
String inputfilepath = "D:\\text\\072001自定义模板.xlsx";
String outputfilepath = "D:\\text\\mytest.xlsx";
String imagefilePath = "D:\\text\\jpg2.jpg" ;
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
WorksheetPart worksheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
// Create Drawing part and add to sheet
Drawing drawingPart = new Drawing();
Relationship drawingRel = worksheet.addTargetPart(drawingPart);
// Add anchor XML to worksheet
org.xlsx4j.sml.CTDrawing drawing = org.xlsx4j.jaxb.Context.getsmlObjectFactory().createCTDrawing();
worksheet.getJaxbElement().setDrawing(drawing);
drawing.setId( drawingRel.getId() );
// Create image part and add to Drawing part
BinaryPartAbstractImage imagePart
= BinaryPartAbstractImage.createImagePart(pkg, drawingPart,
FileUtils.readFileToByteArray(new File(imagefilePath) ));
String imageRelID = imagePart.getSourceRelationship().getId();
// Create and set drawing part content
// Take your pick ..
// .. build it using code
drawingPart.setJaxbElement(
buildDrawingPartContentUsingCode(imageRelID));
// .. or build it from an XML string
// drawingPart.setJaxbElement(
// buildDrawingPartContentFromXmlString(imageRelID));
// Save the xlsx
SaveToZipFile saver = new SaveToZipFile(pkg);
saver.save(outputfilepath);
System.out.println("\n\n done .. " + outputfilepath);
}
public static void variableReplace(InputStream is, OutputStream os, HashMap<String, String> mappings) throws Exception {
SpreadsheetMLPackage opcPackagepkg = SpreadsheetMLPackage.load(is);
// Be sure to get the part which actually contains your variables!
JaxbSmlPart smlPart = (JaxbSmlPart)opcPackagepkg.getParts().get(new PartName("/xl/sharedStrings.xml")); // 获取所有sheet
//JaxbSmlPart smlPart = (JaxbSmlPart)opcPackagepkg.getParts().get(new PartName("/xl/worksheets/sheet1.xml"));
smlPart.variableReplace(mappings);
// Save it
opcPackagepkg.save(os);
}
/**
* This code generated using http://webapp.docx4java.org/OnlineDemo/PartsList.html
* "Method 1"
*/
public static CTDrawing buildDrawingPartContentUsingCode(String imageRelID) {
org.docx4j.dml.spreadsheetdrawing.ObjectFactory dmlspreadsheetdrawingObjectFactory = new org.docx4j.dml.spreadsheetdrawing.ObjectFactory();
CTDrawing drawing = dmlspreadsheetdrawingObjectFactory.createCTDrawing();
// JAXBElement<org.docx4j.dml.spreadsheetdrawing.CTDrawing> drawingWrapped = dmlspreadsheetdrawingObjectFactory.createWsDr(drawing);
// Create object for twoCellAnchor
CTTwoCellAnchor twocellanchor = dmlspreadsheetdrawingObjectFactory.createCTTwoCellAnchor();
drawing.getEGAnchor().add( twocellanchor);
// Create object for clientData
CTAnchorClientData anchorclientdata = dmlspreadsheetdrawingObjectFactory.createCTAnchorClientData();
twocellanchor.setClientData(anchorclientdata);
// Create object for pic
CTPicture picture = dmlspreadsheetdrawingObjectFactory.createCTPicture();
twocellanchor.setPic(picture);
org.docx4j.dml.ObjectFactory dmlObjectFactory = new org.docx4j.dml.ObjectFactory();
// Create object for blipFill
CTBlipFillProperties blipfillproperties = dmlObjectFactory.createCTBlipFillProperties();
picture.setBlipFill(blipfillproperties);
// Create object for blip
CTBlip blip = dmlObjectFactory.createCTBlip();
blipfillproperties.setBlip(blip);
blip.setCstate(org.docx4j.dml.STBlipCompression.NONE);
blip.setEmbed( imageRelID );
// Create object for extLst
CTOfficeArtExtensionList officeartextensionlist = dmlObjectFactory.createCTOfficeArtExtensionList();
blip.setExtLst(officeartextensionlist);
// Create object for ext
CTOfficeArtExtension officeartextension = dmlObjectFactory.createCTOfficeArtExtension();
officeartextensionlist.getExt().add( officeartextension);
officeartextension.setUri( "{28A0092B-C50C-407E-A947-70E740481C1C}");
blip.setLink( "");
// Create object for stretch
CTStretchInfoProperties stretchinfoproperties = dmlObjectFactory.createCTStretchInfoProperties();
blipfillproperties.setStretch(stretchinfoproperties);
// Create object for fillRect
CTRelativeRect relativerect = dmlObjectFactory.createCTRelativeRect();
stretchinfoproperties.setFillRect(relativerect);
relativerect.setR( new Integer(0) );
relativerect.setT( new Integer(0) );
relativerect.setL( new Integer(0) );
relativerect.setB( new Integer(0) );
// Create object for spPr
CTShapeProperties shapeproperties = dmlObjectFactory.createCTShapeProperties();
picture.setSpPr(shapeproperties);
// Create object for xfrm
CTTransform2D transform2d = dmlObjectFactory.createCTTransform2D();
shapeproperties.setXfrm(transform2d);
transform2d.setRot( new Integer(0) );
// Create object for off
CTPoint2D point2d = dmlObjectFactory.createCTPoint2D();
transform2d.setOff(point2d);
point2d.setY( 0 );
point2d.setX( 0 );
// Create object for ext
CTPositiveSize2D positivesize2d = dmlObjectFactory.createCTPositiveSize2D();
transform2d.setExt(positivesize2d);
positivesize2d.setCx( 714375 );
positivesize2d.setCy( 714375 );
// Create object for prstGeom
CTPresetGeometry2D presetgeometry2d = dmlObjectFactory.createCTPresetGeometry2D();
shapeproperties.setPrstGeom(presetgeometry2d);
// Create object for avLst
CTGeomGuideList geomguidelist = dmlObjectFactory.createCTGeomGuideList();
presetgeometry2d.setAvLst(geomguidelist);
presetgeometry2d.setPrst(org.docx4j.dml.STShapeType.RECT);
// Create object for nvPicPr
CTPictureNonVisual picturenonvisual = dmlspreadsheetdrawingObjectFactory.createCTPictureNonVisual();
picture.setNvPicPr(picturenonvisual);
// Create object for cNvPr
CTNonVisualDrawingProps nonvisualdrawingprops = dmlObjectFactory.createCTNonVisualDrawingProps();
picturenonvisual.setCNvPr(nonvisualdrawingprops);
nonvisualdrawingprops.setDescr( "");
nonvisualdrawingprops.setName( "Picture 1");
nonvisualdrawingprops.setId( 2 );
// Create object for cNvPicPr
CTNonVisualPictureProperties nonvisualpictureproperties = dmlObjectFactory.createCTNonVisualPictureProperties();
picturenonvisual.setCNvPicPr(nonvisualpictureproperties);
// Create object for picLocks
CTPictureLocking picturelocking = dmlObjectFactory.createCTPictureLocking();
nonvisualpictureproperties.setPicLocks(picturelocking);
picture.setMacro( "");
// Create object for to
CTMarker marker = dmlspreadsheetdrawingObjectFactory.createCTMarker();
twocellanchor.setTo(marker);
marker.setCol(1);
marker.setColOff( 104775 );
marker.setRow(3);
marker.setRowOff( 142875 );
// Create object for from
CTMarker marker2 = dmlspreadsheetdrawingObjectFactory.createCTMarker();
twocellanchor.setFrom(marker2);
marker2.setCol(0);
marker2.setColOff( 0 );
marker2.setRow(0);
marker2.setRowOff( 0 );
twocellanchor.setEditAs(STEditAs.fromValue("name"));
// return drawingWrapped;
return drawing;
}
/**
* This code generated using http://webapp.docx4java.org/OnlineDemo/PartsList.html
* "Method 2"
*/
public static CTDrawing buildDrawingPartContentFromXmlString(String imageRelID) throws JAXBException {
String openXML = "<xdr:wsDr xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:a14=\"http://schemas.microsoft.com/office/drawing/2010/main\" xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
+ "<xdr:twoCellAnchor editAs=\"oneCell\">"
+ "<xdr:from>"
+ "<xdr:col>0</xdr:col>"
+ "<xdr:colOff>0</xdr:colOff>"
+ "<xdr:row>0</xdr:row>"
+ "<xdr:rowOff>0</xdr:rowOff>"
+"</xdr:from>"
+ "<xdr:to>"
+ "<xdr:col>1</xdr:col>"
+ "<xdr:colOff>104775</xdr:colOff>"
+ "<xdr:row>3</xdr:row>"
+ "<xdr:rowOff>142875</xdr:rowOff>"
+"</xdr:to>"
+ "<xdr:pic>"
+ "<xdr:nvPicPr>"
+ "<xdr:cNvPr id=\"2\" name=\"Picture 1\"/>"
+ "<xdr:cNvPicPr>"
+ "<a:picLocks noChangeAspect=\"1\"/>"
+"</xdr:cNvPicPr>"
+"</xdr:nvPicPr>"
+ "<xdr:blipFill>"
+ "<a:blip r:embed=\"" + imageRelID + "\">"
+ "<a:extLst>"
+ "<a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">"
+ "<a14:useLocalDpi val=\"0\"/>"
+"</a:ext>"
+"</a:extLst>"
+"</a:blip>"
+ "<a:stretch>"
+ "<a:fillRect/>"
+"</a:stretch>"
+"</xdr:blipFill>"
+ "<xdr:spPr>"
+ "<a:xfrm>"
+ "<a:off x=\"0\" y=\"0\"/>"
+ "<a:ext cx=\"714375\" cy=\"714375\"/>"
+"</a:xfrm>"
+ "<a:prstGeom prst=\"rect\">"
+ "<a:avLst/>"
+"</a:prstGeom>"
+"</xdr:spPr>"
+"</xdr:pic>"
+ "<xdr:clientData/>"
+"</xdr:twoCellAnchor>"
+"</xdr:wsDr>";
return (CTDrawing) XmlUtils.unwrap(
XmlUtils.unmarshalString(openXML));
}
}