apps/com.awspaas.user.bpa/src/com/awspaas/user/bpa/aslp/UpdatePptxAndExcelAslp.java

1092 lines
58 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.awspaas.user.bpa.aslp;
import com.actionsoft.apps.coe.pal.constant.CoEConstant;
import com.actionsoft.apps.coe.pal.pal.repository.PALRepositoryQueryAPIManager;
import com.actionsoft.apps.coe.pal.pal.repository.cache.PALRepositoryCache;
import com.actionsoft.apps.coe.pal.pal.repository.model.PALRepositoryModel;
import com.actionsoft.apps.coe.pal.pal.repository.upfile.constant.CoeFileConstant;
import com.actionsoft.apps.coe.pal.pal.repository.upfile.model.UpfileModel;
import com.actionsoft.apps.resource.interop.aslp.ASLP;
import com.actionsoft.apps.resource.interop.aslp.Meta;
import com.actionsoft.apps.resource.plugin.profile.DCPluginProfile;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.fs.DCContext;
import com.actionsoft.bpms.server.fs.dc.DCProfileManager;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.bpms.util.UtilDate;
import com.actionsoft.bpms.util.UtilString;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.bpa.util.HSSFUtils;
import com.awspaas.user.bpa.util.Utils;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.docx4j.dml.CTTableCell;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPresentationProperties;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMaster;
import org.springframework.util.StringUtils;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.*;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class UpdatePptxAndExcelAslp implements ASLP {
@Override
@Meta(parameter = { "name: 'sid', required: true, desc: 'sid'",
"name: 'pl_uuid', required: true, desc: '文件id'",
"name: 'fil_path', required: true, desc: '文件保存地址'",
"name: 'file_url', required: true, desc: '文件路径(不包含文件名称)'",
"name: 'shape_uuid', required: true, desc: '文件内容id'",
"name: 'file_data', required: true, desc: '文件名称'" })
public ResponseObject call(Map<String, Object> params) {
ResponseObject ro = ResponseObject.newWarnResponse("结果未知");
String pl_uuid1 = SDK.getAppAPI().getProperty("com.awspaas.user.apps.new_performance_indicator_library", "pl_uuid");
String shape_uuid1 = SDK.getAppAPI().getProperty("com.awspaas.user.apps.new_performance_indicator_library", "shape_uuid");
String sid = (String) params.get("sid");
if (!SDK.getPortalAPI().checkSession(sid)) {
ro = ResponseObject.newErrResponse("sid无效");
return ro;
}
String pl_uuid = params.get("pl_uuid").toString();
String shape_uuid = params.get("shape_uuid").toString();
if (params.get("shape_uuid").toString().contains("obj_")) {
String name = null;
try {
name = URLDecoder.decode(params.get("file_data").toString(), StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("filename>>>>>>>>>>>" + name);
UpfileModel model = new UpfileModel();
model.setUuid(UUIDGener.getUUID());
model.setPl_uuid(pl_uuid);
model.setShape_uuid(shape_uuid);
model.setDownload(1);
model.setCreateUser(UserContext.fromSessionId(sid).getUID());
model.setCreateTime(new Timestamp(System.currentTimeMillis()));
model.setType("s");
// model.setFileType(fileType);
model.setFileName(name);
DCContext dcContext = getDCContext(model, UserContext.fromSessionId(sid));
if (name.endsWith(".xlsx") || name.endsWith(".xls")) {
try {
//d1d4b52a-d6e2-4a6f-acac-37e07d2ae6e5
UpfileModel model_old = new UpfileModel();
model_old.setUuid(UUIDGener.getUUID());
model_old.setPl_uuid(pl_uuid1);
model_old.setShape_uuid(shape_uuid1);
model_old.setDownload(1);
model_old.setCreateUser(UserContext.fromSessionId(sid).getUID());
model_old.setCreateTime(new Timestamp(System.currentTimeMillis()));
model_old.setType("s");
model_old.setFileName("制度文件模版" + name.substring(name.lastIndexOf(".")));
DCContext dcContext_old = getDCContext(model_old, UserContext.fromSessionId(sid));
// 读取模版文件的Excel文件
String[] filePaths = {dcContext.getFilePath(),
dcContext_old.getFilePath()};
ArrayList<String> list = new ArrayList<>();
for (String path : filePaths) {
list.add(path);
}
String path = dcContext.getPath();
String fileName = name;
if (name.contains(".xlsx")) {
Utils.mergeExcel(list, params.get("file_url").toString(), fileName);
FileInputStream file = new FileInputStream(new File(params.get("fil_path").toString()));
XSSFWorkbook workbook = new XSSFWorkbook(file);
// FileInputStream file2 = new FileInputStream(new File(dcContext.getFilePath()));
// XSSFWorkbook workbook2 = new XSSFWorkbook(file2);
// 创建两个新的Sheet页
// Sheet sheet1 = workbook.createSheet("封面");
// Sheet sheet2 = workbook.createSheet("修订记录");
workbook.setSheetOrder("封面", 0);
workbook.setSheetOrder("修订记录", 1);
// 向新Sheet页中填充数据
// fillData(sheet1);
// fillData(sheet2);
// 保存修改后的Excel文件
// FileOutputStream outputStream = new FileOutputStream(dcContext.getFilePath());
// workbook.write(outputStream);
// workbook.close();
try {
// 获取指定的 sheet 页
XSSFSheet sheet = workbook.getSheet("封面");
// 查找并替换参数
//这个是替换文件编码
//
//
String file_number = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID IN ('file_number','form_number')");
if (StringUtils.isEmpty(file_number)) {
replaceParameter(sheet, "{{file_code}}", "");
} else {
replaceParameter(sheet, "{{file_code}}", file_number);
}
//这个是替换生效日期
String file_date = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID IN('effective_date','form_effective_date') ");
if (UtilString.isNotEmpty(file_date)) {
replaceParameter(sheet, "{{file_date}}", file_date);
} else {
replaceParameter(sheet, "{{file_date}}", "");
}
//这个是替换适用范围
//
String fanwei = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID in( 'application','form_Scope_application')");
if (UtilString.isNotEmpty(fanwei)) {
replaceParameter(sheet, "{{fanwei}}", fanwei);
} else {
replaceParameter(sheet, "{{fanwei}}", "");
}
//这个是替换L1-L4
String L1 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L1'");
replaceParameter(sheet, "{{L1}}", L1);
String L2 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L2'");
replaceParameter(sheet, "{{L2}}", L2);
String L3 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L3'");
replaceParameter(sheet, "{{L3}}", L3);
String L4 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L4'");
replaceParameter(sheet, "{{L4}}", L4);
//这个是替换流程名称
String process_name = DBSql.getString("select PLNAME from app_act_coe_pal_repository where ID = '" + pl_uuid + "'");
replaceParameter(sheet, "{{process_name}}", process_name);
file.close();
XSSFSheet sheet_xd = workbook.getSheet("修订记录");
replaceParameter(sheet_xd, "{{process_name}}", process_name);
// 获取参数所在的行和列
int parameterRow = 3; // 假设参数在第二行
int parameterCol = 0; // 假设参数在第一列
// 获取数据起始行
int dataStartRow = 3; // 假设数据从第四行开始
// 定义要替换的参数数组
String[] parameters = {"{{number}}", "{{version}}", "{{company}}", "{{creauser}}", "{{date}}", "{{shen_user}}", "{{fuhe_user}}", "{{shenpi_user}}", "{{person}}"};
/*// 定义要插入的数据数组
String[][] data = {
{"John Doe", "30", "New York"},
{"Jane Smith", "25", "London"},
{"Michael Johnson", "40", "Sydney"}
};
*/
PALRepositoryModel palRepositoryModel = PALRepositoryCache.getCache().get(pl_uuid);
JSONArray versionHistoryTable = getVersionHistoryTable(palRepositoryModel);
String[][] strArray2 = new String[versionHistoryTable.size()][];
System.out.println("jsonArray>>>>>>>>>>" + versionHistoryTable.toString());
/**
* [{"approver":"任国梁","Drafted_and_revised_date":"2023年5月08日"
* ,"auditor":"杜薇","Issuing_department":"总部金融中心经营管理办公室",
* "reviewer":"/","Contents_and_reasons_for_revision":"新增","P_versions":"V1.0","Drafted_and_revised_by":"赵苗"}]
*/
List<String[]> lists = new LinkedList<>();
for (int i = 0; i < versionHistoryTable.size(); i++) {
JSONObject obj2 = versionHistoryTable.getJSONObject(i);
String[] strArray = new String[9];
strArray[0] = String.valueOf(i + 1);
strArray[1] = obj2.getString("P_versions");
strArray[2] = obj2.getString("Issuing_department");
strArray[3] = obj2.getString("Drafted_and_revised_by");
strArray[4] = obj2.getString("Drafted_and_revised_date");
strArray[5] = obj2.getString("auditor");
strArray[6] = obj2.getString("reviewer");
strArray[7] = obj2.getString("approver");
String str2 = "";
for (int ia = 0; ia < obj2.getString("Contents_and_reasons_for_revision").length(); ia += 19) {
if (ia + 19 < obj2.getString("Contents_and_reasons_for_revision").length()) {
str2 += obj2.getString("Contents_and_reasons_for_revision").substring(ia, ia + 19) + "\n";
} else {
str2 += obj2.getString("Contents_and_reasons_for_revision").substring(ia) + "\n";
}
}
SDK.getLogAPI().consoleInfo("str2>>>>>>>>>>>>>>" + str2);
strArray[8] = str2;
lists.add(strArray);
strArray2[i] = strArray;
}
String[][] datas = strArray2;
// 循环替换参数
for (int i = 0; i < parameters.length; i++) {
String parameter = parameters[i];
String replacement = datas[0][i];
replaceParameter(sheet_xd, parameter, replacement, parameterRow, parameterCol);
}
// 循环插入数据
for (int i = 0; i < datas.length; i++) {
insertData(workbook, sheet_xd, datas[i], dataStartRow + i);
}
// 保存修改后的 Excel 文件
FileOutputStream outputStream = new FileOutputStream(params.get("fil_path").toString());
workbook.write(outputStream);
workbook.close();
outputStream.close();
System.out.println("封面参数替换完成!");
} catch (Exception e) {
}
} else {
HSSFUtils.mergeExcel(list, params.get("file_url").toString(), fileName);
FileInputStream file = new FileInputStream(new File(params.get("fil_path").toString()));
HSSFWorkbook workbook = new HSSFWorkbook(file);
// FileInputStream file2 = new FileInputStream(new File(dcContext.getFilePath()));
// XSSFWorkbook workbook2 = new XSSFWorkbook(file2);
// 创建两个新的Sheet页
// Sheet sheet1 = workbook.createSheet("封面");
// Sheet sheet2 = workbook.createSheet("修订记录");
workbook.setSheetOrder("封面", 0);
workbook.setSheetOrder("修订记录", 1);
// 向新Sheet页中填充数据
// fillData(sheet1);
// fillData(sheet2);
// 保存修改后的Excel文件
// FileOutputStream outputStream = new FileOutputStream(dcContext.getFilePath());
// workbook.write(outputStream);
// workbook.close();
try {
// 获取指定的 sheet 页
HSSFSheet sheet = workbook.getSheet("封面");
// 查找并替换参数
//这个是替换文件编码
String file_number = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID IN('form_number','file_number')");
if (StringUtils.isEmpty(file_number)) {
replaceParameter(sheet, "{{file_code}}", "");
} else {
replaceParameter(sheet, "{{file_code}}", file_number);
}
//这个是替换生效日期
String file_date = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID IN('effective_date','form_effective_date')");
if (UtilString.isNotEmpty(file_date)) {
replaceParameter(sheet, "{{file_date}}", file_date);
} else {
replaceParameter(sheet, "{{file_date}}", "");
}
//这个是替换适用范围
String fanwei = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID in( 'application','form_Scope_application')");
if (UtilString.isNotEmpty(fanwei)) {
replaceParameter(sheet, "{{fanwei}}", fanwei);
} else {
replaceParameter(sheet, "{{fanwei}}", "");
}
//这个是替换L1-L4
String L1 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L1'");
replaceParameter(sheet, "{{L1}}", L1);
String L2 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L2'");
replaceParameter(sheet, "{{L2}}", L2);
String L3 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L3'");
replaceParameter(sheet, "{{L3}}", L3);
String L4 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L4'");
replaceParameter(sheet, "{{L4}}", L4);
//这个是替换流程名称
String process_name = DBSql.getString("select PLNAME from app_act_coe_pal_repository where ID = '" + pl_uuid + "'");
replaceParameter(sheet, "{{process_name}}", process_name);
file.close();
HSSFSheet sheet_xd = workbook.getSheet("修订记录");
replaceParameter(sheet_xd, "{{process_name}}", process_name);
// 获取参数所在的行和列
int parameterRow = 3; // 假设参数在第二行
int parameterCol = 0; // 假设参数在第一列
// 获取数据起始行
int dataStartRow = 3; // 假设数据从第四行开始
// 定义要替换的参数数组
String[] parameters = {"{{number}}", "{{version}}", "{{company}}", "{{creauser}}", "{{date}}", "{{shen_user}}", "{{fuhe_user}}", "{{shenpi_user}}", "{{person}}"};
/*// 定义要插入的数据数组
String[][] data = {
{"John Doe", "30", "New York"},
{"Jane Smith", "25", "London"},
{"Michael Johnson", "40", "Sydney"}
};
*/
PALRepositoryModel palRepositoryModel = PALRepositoryCache.getCache().get(pl_uuid);
JSONArray versionHistoryTable = getVersionHistoryTable(palRepositoryModel);
String[][] strArray2 = new String[versionHistoryTable.size()][];
System.out.println("jsonArray>>>>>>>>>>" + versionHistoryTable.toString());
/**
* [{"approver":"任国梁","Drafted_and_revised_date":"2023年5月08日"
* ,"auditor":"杜薇","Issuing_department":"总部金融中心经营管理办公室",
* "reviewer":"/","Contents_and_reasons_for_revision":"新增","P_versions":"V1.0","Drafted_and_revised_by":"赵苗"}]
*/
List<String[]> lists = new LinkedList<>();
for (int i = 0; i < versionHistoryTable.size(); i++) {
JSONObject obj2 = versionHistoryTable.getJSONObject(i);
String[] strArray = new String[9];
strArray[0] = String.valueOf(i + 1);
strArray[1] = obj2.getString("P_versions");
strArray[2] = obj2.getString("Issuing_department");
strArray[3] = obj2.getString("Drafted_and_revised_by");
strArray[4] = obj2.getString("Drafted_and_revised_date");
strArray[5] = obj2.getString("auditor");
strArray[6] = obj2.getString("reviewer");
strArray[7] = obj2.getString("approver");
String str2 = "";
for (int ia = 0; ia < obj2.getString("Contents_and_reasons_for_revision").length(); ia += 19) {
if (ia + 19 < obj2.getString("Contents_and_reasons_for_revision").length()) {
str2 += obj2.getString("Contents_and_reasons_for_revision").substring(ia, ia + 19) + "\n";
} else {
str2 += obj2.getString("Contents_and_reasons_for_revision").substring(ia) + "\n";
}
}
SDK.getLogAPI().consoleInfo("str2>>>>>>>>>>>>>>" + str2);
strArray[8] = str2;
lists.add(strArray);
strArray2[i] = strArray;
}
String[][] datas = strArray2;
// 循环替换参数
for (int i = 0; i < parameters.length; i++) {
String parameter = parameters[i];
String replacement = datas[0][i];
replaceParameter(sheet_xd, parameter, replacement, parameterRow, parameterCol);
}
// 循环插入数据
for (int i = 0; i < datas.length; i++) {
insertData(workbook, sheet_xd, datas[i], dataStartRow + i);
}
// 保存修改后的 Excel 文件
FileOutputStream outputStream = new FileOutputStream(params.get("fil_path").toString());
workbook.write(outputStream);
workbook.close();
outputStream.close();
System.out.println("封面参数替换完成!");
} catch (Exception e) {
}
}
//Utils.mergeExcel(list,path+fileName);
//Open the first excel file.
// 读取上传的Excel文件
System.out.println("Excel文件修改成功");
} catch (Exception e) {
e.printStackTrace();
}
} else if (name.endsWith(".pptx") || name.endsWith(".ppt")) {
UpfileModel model_old = new UpfileModel();
model_old.setUuid(UUIDGener.getUUID());
model_old.setPl_uuid(pl_uuid1);
model_old.setShape_uuid(shape_uuid1);
model_old.setDownload(1);
model_old.setCreateUser(UserContext.fromSessionId(sid).getUID());
model_old.setCreateTime(new Timestamp(System.currentTimeMillis()));
model_old.setType("s");
model_old.setFileName("ppt版本模板" + name.substring(name.lastIndexOf(".")));
DCContext dcContext_old = getDCContext(model_old, UserContext.fromSessionId(sid));
String[] fileNames = {dcContext.getFilePath(), dcContext_old.getFilePath()};
String mergedFileName = dcContext.getPath() + "dc.pptx";
if (name.endsWith(".pptx") || name.endsWith(".ppt")) {
try (FileOutputStream outputStream = new FileOutputStream(mergedFileName)) {
XMLSlideShow mergedSlideShow = new XMLSlideShow();
for (String fileName : fileNames) {
XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream(fileName));
for (XSLFSlide slide : slideShow.getSlides()) {
mergedSlideShow.setPageSize(slideShow.getPageSize());
XSLFSlide newSlide = mergedSlideShow.createSlide().importContent(slide);
copyConditionalFormatting(slide, newSlide);
copyImages(slide, newSlide);
}
}
mergedSlideShow.write(outputStream);
mergedSlideShow.close();
outputStream.close();
//生成之后的数据pptx
XMLSlideShow slideShows = new XMLSlideShow(new FileInputStream(mergedFileName));
SDK.getLogAPI().consoleInfo("开始执行了吗");
// 获取幻灯片列表
List<XSLFSlide> slides = slideShows.getSlides();
SDK.getLogAPI().consoleInfo("幻灯片的大小是多少>>>>>>>" + slides.size());
//移动倒数第二个至第一张幻灯
XSLFSlide secondslide = slides.get(slides.size() - 2);
slideShows.setSlideOrder(secondslide, 0);
String process_name = DBSql.getString("select PLNAME from app_act_coe_pal_repository where ID = '" + pl_uuid + "'");
for (XSLFSlide slide : slides) {
for (XSLFShape shape : slide.getShapes()) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape textShape = (XSLFTextShape) shape;
for (XSLFTextParagraph paragraph : textShape.getTextParagraphs()) {
for (XSLFTextRun textRun : paragraph.getTextRuns()) {
String text = textRun.getRawText();
// 根据需要替换参数
text = text.replace("{{process_name}}", process_name);
textRun.setText(text);
}
}
}
}
}
XSLFSlide secondslide_last = slides.get(slides.size() - 1);
slideShows.setSlideOrder(secondslide_last, 1);
List<XSLFSlide> newSlides = new ArrayList<>();
PALRepositoryModel palRepositoryModel = PALRepositoryCache.getCache().get(pl_uuid);
JSONArray versionHistoryTable = getVersionHistoryTable(palRepositoryModel);
System.out.println("jsonArray>>>>>>>>>>" + versionHistoryTable.toString());
int q = 0;
for (XSLFSlide slide : slides) {
for (XSLFShape shape : slide.getShapes()) {
if (shape instanceof XSLFTable) {
XSLFTable table = (XSLFTable) shape;
for (XSLFTableRow row : table.getRows()) {
for (XSLFTableCell cell : row.getCells()) {
for (XSLFTextParagraph paragraph : cell.getTextParagraphs()) {
for (XSLFTextRun textRun : paragraph.getTextRuns()) {
String text = textRun.getRawText();
// 根据需要替换参数
String L1 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L1'");
String L2 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L2'");
String L3 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L3'");
String L4 = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID = 'Process_Architecture_L4'");
String fanwei = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID in( 'application','form_Scope_application')");
String file_number = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID IN('form_number','file_number')");
String P_versions = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID IN('P_versions','versions')");
String file_date = DBSql.getString("select PROPERTYVALUE from APP_ACT_COE_PAL_PROP where PLID = '" + pl_uuid + "' and PROPERTYID IN('effective_date','form_effective_date') ");
if (UtilString.isNotEmpty(P_versions)) {
text = text.replace("{{file_version}}", P_versions);
} else {
text = text.replace("{{file_version}}", "");
}
if (UtilString.isNotEmpty(file_date)) {
text = text.replace("{{file_data}}", file_date);
} else {
text = text.replace("{{file_data}}", "");
}
if (UtilString.isNotEmpty(file_number)) {
text = text.replace("{{file_code}}", file_number);
} else {
text = text.replace("{{file_code}}", "");
}
if (UtilString.isNotEmpty(fanwei)) {
text = text.replace("{{fanwei}}", fanwei);
} else {
text = text.replace("{{fanwei}}", "");
}
text = text.replace("{{L1}}", L1);
text = text.replace("{{L2}}", L2);
text = text.replace("{{L3}}", L3);
text = text.replace("{{L4}}", L4);
textRun.setText(text);
}
}
}
}
if (q != 0) {
XSLFTable tables = (XSLFTable) shape;
XSLFTableRow firstRow = tables.getRows().get(1);
// 获取表格的第一行
for (int i = 1; i < versionHistoryTable.size(); i++) {
XSLFTableRow newRow = table.addRow();
for (int col = 0; col < firstRow.getCells().size(); col++) {
XSLFTableCell cell = firstRow.getCells().get(col);
XSLFTableCell newCell = newRow.addCell();
newCell.setText(cell.getText());
newCell.setFillColor(cell.getFillColor());
newCell.setBorderColor(XSLFTableCell.BorderEdge.bottom, cell.getBorderColor(XSLFTableCell.BorderEdge.bottom));
newCell.setBorderColor(XSLFTableCell.BorderEdge.top, cell.getBorderColor(XSLFTableCell.BorderEdge.top));
newCell.setBorderColor(XSLFTableCell.BorderEdge.left, cell.getBorderColor(XSLFTableCell.BorderEdge.left));
newCell.setBorderColor(XSLFTableCell.BorderEdge.right, cell.getBorderColor(XSLFTableCell.BorderEdge.right));
newCell.setBorderWidth(XSLFTableCell.BorderEdge.bottom, cell.getBorderWidth(XSLFTableCell.BorderEdge.bottom));
newCell.setBorderWidth(XSLFTableCell.BorderEdge.top, cell.getBorderWidth(XSLFTableCell.BorderEdge.top));
newCell.setBorderWidth(XSLFTableCell.BorderEdge.left, cell.getBorderWidth(XSLFTableCell.BorderEdge.left));
newCell.setBorderWidth(XSLFTableCell.BorderEdge.right, cell.getBorderWidth(XSLFTableCell.BorderEdge.right));
newCell.setVerticalAlignment(cell.getVerticalAlignment());
newCell.setHorizontalCentered(cell.isHorizontalCentered());
newCell.setLeftInset(cell.getLeftInset());
newCell.setRightInset(cell.getRightInset());
newCell.setTopInset(cell.getTopInset());
newCell.setBottomInset(cell.getBottomInset());
}
}
for (int i = 0; i < versionHistoryTable.size(); i++) {
JSONObject obj2 = versionHistoryTable.getJSONObject(i);
// 替换参数
if (i == 0) {
for (int col = 0; col < firstRow.getCells().size(); col++) {
XSLFTableCell cell = firstRow.getCells().get(col);
String text = cell.getText();
text = text.replace("{{version}}", obj2.getString("P_versions"));
text = text.replace("{{company}}", obj2.getString("Issuing_department"));
text = text.replace("{{creauser}}", obj2.getString("Drafted_and_revised_by"));
text = text.replace("{{date}}", obj2.getString("Drafted_and_revised_date"));
text = text.replace("{{shen_user}}", obj2.getString("auditor"));
text = text.replace("{{fuhe_user}}", obj2.getString("reviewer"));
text = text.replace("{{shenpi_user}}", obj2.getString("approver"));
text = text.replace("{{person}}", obj2.getString("Contents_and_reasons_for_revision"));
cell.setText(text);
}
} else {
System.out.println("版本2的时候怎么不进");
XSLFTableRow newRow = table.getRows().get(i + 1);
for (int col = 0; col < newRow.getCells().size(); col++) {
XSLFTableCell cell = newRow.getCells().get(col);
String text = cell.getText();
text = text.replace("{{version}}", obj2.getString("P_versions"));
text = text.replace("{{company}}", obj2.getString("Issuing_department"));
text = text.replace("{{creauser}}", obj2.getString("Drafted_and_revised_by"));
text = text.replace("{{date}}", obj2.getString("Drafted_and_revised_date"));
text = text.replace("{{shen_user}}", obj2.getString("auditor"));
text = text.replace("{{fuhe_user}}", obj2.getString("reviewer"));
text = text.replace("{{shenpi_user}}", obj2.getString("approver"));
text = text.replace("{{person}}", obj2.getString("Contents_and_reasons_for_revision"));
cell.setText(text);
// XSLFTableRow row = table.getRows().get(i);
}
}
}
}
SDK.getLogAPI().consoleInfo("asddasdasdas");
}
}
q++;
}
// 获取当前幻灯片中的表格对象
// 获取PPT的第一页
// 替换文本框中的参数
FileOutputStream out = new FileOutputStream(params.get("fil_path").toString());
slideShows.write(out);
slideShows.close();
out.close();
FileInputStream fileInputStream = new FileInputStream(params.get("fil_path").toString());
// 打开现有PPTX文件
XMLSlideShow ppt = new XMLSlideShow(fileInputStream);
// 获取要复制的幻灯片
XSLFSlide sourceSlide = ppt.getSlides().get(1);
// 复制幻灯片
XSLFSlide newSlide = ppt.createSlide(sourceSlide.getSlideLayout());
// 复制表格
copyTable(sourceSlide, newSlide);
// 获取复制出来的幻灯片中的表格
XSLFTable table = (XSLFTable) newSlide.getShapes().get(0);
// 删除表格中的指定行假设要删除第2行
// 获取要删除的行假设要删除第2行
//XSLFTableRow rowToRemove = table.getRows().get(1);
// 获取所在的形状,并删除指定行
// table.getRows().remove(rowToRemove);
table.getCTTable().getTrList().remove(1);
// 保存修改后的PPTX文件
FileOutputStream fileOutputStream = new FileOutputStream(params.get("fil_path").toString());
ppt.write(fileOutputStream);
fileOutputStream.close();
SDK.getLogAPI().consoleInfo("移动文件位置替换完成");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return null;
}
private static void copyTable(XSLFSlide sourceSlide, XSLFSlide newSlide) {
for (XSLFShape shape : sourceSlide.getShapes()) {
if (shape instanceof XSLFTable) {
XSLFTable sourceTable = (XSLFTable) shape;
// 创建一个新表格,并复制原表格的属性
XSLFTable newTable = newSlide.createTable();
newTable.setAnchor(sourceTable.getAnchor());
// 复制表格的行和单元格
for (int rowIndex = 0; rowIndex < sourceTable.getNumberOfRows(); rowIndex++) {
XSLFTableRow sourceRow = sourceTable.getRows().get(rowIndex);
XSLFTableRow newRow = newTable.addRow();
for (XSLFTableCell sourceCell : sourceRow.getCells()) {
XSLFTableCell newCell = newRow.addCell();
// 复制单元格的文本内容
// newCell.setText(sourceCell.getText());
// 设置单元格样式
newCell.setVerticalAlignment(sourceCell.getVerticalAlignment());
newCell.setLineColor(sourceCell.getLineColor());
//newCell.setBorderWidth(sourceCell.getBorderWidth());
// 复制单元格的样式属性
XSLFTextParagraph sourceParagraph = sourceCell.getTextParagraphs().get(0);
XSLFTextParagraph newParagraph = newCell.addNewTextParagraph();
XSLFTextRun newTextRun = newParagraph.addNewTextRun();
newTextRun.setText(sourceParagraph.getText());
newTextRun.setFontColor(sourceParagraph.getTextRuns().get(0).getFontColor());
// 继续复制其他属性...
}
}
}
}
}
private static void copyConditionalFormatting(HSLFSlide sourceSlide, HSLFSlide targetSlide) {
List<HSLFShape> shapes = sourceSlide.getShapes();
for (HSLFShape shape : shapes) {
if (shape instanceof HSLFTextShape) {
HSLFTextShape textShape = (HSLFTextShape) shape;
HSLFTextShape newShape = targetSlide.createTextBox();
newShape.setText(textShape.getText());
newShape.setTextPlaceholder(textShape.getTextPlaceholder());
} else {
targetSlide.addShape(shape);
}
}
}
private static HSLFTextShape getMatchingTextShape(HSLFSlide slide, HSLFTextShape sourceShape) {
List<HSLFShape> shapes = slide.getShapes();
for (HSLFShape shape : shapes) {
if (shape instanceof HSLFTextShape) {
HSLFTextShape textShape = (HSLFTextShape) shape;
if (textShape.getTextParagraphs().size() == sourceShape.getTextParagraphs().size()) {
return textShape;
}
}
}
return null;
}
// 更新幻灯片ID列表
// 更新幻灯片ID列表
// 移动幻灯片位置
// 移动幻灯片位置
private static void copyConditionalFormatting(XSLFSlide sourceSlide, XSLFSlide targetSlide) {
List<XSLFShape> shapes = sourceSlide.getShapes();
for (XSLFShape shape : shapes) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape textShape = (XSLFTextShape) shape;
XSLFTextShape newShape = getMatchingTextShape(targetSlide, textShape);
if (newShape != null && newShape.getXmlObject() != null && newShape.getXmlObject() != null) {
textShape.getXmlObject().set(newShape.getXmlObject());
}
}
}
}
private static XSLFTextShape getMatchingTextShape(XSLFSlide slide, XSLFTextShape sourceShape) {
List<XSLFShape> shapes = slide.getShapes();
for (XSLFShape shape : shapes) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape textShape = (XSLFTextShape) shape;
if (textShape.getTextParagraphs().size() == sourceShape.getTextParagraphs().size()) {
return textShape;
}
}
}
return null;
}
private static void copyImages(XSLFSlide sourceSlide, XSLFSlide targetSlide) {
for (XSLFShape shape : sourceSlide.getShapes()) {
if (shape instanceof XSLFPictureShape) {
XSLFPictureShape pictureShape = (XSLFPictureShape) shape;
XSLFPictureData pictureData = pictureShape.getPictureData();
try {
byte[] pictureBytes = pictureData.getData();
String pictureType = pictureData.getContentType();
XSLFPictureData pictureIndex = targetSlide.getSlideShow().addPicture(pictureBytes, pictureData.getType());
XSLFPictureShape newPictureShape = targetSlide.createPicture(pictureIndex);
newPictureShape.setAnchor(pictureShape.getAnchor());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private static void copyImages(HSLFSlide sourceSlide, HSLFSlide targetSlide) {
for (HSLFShape shape : sourceSlide.getShapes()) {
if (shape instanceof HSLFPictureShape) {
HSLFPictureShape pictureShape = (HSLFPictureShape) shape;
HSLFPictureData pictureData = pictureShape.getPictureData();
try {
byte[] pictureBytes = pictureData.getData();
String pictureType = pictureData.getContentType();
HSLFPictureData pictureIndex = targetSlide.getSlideShow().addPicture(pictureBytes, pictureData.getType());
HSLFPictureShape newPictureShape = targetSlide.createPicture(pictureIndex);
newPictureShape.setAnchor(pictureShape.getAnchor());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
*获取修订记录
* @param repositoryModel
* @return
*/
private JSONArray getVersionHistoryTable(PALRepositoryModel repositoryModel) {
JSONArray versionHistoryTable = new JSONArray();// 修订记录
List<PALRepositoryModel> list = PALRepositoryCache.getByVersionId(repositoryModel.getVersionId());
list.sort((m1, m2)-> {
return m1.getVersion() - m2.getVersion() > 0 ? 1 : -1;
});
for (PALRepositoryModel model : list) {
if(model.getVersion()>repositoryModel.getVersion()) {
continue;
}
JSONObject obj = new JSONObject();
Map<String, JSONObject> map = PALRepositoryQueryAPIManager.queryRepositoryAttributeById(model.getId());
// 版本P_versions
String attr = "P_versions";
String val = "";
//yujh 修改,统一使用版本工具类获取版本号
obj.put(attr, VersionUtil.getVersionStrV(model.getVersion()));
// 拟制/修订单位Issuing_department
attr = "Issuing_department";
val = specialCharTransfer(map.containsKey(attr) ? (map.get(attr).getString("text")) : "").replace("\n", "");
obj.put(attr, val);
// 拟制/修订人Drafted_and_revised_by
attr = "Drafted_and_revised_by";
val = specialCharTransfer(map.containsKey(attr) ? (map.get(attr).getString("text")) : "").replace("\n", "");
obj.put(attr, val);
// 拟制/修订日期Drafted_and_revised_date
attr = "Drafted_and_revised_date";
val = specialCharTransfer(map.containsKey(attr) ? (map.get(attr).getString("text")) : "").replace("\n", "");
if (UtilString.isNotEmpty(val)) {
try {
Timestamp timestamp = UtilDate.parseTsFromDateTime(val);
val = UtilDate.yearFormat(timestamp) + "" + Integer.parseInt(UtilDate.monthFormat(timestamp)) + "" + UtilDate.dayFormat(timestamp) + "";
} catch (Exception e) {
e.printStackTrace();
}
}
obj.put(attr, val);
// 审核人auditor
attr = "auditor";
val = specialCharTransfer(map.containsKey(attr) ? (map.get(attr).getString("text")) : "").replace("\n", "");
obj.put(attr, val);
// 复核人reviewer
attr = "reviewer";
val = specialCharTransfer(map.containsKey(attr) ? (map.get(attr).getString("text")) : "").replace("\n", "");
obj.put(attr, val);
// 审批人approver
attr = "approver";
val = specialCharTransfer(map.containsKey(attr) ? (map.get(attr).getString("text")) : "").replace("\n", "");
obj.put(attr, val);
// 修订内容及理由Contents_and_reasons_for_revision
attr = "Contents_and_reasons_for_revision";
val = specialCharTransfer(map.containsKey(attr) ? (map.get(attr).getString("text")) : "").replace("\n", "");
obj.put(attr, val);
versionHistoryTable.add(obj);
}
return versionHistoryTable;
}
public static String specialCharTransfer(String str) {
if (str == null || "".equals(str.trim())) {
return str;
}
return str.replace("&", "&amp;");
}
/**
* 解决xlsx文件
* @param sheet
* @param parameter
* @param replacement
* @param row
* @param col
*/
private static void replaceParameter(XSSFSheet sheet, String parameter, String replacement, int row, int col) {
XSSFRow rowData = sheet.getRow(row);
if (rowData == null) {
rowData = sheet.createRow(row);
}
XSSFCell cell = rowData.getCell(col);
if (cell == null) {
cell = rowData.createCell(col);
}
String cellValue = cell.getStringCellValue();
if (cellValue.contains(parameter)) {
XSSFCellStyle cellStyle = cell.getCellStyle();
// cellStyle.cloneStyleFrom(cell.getCellStyle()); // 复制原有的单元格样式
cell.setCellValue(cellValue.replace(parameter, replacement));
cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle);
}
}
/**
* 解决xls文件的
* @param sheet
* @param parameter
* @param replacement
* @param row
* @param col
*/
private static void replaceParameter(HSSFSheet sheet, String parameter, String replacement, int row, int col) {
HSSFRow rowData = sheet.getRow(row);
if (rowData == null) {
rowData = sheet.createRow(row);
}
HSSFCell cell = rowData.getCell(col);
if (cell == null) {
cell = rowData.createCell(col);
}
String cellValue = cell.getStringCellValue();
if (cellValue.contains(parameter)) {
HSSFCellStyle cellStyle = cell.getCellStyle();
//cellStyle.cloneStyleFrom(cell.getCellStyle()); // 复制原有的单元格样式
cell.setCellValue(cellValue.replace(parameter, replacement));
cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle);
}
}
/**
* 插入数据xlsx的
* @param sheet
* @param rowData
* @param row
*/
private static void insertData(XSSFWorkbook workbook, XSSFSheet sheet, String[] rowData, int row) {
XSSFRow newRow = sheet.createRow(row);
for (int i = 0; i < rowData.length; i++) {
XSSFCell cell = newRow.createCell(i);
XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.cloneStyleFrom(cell.getCellStyle());
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cell.setCellValue(rowData[i]);
// 自动调整列宽
//sheet.setColumnWidth(8, 100 * 256);
// 计算行数和行高
int rowNum = cell.getStringCellValue().split("\n").length;
float rowHeightInPoints = sheet.getDefaultRowHeightInPoints();
float rowHeight = rowNum * rowHeightInPoints;
// 设置行高
newRow.setHeightInPoints(rowHeight);
cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle);
}
}
/**
* 插入数据xls的
* @param sheet
* @param rowData
* @param row
*/
private static void insertData(HSSFWorkbook workbook, HSSFSheet sheet, String[] rowData, int row) {
HSSFRow newRow = sheet.createRow(row);
for (int i = 0; i < rowData.length; i++) {
HSSFCell cell = newRow.createCell(i);
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.cloneStyleFrom(cell.getCellStyle());
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cell.setCellValue(rowData[i]);
// 自动调整列宽
// sheet.setColumnWidth(8, 100 * 256);
// 计算行数和行高
int rowNum = cell.getStringCellValue().split("\n").length;
float rowHeightInPoints = sheet.getDefaultRowHeightInPoints();
float rowHeight = rowNum * rowHeightInPoints;
// 设置行高
newRow.setHeightInPoints(rowHeight);
cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle);
}
}
/**
* 替换参数xlsx的
* @param sheet
* @param parameter
* @param replacement
*/
private static void replaceParameter(XSSFSheet sheet, String parameter, String replacement) {
for (Row row : sheet) {
for (Cell cell : row) {
/*if (CellType.forInt(cell.getCellType()) == CellType.STRING) {
String cellValue = cell.getStringCellValue();
if (cellValue.contains(parameter)) {
CellStyle cellStyle = cell.getCellStyle();
cell.setCellValue(cellValue.replace(parameter, replacement));
cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle);
}
}*/
}
}
}
/**
* 替换参数xls的
* @param sheet
* @param parameter
* @param replacement
*/
private static void replaceParameter(HSSFSheet sheet, String parameter, String replacement) {
for (Row row : sheet) {
for (Cell cell : row) {
/*if (CellType.forInt(cell.getCellType()) == CellType.STRING) {
String cellValue = cell.getStringCellValue();
if (cellValue.contains(parameter)) {
CellStyle cellStyle = cell.getCellStyle();
cell.setCellValue(cellValue.replace(parameter, replacement));
cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle);
}
}*/
}
}
}
public DCContext getDCContext(UpfileModel model,UserContext userContext) {
DCContext dcContext = null;
DCPluginProfile dcProfile = DCProfileManager.getDCProfile(CoEConstant.APP_ID, CoeFileConstant.COE_UPFILE);
if (dcProfile != null) {
if ("f".equals(model.getType())) {// 文件
dcContext = new DCContext(userContext, dcProfile, CoEConstant.APP_ID, "file",
model.getPl_uuid(), model.getFileName());
} else {// 图形
dcContext = new DCContext(userContext, dcProfile, CoEConstant.APP_ID, model.getPl_uuid(),
model.getShape_uuid(), model.getFileName());
}
}
return dcContext;
}
}