认证-上报库-自定义导出excel模板-图片处理
This commit is contained in:
+1
@@ -53,6 +53,7 @@ public interface IParamsReportDetailEOService extends IService<ParamsReportDetai
|
|||||||
// 自定义导出-word模板
|
// 自定义导出-word模板
|
||||||
void exportCustomWord(ParamsReportDetailVO paramsReportDetailVO, HttpServletResponse response, HttpServletRequest request);
|
void exportCustomWord(ParamsReportDetailVO paramsReportDetailVO, HttpServletResponse response, HttpServletRequest request);
|
||||||
|
|
||||||
|
// 自定义导出-excel模板
|
||||||
void exportCustomExcel(ParamsReportDetailVO paramsReportDetailVO, HttpServletResponse response, HttpServletRequest request);
|
void exportCustomExcel(ParamsReportDetailVO paramsReportDetailVO, HttpServletResponse response, HttpServletRequest request);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-1
@@ -36,11 +36,14 @@ import com.jero.modules.system.service.ISysDictItemService;
|
|||||||
import org.apache.commons.lang3.ObjectUtils;
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.poi.hssf.util.HSSFColor;
|
import org.apache.poi.hssf.util.HSSFColor;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||||
import org.apache.poi.util.IOUtils;
|
import org.apache.poi.util.IOUtils;
|
||||||
import org.apache.poi.xssf.usermodel.*;
|
import org.apache.poi.xssf.usermodel.*;
|
||||||
import org.aspectj.util.FileUtil;
|
import org.aspectj.util.FileUtil;
|
||||||
import org.docx4j.Docx4J;
|
import org.docx4j.Docx4J;
|
||||||
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
||||||
|
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
|
||||||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@@ -795,6 +798,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
|
|||||||
// 查询导出数据
|
// 查询导出数据
|
||||||
List<Map> allParamsInfoList = queryForExportCustom(paramsReportDetailVO);
|
List<Map> allParamsInfoList = queryForExportCustom(paramsReportDetailVO);
|
||||||
HashMap<String, String> params = (HashMap<String, String>) allParamsInfoList.get(0);
|
HashMap<String, String> params = (HashMap<String, String>) allParamsInfoList.get(0);
|
||||||
|
List<Map<String, Object>> imgListAll = (List<Map<String, Object>>) allParamsInfoList.get(1).get("imgListAll");
|
||||||
|
|
||||||
String templateUrl = "";
|
String templateUrl = "";
|
||||||
ParamsExportTemplateEO paramsExportTemplateEO = paramsExportTemplateEOService.getById(paramsReportDetailVO.getExportTemplateId());
|
ParamsExportTemplateEO paramsExportTemplateEO = paramsExportTemplateEOService.getById(paramsReportDetailVO.getExportTemplateId());
|
||||||
@@ -820,7 +824,14 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl<ParamsReportDet
|
|||||||
InputStream wordTemplate = CosBootUtil.download(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);
|
// 替换占位符内容
|
||||||
|
// docx4j方式
|
||||||
|
InputStream excelTemplate = CosBootUtil.download(templateUrl);
|
||||||
|
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.load(wordTemplate);
|
||||||
|
Workbook workbook = WorkbookFactory.create(excelTemplate);
|
||||||
|
ExcelUtil.replacePictureBatchPOI(workbook, pkg, imgListAll);
|
||||||
|
ExcelUtil.variableReplace(pkg, params);
|
||||||
|
pkg.save(wordOS);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+371
-44
@@ -1,50 +1,384 @@
|
|||||||
package com.jero.modules.project.util;
|
package com.jero.modules.project.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
import org.docx4j.XmlUtils;
|
import org.docx4j.XmlUtils;
|
||||||
import org.docx4j.dml.*;
|
import org.docx4j.dml.*;
|
||||||
import org.docx4j.dml.spreadsheetdrawing.*;
|
import org.docx4j.dml.spreadsheetdrawing.*;
|
||||||
import org.docx4j.openpackaging.io.SaveToZipFile;
|
import org.docx4j.openpackaging.io.SaveToZipFile;
|
||||||
|
import org.docx4j.openpackaging.io3.stores.PartStore;
|
||||||
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
|
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
|
||||||
import org.docx4j.openpackaging.parts.DrawingML.Drawing;
|
import org.docx4j.openpackaging.parts.DrawingML.Drawing;
|
||||||
|
import org.docx4j.openpackaging.parts.Part;
|
||||||
import org.docx4j.openpackaging.parts.PartName;
|
import org.docx4j.openpackaging.parts.PartName;
|
||||||
|
import org.docx4j.openpackaging.parts.Parts;
|
||||||
import org.docx4j.openpackaging.parts.SpreadsheetML.JaxbSmlPart;
|
import org.docx4j.openpackaging.parts.SpreadsheetML.JaxbSmlPart;
|
||||||
|
import org.docx4j.openpackaging.parts.SpreadsheetML.WorkbookPart;
|
||||||
import org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart;
|
import org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart;
|
||||||
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
|
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
|
||||||
|
import org.docx4j.openpackaging.parts.relationships.Namespaces;
|
||||||
|
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
|
||||||
import org.docx4j.relationships.Relationship;
|
import org.docx4j.relationships.Relationship;
|
||||||
|
import org.xlsx4j.sml.Cell;
|
||||||
|
import org.xlsx4j.sml.SheetData;
|
||||||
|
import org.xlsx4j.sml.Worksheet;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
import javax.xml.bind.util.JAXBResult;
|
import java.awt.image.BufferedImage;
|
||||||
import javax.xml.transform.Source;
|
import java.io.ByteArrayInputStream;
|
||||||
import javax.xml.transform.Templates;
|
|
||||||
import javax.xml.transform.TransformerFactory;
|
|
||||||
import javax.xml.transform.stream.StreamSource;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class ExcelUtil {
|
public class ExcelUtil {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
String inputfilepath = "D:\\text\\072001自定义模板.xlsx";
|
String inputfilepath = "D:\\text\\072001自定义模板.xlsx";
|
||||||
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.load(new File(inputfilepath));
|
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.load(new File(inputfilepath));
|
||||||
WorksheetPart worksheet = pkg.getWorkbookPart().getWorksheet(0);
|
// .createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
|
||||||
JAXBResult result = XmlUtils.prepareJAXBResult(worksheet.getJAXBContext());
|
JaxbSmlPart smlPart = (JaxbSmlPart)pkg.getParts().get(new PartName("/xl/sharedStrings.xml")); // 获取所有sheet中的文本
|
||||||
|
Parts parts = pkg.getParts();
|
||||||
|
System.out.println("mmmmmmm");
|
||||||
|
|
||||||
|
/*JAXBResult result = XmlUtils.prepareJAXBResult(worksheet.getJAXBContext());
|
||||||
Map<String, Object> transformParameters = new HashMap<>();
|
Map<String, Object> transformParameters = new HashMap<>();
|
||||||
transformParameters.put("NIO 07200103", "dfasdfas");
|
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"));
|
Source xsltSource = new StreamSource(new File("D:\\text\\sharedStrings.xml"));
|
||||||
TransformerFactory transFact = TransformerFactory.newInstance();
|
TransformerFactory transFact = TransformerFactory.newInstance();
|
||||||
Templates cachedXSLT = transFact.newTemplates(xsltSource);
|
Templates cachedXSLT = transFact.newTemplates(xsltSource);
|
||||||
|
pkg.getWorkbookPart().transform(cachedXSLT,transformParameters,result);*/
|
||||||
pkg.getWorkbookPart().transform(cachedXSLT,transformParameters,result);
|
|
||||||
|
|
||||||
// variableReplace();
|
// variableReplace();
|
||||||
// xlsx4JAddImage();
|
// xlsx4JAddImage();
|
||||||
|
|
||||||
|
WorkbookPart workbookPart = pkg.getWorkbookPart();
|
||||||
|
WorksheetPart sheet = workbookPart.getWorksheet(0);
|
||||||
|
Worksheet ws = sheet.getContents();
|
||||||
|
SheetData data = ws.getSheetData();
|
||||||
|
System.out.println("mmmmmmm");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replacePictureBatchPOI(Workbook workbook, SpreadsheetMLPackage pkg, List<Map<String, Object>> imgListAll) throws Exception {
|
||||||
|
//获取全部sheet页中表格
|
||||||
|
Sheet sheet=null;
|
||||||
|
int partNameNum = 0;
|
||||||
|
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表
|
||||||
|
sheet = workbook.getSheetAt(i);
|
||||||
|
WorksheetPart worksheetPart = (WorksheetPart) pkg.getParts().get(new PartName("/xl/worksheets/" + sheet.getSheetName().toLowerCase() + ".xml"));
|
||||||
|
partNameNum++;
|
||||||
|
|
||||||
|
//获得数据的总行数
|
||||||
|
int totalRowNum = sheet.getLastRowNum();
|
||||||
|
|
||||||
|
//获得所有数据
|
||||||
|
for (int x = 0; x < totalRowNum; x++) {
|
||||||
|
//获得第x行对象
|
||||||
|
Row row = sheet.getRow(x);
|
||||||
|
if (!(row == null)) {
|
||||||
|
//获得总列数
|
||||||
|
int cellLength = row.getLastCellNum();
|
||||||
|
//如果x行里的所有单元格
|
||||||
|
for (int y = 0; y < cellLength; y++) {
|
||||||
|
|
||||||
|
org.apache.poi.ss.usermodel.Cell cell = row.getCell(y);
|
||||||
|
if(ObjectUtil.isNull(cell)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String cellValue = cell.getStringCellValue();
|
||||||
|
|
||||||
|
for (Map<String, Object> imgMap : imgListAll) {
|
||||||
|
String key = (String) imgMap.get("key");
|
||||||
|
String text = (String) imgMap.get("text");
|
||||||
|
byte[] bytes = (byte[]) imgMap.get("bytes");
|
||||||
|
if (("${" + key + "}").equals(cellValue)) {
|
||||||
|
putImagePOI(worksheetPart, pkg, cell, bytes, sheet.getSheetName().toLowerCase(), String.valueOf(partNameNum));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replacePictureBatch(SpreadsheetMLPackage pkg,List<Map<String, Object>> imgListAll) throws Exception {
|
||||||
|
Parts parts = pkg.getParts();
|
||||||
|
Map<PartName, Part> map = parts.getParts();
|
||||||
|
|
||||||
|
for (Map.Entry<PartName, Part> partMap : map.entrySet()) {
|
||||||
|
int partNameNum = 0;
|
||||||
|
if (partMap.getValue() instanceof WorksheetPart) {
|
||||||
|
String partName = String.valueOf(partMap.getKey());
|
||||||
|
WorksheetPart worksheetPart = (WorksheetPart) partMap.getValue();
|
||||||
|
SheetData sheetData = worksheetPart.getJaxbElement().getSheetData();
|
||||||
|
partNameNum++;
|
||||||
|
|
||||||
|
for (org.xlsx4j.sml.Row r : sheetData.getRow() ) { // 循环表格行
|
||||||
|
for (Cell c : r.getC()) { // 循环表格单元格
|
||||||
|
Object cellValue = c.getV(); // 获取单元格数内容
|
||||||
|
|
||||||
|
if (cellValue instanceof String) {
|
||||||
|
for (Map<String, Object> imgMap : imgListAll) {
|
||||||
|
String key = (String) imgMap.get("key");
|
||||||
|
String text = (String) imgMap.get("text");
|
||||||
|
byte[] bytes = (byte[]) imgMap.get("bytes");
|
||||||
|
|
||||||
|
if (("${" + key + "}").equals(cellValue)) {
|
||||||
|
c.setV(text);
|
||||||
|
putImage(worksheetPart, pkg, c, bytes, partName, String.valueOf(partNameNum));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void putImagePOI(WorksheetPart worksheetPart, SpreadsheetMLPackage pkg, org.apache.poi.ss.usermodel.Cell newCell, byte[] bytes, String sheetName, String partName) throws Exception {
|
||||||
|
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||||||
|
BufferedImage bufferedImage = ImageIO.read(in);
|
||||||
|
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(pkg, worksheetPart, bytes);
|
||||||
|
String imagePartName = imagePart.getPartName().getName();
|
||||||
|
|
||||||
|
PartName drawingPart = new PartName(worksheetPart.getPartName().getName().replace("worksheets/" + sheetName, "drawings/drawing" + partName));
|
||||||
|
Drawing drawing = (Drawing) pkg.getParts().get(drawingPart);
|
||||||
|
int currentId = 0;
|
||||||
|
if (drawing == null) {
|
||||||
|
drawing = new Drawing(drawingPart);
|
||||||
|
drawing.setContents(new org.docx4j.dml.spreadsheetdrawing.CTDrawing());
|
||||||
|
Relationship relationship = worksheetPart.addTargetPart(drawing);
|
||||||
|
org.xlsx4j.sml.CTDrawing smlDrawing = new org.xlsx4j.sml.CTDrawing();
|
||||||
|
smlDrawing.setId(relationship.getId());
|
||||||
|
smlDrawing.setParent(worksheetPart.getContents());
|
||||||
|
worksheetPart.getContents().setDrawing(smlDrawing);
|
||||||
|
} else {
|
||||||
|
currentId = drawing.getContents().getEGAnchor().size();
|
||||||
|
}
|
||||||
|
|
||||||
|
CTTwoCellAnchor anchor = new CTTwoCellAnchor();
|
||||||
|
anchor.setFrom(new CTMarker());
|
||||||
|
anchor.getFrom().setCol(newCell.getColumnIndex());
|
||||||
|
anchor.getFrom().setRow(newCell.getRowIndex());
|
||||||
|
anchor.setTo(new CTMarker());
|
||||||
|
anchor.getTo().setCol(newCell.getColumnIndex() + 1);
|
||||||
|
anchor.getTo().setRow(newCell.getRowIndex() + 1);
|
||||||
|
|
||||||
|
CTPicture picture = new CTPicture();
|
||||||
|
|
||||||
|
CTBlipFillProperties blipFillProperties = new CTBlipFillProperties();
|
||||||
|
blipFillProperties.setStretch(new CTStretchInfoProperties());
|
||||||
|
blipFillProperties.getStretch().setFillRect(new CTRelativeRect());
|
||||||
|
blipFillProperties.setBlip(new CTBlip());
|
||||||
|
blipFillProperties.getBlip().setEmbed("rId" + (currentId + 1));
|
||||||
|
blipFillProperties.getBlip().setCstate(STBlipCompression.NONE);
|
||||||
|
|
||||||
|
picture.setBlipFill(blipFillProperties);
|
||||||
|
|
||||||
|
CTNonVisualDrawingProps nonVisualDrawingProps = new CTNonVisualDrawingProps();
|
||||||
|
nonVisualDrawingProps.setId(currentId + 2);
|
||||||
|
nonVisualDrawingProps.setName(imagePartName.substring(imagePartName.lastIndexOf("/") + 1));
|
||||||
|
nonVisualDrawingProps.setDescr(nonVisualDrawingProps.getName());
|
||||||
|
|
||||||
|
CTNonVisualPictureProperties nonVisualPictureProperties = new CTNonVisualPictureProperties();
|
||||||
|
nonVisualPictureProperties.setPicLocks(new CTPictureLocking());
|
||||||
|
nonVisualPictureProperties.getPicLocks().setNoChangeAspect(true);
|
||||||
|
CTPictureNonVisual nonVisualPicture = new CTPictureNonVisual();
|
||||||
|
|
||||||
|
nonVisualPicture.setCNvPr(nonVisualDrawingProps);
|
||||||
|
nonVisualPicture.setCNvPicPr(nonVisualPictureProperties);
|
||||||
|
|
||||||
|
picture.setNvPicPr(nonVisualPicture);
|
||||||
|
|
||||||
|
CTShapeProperties shapeProperties = new CTShapeProperties();
|
||||||
|
CTTransform2D transform2D = new CTTransform2D();
|
||||||
|
transform2D.setOff(new CTPoint2D());
|
||||||
|
transform2D.setExt(new CTPositiveSize2D());
|
||||||
|
shapeProperties.setXfrm(transform2D);
|
||||||
|
shapeProperties.setPrstGeom(new CTPresetGeometry2D());
|
||||||
|
shapeProperties.getPrstGeom().setPrst(STShapeType.RECT);
|
||||||
|
shapeProperties.getPrstGeom().setAvLst(new CTGeomGuideList());
|
||||||
|
CTPositiveSize2D positivesize2D = new CTPositiveSize2D();
|
||||||
|
transform2D.setExt(positivesize2D);
|
||||||
|
positivesize2D.setCx( XlsxUtils.convertPxToEmu(bufferedImage.getWidth()) );
|
||||||
|
positivesize2D.setCy( XlsxUtils.convertPxToEmu(bufferedImage.getHeight()) );
|
||||||
|
|
||||||
|
picture.setSpPr(shapeProperties);
|
||||||
|
|
||||||
|
anchor.setPic(picture);
|
||||||
|
anchor.setClientData(new CTAnchorClientData());
|
||||||
|
anchor.setEditAs(STEditAs.ONE_CELL);
|
||||||
|
drawing.getContents().getEGAnchor().add(anchor);
|
||||||
|
|
||||||
|
Relationship rel = new Relationship();
|
||||||
|
rel.setId("rId" + (currentId + 1));
|
||||||
|
rel.setType(Namespaces.IMAGE);
|
||||||
|
rel.setTarget(imagePartName);
|
||||||
|
|
||||||
|
drawing.getRelationshipsPart().addRelationship(rel);
|
||||||
|
RelationshipsPart relPart = drawing.getRelationshipsPart();
|
||||||
|
pkg.getParts().remove(relPart.getPartName());
|
||||||
|
pkg.getParts().put(relPart);
|
||||||
|
pkg.getParts().remove(drawing.getPartName());
|
||||||
|
pkg.getParts().put(drawing);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void putImage(WorksheetPart worksheetPart, SpreadsheetMLPackage pkg, Cell newCell, byte[] bytes, String sheetName, String partName) throws Exception {
|
||||||
|
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||||||
|
BufferedImage bufferedImage = ImageIO.read(in);
|
||||||
|
|
||||||
|
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(pkg, worksheetPart, bytes);
|
||||||
|
CTTwoCellAnchor anchor = new CTTwoCellAnchor();
|
||||||
|
anchor.setFrom(new CTMarker());
|
||||||
|
CellReference cellReference = new CellReference("", newCell.getR());
|
||||||
|
anchor.getFrom().setCol(cellReference.getColumn() - 1);
|
||||||
|
anchor.getFrom().setRow(cellReference.getRow() - 1);
|
||||||
|
anchor.setTo(new CTMarker());
|
||||||
|
anchor.getTo().setCol(cellReference.getColumn());
|
||||||
|
anchor.getTo().setRow(cellReference.getRow());
|
||||||
|
|
||||||
|
PartName drawingPart = new PartName(worksheetPart.getPartName().getName().replace("worksheets/" + sheetName, "drawings/drawing" + partName));
|
||||||
|
String imagePartName = imagePart.getPartName().getName();
|
||||||
|
Drawing drawing = (Drawing) pkg.getParts().get(drawingPart);
|
||||||
|
int currentId = 0;
|
||||||
|
if (drawing == null) {
|
||||||
|
drawing = new Drawing(drawingPart);
|
||||||
|
drawing.setContents(new org.docx4j.dml.spreadsheetdrawing.CTDrawing());
|
||||||
|
Relationship relationship = worksheetPart.addTargetPart(drawing);
|
||||||
|
org.xlsx4j.sml.CTDrawing smlDrawing = new org.xlsx4j.sml.CTDrawing();
|
||||||
|
smlDrawing.setId(relationship.getId());
|
||||||
|
smlDrawing.setParent(worksheetPart.getContents());
|
||||||
|
worksheetPart.getContents().setDrawing(smlDrawing);
|
||||||
|
} else {
|
||||||
|
currentId = drawing.getContents().getEGAnchor().size();
|
||||||
|
}
|
||||||
|
|
||||||
|
CTPicture picture = new CTPicture();
|
||||||
|
|
||||||
|
CTBlipFillProperties blipFillProperties = new CTBlipFillProperties();
|
||||||
|
blipFillProperties.setStretch(new CTStretchInfoProperties());
|
||||||
|
blipFillProperties.getStretch().setFillRect(new CTRelativeRect());
|
||||||
|
blipFillProperties.setBlip(new CTBlip());
|
||||||
|
blipFillProperties.getBlip().setEmbed("rId" + (currentId + 1));
|
||||||
|
blipFillProperties.getBlip().setCstate(STBlipCompression.PRINT);
|
||||||
|
|
||||||
|
picture.setBlipFill(blipFillProperties);
|
||||||
|
|
||||||
|
CTNonVisualDrawingProps nonVisualDrawingProps = new CTNonVisualDrawingProps();
|
||||||
|
nonVisualDrawingProps.setId(currentId + 2);
|
||||||
|
nonVisualDrawingProps.setName(imagePartName.substring(imagePartName.lastIndexOf("/") + 1));
|
||||||
|
nonVisualDrawingProps.setDescr(nonVisualDrawingProps.getName());
|
||||||
|
|
||||||
|
CTNonVisualPictureProperties nonVisualPictureProperties = new CTNonVisualPictureProperties();
|
||||||
|
nonVisualPictureProperties.setPicLocks(new CTPictureLocking());
|
||||||
|
nonVisualPictureProperties.getPicLocks().setNoChangeAspect(true);
|
||||||
|
CTPictureNonVisual nonVisualPicture = new CTPictureNonVisual();
|
||||||
|
|
||||||
|
nonVisualPicture.setCNvPr(nonVisualDrawingProps);
|
||||||
|
nonVisualPicture.setCNvPicPr(nonVisualPictureProperties);
|
||||||
|
|
||||||
|
picture.setNvPicPr(nonVisualPicture);
|
||||||
|
|
||||||
|
CTShapeProperties shapeProperties = new CTShapeProperties();
|
||||||
|
CTTransform2D transform2D = new CTTransform2D();
|
||||||
|
transform2D.setOff(new CTPoint2D());
|
||||||
|
transform2D.setExt(new CTPositiveSize2D());
|
||||||
|
shapeProperties.setXfrm(transform2D);
|
||||||
|
shapeProperties.setPrstGeom(new CTPresetGeometry2D());
|
||||||
|
shapeProperties.getPrstGeom().setPrst(STShapeType.RECT);
|
||||||
|
shapeProperties.getPrstGeom().setAvLst(new CTGeomGuideList());
|
||||||
|
CTPositiveSize2D positivesize2D = new CTPositiveSize2D();
|
||||||
|
transform2D.setExt(positivesize2D);
|
||||||
|
positivesize2D.setCx( XlsxUtils.convertPxToEmu(bufferedImage.getWidth()) );
|
||||||
|
positivesize2D.setCy( XlsxUtils.convertPxToEmu(bufferedImage.getHeight()) );
|
||||||
|
|
||||||
|
picture.setSpPr(shapeProperties);
|
||||||
|
|
||||||
|
anchor.setPic(picture);
|
||||||
|
anchor.setClientData(new CTAnchorClientData());
|
||||||
|
|
||||||
|
drawing.getContents().getEGAnchor().add(anchor);
|
||||||
|
|
||||||
|
Relationship rel = new Relationship();
|
||||||
|
rel.setId("rId" + (currentId + 1));
|
||||||
|
rel.setType(Namespaces.IMAGE);
|
||||||
|
rel.setTarget(imagePartName);
|
||||||
|
|
||||||
|
drawing.getRelationshipsPart().addRelationship(rel);
|
||||||
|
RelationshipsPart relPart = drawing.getRelationshipsPart();
|
||||||
|
pkg.getParts().remove(relPart.getPartName());
|
||||||
|
pkg.getParts().put(relPart);
|
||||||
|
pkg.getParts().remove(drawing.getPartName());
|
||||||
|
pkg.getParts().put(drawing);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getWmlTemplateString(JaxbSmlPart smlPart) {
|
||||||
|
String wmlTemplateString = null;
|
||||||
|
PartStore partStore = smlPart.getPackage().getSourcePartStore();
|
||||||
|
String name = smlPart.getPartName().getName();
|
||||||
|
try {
|
||||||
|
InputStream is = partStore.loadPart(
|
||||||
|
name.substring(1));
|
||||||
|
if (is == null) {
|
||||||
|
log.warn(name + " missing from part store");
|
||||||
|
} else {
|
||||||
|
log.info("Lazily unmarshalling " + name);
|
||||||
|
// wmlTemplateString = convertStreamToString(is);
|
||||||
|
|
||||||
|
// This seems to be about 5% faster than the Scanner approach
|
||||||
|
wmlTemplateString = IOUtils.toString(is, "UTF-8");
|
||||||
|
List<String> placeholderList = new ArrayList<>();
|
||||||
|
getAllPlaceholderElementFromObject(wmlTemplateString, 0, placeholderList);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
return wmlTemplateString;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<String> getAllPlaceholderElementFromObject(String wmlTemplateString, int offset, List<String> placeholderList) {
|
||||||
|
|
||||||
|
int startKey = wmlTemplateString.indexOf("${", offset);
|
||||||
|
if (startKey == -1)
|
||||||
|
return null;
|
||||||
|
else {
|
||||||
|
int keyEnd = wmlTemplateString.indexOf('}', startKey);
|
||||||
|
String placeHolder = wmlTemplateString.substring(startKey, keyEnd + 1);
|
||||||
|
if (isPlaceholder(placeHolder)) {
|
||||||
|
placeholderList.add(placeHolder);
|
||||||
|
}
|
||||||
|
return getAllPlaceholderElementFromObject(wmlTemplateString, keyEnd + 1, placeholderList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断字符串是否有${}占位符
|
||||||
|
*
|
||||||
|
* @param str 需要判断的字符串
|
||||||
|
* @return 是否字符串是否有${}占位符
|
||||||
|
* @author liyawei
|
||||||
|
*/
|
||||||
|
private static boolean isPlaceholder(String str) {
|
||||||
|
if (str != null && !str.isEmpty()) {
|
||||||
|
Pattern pattern = Pattern.compile("([$]\\{[A-Za-z0-9./ -]+\\})"); // 识别范围 认证模块专用
|
||||||
|
Matcher m = pattern.matcher(str);
|
||||||
|
return m.find();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void xlsx4JAddImage() throws Exception {
|
public static void xlsx4JAddImage() throws Exception {
|
||||||
@@ -88,17 +422,11 @@ public class ExcelUtil {
|
|||||||
System.out.println("\n\n done .. " + outputfilepath);
|
System.out.println("\n\n done .. " + outputfilepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void variableReplace(InputStream is, OutputStream os, HashMap<String, String> mappings) throws Exception {
|
public static void variableReplace(SpreadsheetMLPackage pkg, HashMap<String, String> mappings) throws Exception {
|
||||||
|
|
||||||
SpreadsheetMLPackage opcPackagepkg = SpreadsheetMLPackage.load(is);
|
|
||||||
|
|
||||||
// Be sure to get the part which actually contains your variables!
|
// 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)pkg.getParts().get(new PartName("/xl/sharedStrings.xml")); // 获取所有sheet
|
||||||
//JaxbSmlPart smlPart = (JaxbSmlPart)opcPackagepkg.getParts().get(new PartName("/xl/worksheets/sheet1.xml"));
|
//JaxbSmlPart smlPart = (JaxbSmlPart)opcPackagepkg.getParts().get(new PartName("/xl/worksheets/sheet1.xml"));
|
||||||
smlPart.variableReplace(mappings);
|
smlPart.variableReplace(mappings);
|
||||||
|
|
||||||
// Save it
|
|
||||||
opcPackagepkg.save(os);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -128,14 +456,14 @@ public class ExcelUtil {
|
|||||||
CTBlip blip = dmlObjectFactory.createCTBlip();
|
CTBlip blip = dmlObjectFactory.createCTBlip();
|
||||||
blipfillproperties.setBlip(blip);
|
blipfillproperties.setBlip(blip);
|
||||||
blip.setCstate(org.docx4j.dml.STBlipCompression.NONE);
|
blip.setCstate(org.docx4j.dml.STBlipCompression.NONE);
|
||||||
blip.setEmbed( imageRelID );
|
blip.setEmbed( imageRelID ); //-------
|
||||||
// Create object for extLst
|
// Create object for extLst
|
||||||
CTOfficeArtExtensionList officeartextensionlist = dmlObjectFactory.createCTOfficeArtExtensionList();
|
CTOfficeArtExtensionList officeartextensionlist = dmlObjectFactory.createCTOfficeArtExtensionList();
|
||||||
blip.setExtLst(officeartextensionlist);
|
blip.setExtLst(officeartextensionlist);
|
||||||
// Create object for ext
|
// Create object for ext
|
||||||
CTOfficeArtExtension officeartextension = dmlObjectFactory.createCTOfficeArtExtension();
|
CTOfficeArtExtension officeartextension = dmlObjectFactory.createCTOfficeArtExtension();
|
||||||
officeartextensionlist.getExt().add( officeartextension);
|
officeartextensionlist.getExt().add( officeartextension);
|
||||||
officeartextension.setUri( "{28A0092B-C50C-407E-A947-70E740481C1C}");
|
officeartextension.setUri( "{28A0092B-C50C-407E-A947-70E740481C1C}"); //------
|
||||||
blip.setLink( "");
|
blip.setLink( "");
|
||||||
// Create object for stretch
|
// Create object for stretch
|
||||||
CTStretchInfoProperties stretchinfoproperties = dmlObjectFactory.createCTStretchInfoProperties();
|
CTStretchInfoProperties stretchinfoproperties = dmlObjectFactory.createCTStretchInfoProperties();
|
||||||
@@ -143,10 +471,10 @@ public class ExcelUtil {
|
|||||||
// Create object for fillRect
|
// Create object for fillRect
|
||||||
CTRelativeRect relativerect = dmlObjectFactory.createCTRelativeRect();
|
CTRelativeRect relativerect = dmlObjectFactory.createCTRelativeRect();
|
||||||
stretchinfoproperties.setFillRect(relativerect);
|
stretchinfoproperties.setFillRect(relativerect);
|
||||||
relativerect.setR( new Integer(0) );
|
relativerect.setR( new Integer(0) ); //------
|
||||||
relativerect.setT( new Integer(0) );
|
relativerect.setT( new Integer(0) ); //------
|
||||||
relativerect.setL( new Integer(0) );
|
relativerect.setL( new Integer(0) ); //------
|
||||||
relativerect.setB( new Integer(0) );
|
relativerect.setB( new Integer(0) ); //------
|
||||||
// Create object for spPr
|
// Create object for spPr
|
||||||
CTShapeProperties shapeproperties = dmlObjectFactory.createCTShapeProperties();
|
CTShapeProperties shapeproperties = dmlObjectFactory.createCTShapeProperties();
|
||||||
picture.setSpPr(shapeproperties);
|
picture.setSpPr(shapeproperties);
|
||||||
@@ -157,13 +485,13 @@ public class ExcelUtil {
|
|||||||
// Create object for off
|
// Create object for off
|
||||||
CTPoint2D point2d = dmlObjectFactory.createCTPoint2D();
|
CTPoint2D point2d = dmlObjectFactory.createCTPoint2D();
|
||||||
transform2d.setOff(point2d);
|
transform2d.setOff(point2d);
|
||||||
point2d.setY( 0 );
|
point2d.setY( 0 ); //------
|
||||||
point2d.setX( 0 );
|
point2d.setX( 0 ); //------
|
||||||
// Create object for ext
|
// Create object for ext
|
||||||
CTPositiveSize2D positivesize2d = dmlObjectFactory.createCTPositiveSize2D();
|
CTPositiveSize2D positivesize2d = dmlObjectFactory.createCTPositiveSize2D();
|
||||||
transform2d.setExt(positivesize2d);
|
transform2d.setExt(positivesize2d);
|
||||||
positivesize2d.setCx( 714375 );
|
positivesize2d.setCx( 714375 ); //------
|
||||||
positivesize2d.setCy( 714375 );
|
positivesize2d.setCy( 714375 ); //------
|
||||||
// Create object for prstGeom
|
// Create object for prstGeom
|
||||||
CTPresetGeometry2D presetgeometry2d = dmlObjectFactory.createCTPresetGeometry2D();
|
CTPresetGeometry2D presetgeometry2d = dmlObjectFactory.createCTPresetGeometry2D();
|
||||||
shapeproperties.setPrstGeom(presetgeometry2d);
|
shapeproperties.setPrstGeom(presetgeometry2d);
|
||||||
@@ -177,9 +505,9 @@ public class ExcelUtil {
|
|||||||
// Create object for cNvPr
|
// Create object for cNvPr
|
||||||
CTNonVisualDrawingProps nonvisualdrawingprops = dmlObjectFactory.createCTNonVisualDrawingProps();
|
CTNonVisualDrawingProps nonvisualdrawingprops = dmlObjectFactory.createCTNonVisualDrawingProps();
|
||||||
picturenonvisual.setCNvPr(nonvisualdrawingprops);
|
picturenonvisual.setCNvPr(nonvisualdrawingprops);
|
||||||
nonvisualdrawingprops.setDescr( "");
|
nonvisualdrawingprops.setDescr( ""); //------
|
||||||
nonvisualdrawingprops.setName( "Picture 1");
|
nonvisualdrawingprops.setName( "Picture 1"); //------
|
||||||
nonvisualdrawingprops.setId( 2 );
|
nonvisualdrawingprops.setId( 2 ); //------
|
||||||
// Create object for cNvPicPr
|
// Create object for cNvPicPr
|
||||||
CTNonVisualPictureProperties nonvisualpictureproperties = dmlObjectFactory.createCTNonVisualPictureProperties();
|
CTNonVisualPictureProperties nonvisualpictureproperties = dmlObjectFactory.createCTNonVisualPictureProperties();
|
||||||
picturenonvisual.setCNvPicPr(nonvisualpictureproperties);
|
picturenonvisual.setCNvPicPr(nonvisualpictureproperties);
|
||||||
@@ -190,18 +518,18 @@ public class ExcelUtil {
|
|||||||
// Create object for to
|
// Create object for to
|
||||||
CTMarker marker = dmlspreadsheetdrawingObjectFactory.createCTMarker();
|
CTMarker marker = dmlspreadsheetdrawingObjectFactory.createCTMarker();
|
||||||
twocellanchor.setTo(marker);
|
twocellanchor.setTo(marker);
|
||||||
marker.setCol(1);
|
marker.setCol(1); //------
|
||||||
marker.setColOff( 104775 );
|
marker.setColOff( 104775 ); //------
|
||||||
marker.setRow(3);
|
marker.setRow(3); //------
|
||||||
marker.setRowOff( 142875 );
|
marker.setRowOff( 142875 ); //------
|
||||||
// Create object for from
|
// Create object for from
|
||||||
CTMarker marker2 = dmlspreadsheetdrawingObjectFactory.createCTMarker();
|
CTMarker marker2 = dmlspreadsheetdrawingObjectFactory.createCTMarker();
|
||||||
twocellanchor.setFrom(marker2);
|
twocellanchor.setFrom(marker2);
|
||||||
marker2.setCol(0);
|
marker2.setCol(0); //------
|
||||||
marker2.setColOff( 0 );
|
marker2.setColOff( 0 ); //------
|
||||||
marker2.setRow(0);
|
marker2.setRow(0); //------
|
||||||
marker2.setRowOff( 0 );
|
marker2.setRowOff( 0 ); //------
|
||||||
twocellanchor.setEditAs(STEditAs.fromValue("name"));
|
twocellanchor.setEditAs(STEditAs.ONE_CELL);
|
||||||
|
|
||||||
// return drawingWrapped;
|
// return drawingWrapped;
|
||||||
return drawing;
|
return drawing;
|
||||||
@@ -264,5 +592,4 @@ public class ExcelUtil {
|
|||||||
XmlUtils.unmarshalString(openXML));
|
XmlUtils.unmarshalString(openXML));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user