excel文件生成首页和修订记录
This commit is contained in:
parent
24fd1ab36d
commit
7e4e97f3f7
@ -0,0 +1,181 @@
|
||||
package com.awspaas.apps.coe.pal.output.zd;
|
||||
|
||||
import org.apache.poi.hssf.usermodel.*;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellType;
|
||||
import org.apache.poi.ss.usermodel.DateUtil;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.xssf.usermodel.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class HSSFUtils {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* #合并多个excel文件
|
||||
* @param fileLists excel文件路径
|
||||
* @param path 目标文件保存目录
|
||||
* @param fileName 目标文件名称
|
||||
*/
|
||||
public static void mergeExcel(List<String> fileLists, String path, String fileName) {
|
||||
// 创建新的excel工作簿
|
||||
HSSFWorkbook newExcelWorkBook = new HSSFWorkbook();
|
||||
// 遍历需要合并的excel文件
|
||||
for (String excelName : fileLists) {
|
||||
try (InputStream in = new FileInputStream(excelName)) {
|
||||
// 创建工作簿
|
||||
HSSFWorkbook tmpWorkBook = new HSSFWorkbook(in);
|
||||
// 获取工作簿中的Sheet个数
|
||||
int len = tmpWorkBook.getNumberOfSheets();
|
||||
if (len <= 1) {
|
||||
HSSFSheet tmpSheet = tmpWorkBook.getSheetAt(0);
|
||||
HSSFSheet newExcelSheet = newExcelWorkBook.createSheet(tmpSheet.getSheetName());
|
||||
// 复制sheet内容
|
||||
copyExcelSheet(newExcelWorkBook, tmpSheet, newExcelSheet);
|
||||
} else {
|
||||
for (int i = 0; i < len; i++) {
|
||||
HSSFSheet tmpSheet = tmpWorkBook.getSheetAt(i);
|
||||
HSSFSheet newExcelSheet = newExcelWorkBook.createSheet(tmpSheet.getSheetName());
|
||||
// 复制sheet内容
|
||||
copyExcelSheet(newExcelWorkBook, tmpSheet, newExcelSheet);
|
||||
}
|
||||
}
|
||||
// 关闭tmpWorkBook工作簿
|
||||
tmpWorkBook.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// 新生成的excel文件
|
||||
if (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls")) {
|
||||
fileName += ".xlsx";
|
||||
}
|
||||
String excelFileName = path + File.separator + fileName;
|
||||
// 判断文件是否存在
|
||||
File excelFile = new File(excelFileName);
|
||||
if (excelFile.exists()) {
|
||||
// 存在则删除
|
||||
excelFile.delete();
|
||||
}
|
||||
// 使用输出流写出
|
||||
try (FileOutputStream fos = new FileOutputStream(excelFileName)) {
|
||||
newExcelWorkBook.write(fos);
|
||||
fos.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
newExcelWorkBook.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.out.println("excel文件合并成功,合并后文件路径:" + excelFileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* #复制sheet到新的excel文件中
|
||||
* @param workbook excel工作簿
|
||||
* @param tmpSheet 来源sheet
|
||||
* @param newExcelSheet 新生成的sheet
|
||||
*/
|
||||
public static void copyExcelSheet(HSSFWorkbook workbook, HSSFSheet tmpSheet, HSSFSheet newExcelSheet) {
|
||||
// 合并单元格
|
||||
mergeSheetAllRegion(tmpSheet, newExcelSheet);
|
||||
// 设置单元格列宽度
|
||||
// 获取最后一个单元格位置
|
||||
int len = tmpSheet.getRow(tmpSheet.getFirstRowNum()).getLastCellNum();
|
||||
for (int i = 0; i < len; i++) {
|
||||
newExcelSheet.setColumnWidth(i, tmpSheet.getColumnWidth(i));
|
||||
}
|
||||
// 复制每行内容
|
||||
Iterator<Row> it = tmpSheet.iterator();
|
||||
while (it.hasNext()) {
|
||||
HSSFRow tmpRow = (HSSFRow) it.next();
|
||||
// 创建新行
|
||||
HSSFRow newExcelRow = newExcelSheet.createRow(tmpRow.getRowNum());
|
||||
// 复制行
|
||||
copyExcelRow(workbook, tmpRow, newExcelRow);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* #合并单元格
|
||||
* @param tmpSheet 来源sheet
|
||||
* @param newExcelSheet 目标sheet
|
||||
*/
|
||||
private static void mergeSheetAllRegion(HSSFSheet tmpSheet, HSSFSheet newExcelSheet) {
|
||||
int num = tmpSheet.getNumMergedRegions();
|
||||
CellRangeAddress cellRange = null;
|
||||
for (int i = 0; i < num; i++) {
|
||||
cellRange = tmpSheet.getMergedRegion(i);
|
||||
newExcelSheet.addMergedRegion(cellRange);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* #复制excel中的行到新的sheet中
|
||||
* @param workbook 目标工作簿
|
||||
* @param tmpRow 来源excel行
|
||||
* @param newExcelRow 目标excel行
|
||||
*/
|
||||
public static void copyExcelRow(HSSFWorkbook workbook, HSSFRow tmpRow, HSSFRow newExcelRow) {
|
||||
// 设置行高
|
||||
newExcelRow.setHeight(tmpRow.getHeight());
|
||||
// 获取所有列
|
||||
Iterator<Cell> it = tmpRow.cellIterator();
|
||||
while (it.hasNext()) {
|
||||
HSSFCell tmpCell = (HSSFCell) it.next();
|
||||
// 创建单元格
|
||||
HSSFCell newExcelCell = newExcelRow.createCell(tmpCell.getColumnIndex());
|
||||
// 复制单元格
|
||||
copyExcelCell(workbook, tmpCell, newExcelCell);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* #复制单元格
|
||||
* @param workbook 目标工作簿
|
||||
* @param tmpCell 来源excel单元格
|
||||
* @param newExcelCell 目标excel单元格
|
||||
*/
|
||||
public static void copyExcelCell(HSSFWorkbook workbook, HSSFCell tmpCell, HSSFCell newExcelCell) {
|
||||
HSSFCellStyle newExcelStyle = workbook.createCellStyle();
|
||||
// 复制单元格样式
|
||||
newExcelStyle.cloneStyleFrom(tmpCell.getCellStyle());
|
||||
// 单元格样式
|
||||
newExcelCell.setCellStyle(newExcelStyle);
|
||||
if (tmpCell.getCellComment() != null) {
|
||||
newExcelCell.setCellComment(tmpCell.getCellComment());
|
||||
}
|
||||
// 不同数据类型处理
|
||||
CellType tmpCellType = CellType.forInt(tmpCell.getCellType());
|
||||
|
||||
newExcelCell.setCellType(tmpCellType);
|
||||
if (tmpCellType == CellType.NUMERIC) {
|
||||
if (DateUtil.isCellDateFormatted(tmpCell)) {
|
||||
newExcelCell.setCellValue(tmpCell.getDateCellValue());
|
||||
} else {
|
||||
newExcelCell.setCellValue(tmpCell.getNumericCellValue());
|
||||
}
|
||||
} else if (tmpCellType == CellType.STRING) {
|
||||
newExcelCell.setCellValue(tmpCell.getRichStringCellValue());
|
||||
} else if (tmpCellType == CellType.BLANK) {
|
||||
} else if (tmpCellType == CellType.BOOLEAN) {
|
||||
newExcelCell.setCellValue(tmpCell.getBooleanCellValue());
|
||||
} else if (tmpCellType == CellType.ERROR) {
|
||||
newExcelCell.setCellErrorValue(tmpCell.getErrorCellValue());
|
||||
} else if (tmpCellType == CellType.FORMULA) {
|
||||
newExcelCell.setCellFormula(tmpCell.getCellFormula());
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,472 @@
|
||||
package com.awspaas.apps.coe.pal.output.zd;
|
||||
|
||||
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.plugin.profile.DCPluginProfile;
|
||||
import com.actionsoft.bpms.bo.engine.BO;
|
||||
import com.actionsoft.bpms.commons.database.RowMap;
|
||||
import com.actionsoft.bpms.commons.formfile.model.delegate.FormFile;
|
||||
import com.actionsoft.bpms.server.UserContext;
|
||||
import com.actionsoft.bpms.server.bind.annotation.Controller;
|
||||
import com.actionsoft.bpms.server.bind.annotation.Mapping;
|
||||
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.spire.presentation.*;
|
||||
import com.spire.presentation.drawing.FillFormatType;
|
||||
import org.apache.poi.hssf.usermodel.HSSFCell;
|
||||
import org.apache.poi.hssf.usermodel.HSSFRow;
|
||||
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Color;
|
||||
import org.apache.poi.xssf.usermodel.XSSFCell;
|
||||
import org.apache.poi.xssf.usermodel.XSSFRow;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
public class UpFileExcelAndPptController {
|
||||
@Mapping("com.awspaas.apps.coe.pal.output.zd.UpFileExcelAndPptController.upfile")
|
||||
public JSONObject upfile(String sid,String pl_uuid,String shape_uuid,String file_data){
|
||||
if (shape_uuid.contains("obj_")){
|
||||
System.out.println("file_data>>>>>>>>>>>>>"+file_data);
|
||||
JSONObject jsonObject = JSONObject.parseObject(file_data);
|
||||
JSONObject jqXHR = jsonObject.getJSONObject("jqXHR");
|
||||
JSONObject responseJSON = jqXHR.getJSONObject("responseJSON");
|
||||
JSONObject files = responseJSON.getJSONObject("files");
|
||||
String name = files.getString("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.contains(".xlsx")||name.contains(".xls")){
|
||||
try {
|
||||
|
||||
//d1d4b52a-d6e2-4a6f-acac-37e07d2ae6e5
|
||||
UpfileModel model_old = new UpfileModel();
|
||||
model_old.setUuid(UUIDGener.getUUID());
|
||||
model_old.setPl_uuid("f9df56d7-a6b2-442c-9898-822619057493");
|
||||
model_old.setShape_uuid("obj_7ebfdb24f36e469cb841988cbda035c8");
|
||||
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("制度文件模版.xlsx");
|
||||
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;
|
||||
Utils.mergeExcel(list, path, fileName);
|
||||
//Utils.mergeExcel(list,path+fileName);
|
||||
|
||||
//Open the first excel file.
|
||||
|
||||
|
||||
// 读取上传的Excel文件
|
||||
FileInputStream file = new FileInputStream(new File(dcContext.getFilePath()));
|
||||
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 = '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 = '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 = '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("修订记录");
|
||||
|
||||
|
||||
|
||||
// 获取参数所在的行和列
|
||||
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");
|
||||
strArray[8] = obj2.getString("Contents_and_reasons_for_revision");
|
||||
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(sheet_xd, datas[i], dataStartRow + i);
|
||||
}
|
||||
|
||||
|
||||
// 保存修改后的 Excel 文件
|
||||
FileOutputStream outputStream = new FileOutputStream(dcContext.getFilePath());
|
||||
workbook.write(outputStream);
|
||||
workbook.close();
|
||||
outputStream.close();
|
||||
|
||||
System.out.println("封面参数替换完成!");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println("Excel文件修改成功!");
|
||||
} catch ( Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("ok","Excel文件修改成功");
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*获取修订记录
|
||||
* @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 = "";
|
||||
String plvers = model.getVersion()+"";
|
||||
String plver = "";
|
||||
if (!"".equals(plvers)) {
|
||||
if (plvers.length() > 1) {
|
||||
plver = plvers.substring(0, 1) + "."
|
||||
+ plvers.substring(plvers.length() - 1, plvers.length());
|
||||
} else {
|
||||
plver = plvers.substring(0, 1) + ".0";
|
||||
}
|
||||
}
|
||||
//val = specialCharTransfer(map.containsKey(attr) ? (map.get(attr).getString("text")) : "").replace("\n", "");
|
||||
obj.put(attr, "V"+plver);
|
||||
|
||||
// 拟制/修订单位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("&", "&");
|
||||
}
|
||||
|
||||
/**
|
||||
* 解决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)) {
|
||||
cell.setCellValue(cellValue.replace(parameter, replacement));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解决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)) {
|
||||
cell.setCellValue(cellValue.replace(parameter, replacement));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据xlsx的
|
||||
* @param sheet
|
||||
* @param rowData
|
||||
* @param row
|
||||
*/
|
||||
private static void insertData(XSSFSheet sheet, String[] rowData, int row) {
|
||||
XSSFRow newRow = sheet.createRow(row);
|
||||
for (int i = 0; i < rowData.length; i++) {
|
||||
XSSFCell cell = newRow.createCell(i);
|
||||
cell.setCellValue(rowData[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据xls的
|
||||
* @param sheet
|
||||
* @param rowData
|
||||
* @param row
|
||||
*/
|
||||
private static void insertData(HSSFSheet sheet, String[] rowData, int row) {
|
||||
HSSFRow newRow = sheet.createRow(row);
|
||||
for (int i = 0; i < rowData.length; i++) {
|
||||
HSSFCell cell = newRow.createCell(i);
|
||||
cell.setCellValue(rowData[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换参数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)) {
|
||||
cell.setCellValue(cellValue.replace(parameter, replacement));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 替换参数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)) {
|
||||
cell.setCellValue(cellValue.replace(parameter, replacement));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,181 @@
|
||||
package com.awspaas.apps.coe.pal.output.zd;
|
||||
import org.apache.poi.hssf.usermodel.*;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.xssf.usermodel.*;
|
||||
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.poi.ss.usermodel.CellType.*;
|
||||
import static org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType.FORMULA;
|
||||
|
||||
public class Utils {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* #合并多个excel文件
|
||||
* @param fileLists excel文件路径
|
||||
* @param path 目标文件保存目录
|
||||
* @param fileName 目标文件名称
|
||||
*/
|
||||
public static void mergeExcel(List<String> fileLists, String path, String fileName) {
|
||||
// 创建新的excel工作簿
|
||||
XSSFWorkbook newExcelWorkBook = new XSSFWorkbook();
|
||||
// 遍历需要合并的excel文件
|
||||
for (String excelName : fileLists) {
|
||||
try (InputStream in = new FileInputStream(excelName)) {
|
||||
// 创建工作簿
|
||||
XSSFWorkbook tmpWorkBook = new XSSFWorkbook(in);
|
||||
// 获取工作簿中的Sheet个数
|
||||
int len = tmpWorkBook.getNumberOfSheets();
|
||||
if (len <= 1) {
|
||||
XSSFSheet tmpSheet = tmpWorkBook.getSheetAt(0);
|
||||
XSSFSheet newExcelSheet = newExcelWorkBook.createSheet(tmpSheet.getSheetName());
|
||||
// 复制sheet内容
|
||||
copyExcelSheet(newExcelWorkBook, tmpSheet, newExcelSheet);
|
||||
} else {
|
||||
for (int i = 0; i < len; i++) {
|
||||
XSSFSheet tmpSheet = tmpWorkBook.getSheetAt(i);
|
||||
XSSFSheet newExcelSheet = newExcelWorkBook.createSheet(tmpSheet.getSheetName());
|
||||
// 复制sheet内容
|
||||
copyExcelSheet(newExcelWorkBook, tmpSheet, newExcelSheet);
|
||||
}
|
||||
}
|
||||
// 关闭tmpWorkBook工作簿
|
||||
tmpWorkBook.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// 新生成的excel文件
|
||||
if (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls")) {
|
||||
fileName += ".xlsx";
|
||||
}
|
||||
String excelFileName = path + File.separator + fileName;
|
||||
// 判断文件是否存在
|
||||
File excelFile = new File(excelFileName);
|
||||
if (excelFile.exists()) {
|
||||
// 存在则删除
|
||||
excelFile.delete();
|
||||
}
|
||||
// 使用输出流写出
|
||||
try (FileOutputStream fos = new FileOutputStream(excelFileName)) {
|
||||
newExcelWorkBook.write(fos);
|
||||
fos.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
newExcelWorkBook.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.out.println("excel文件合并成功,合并后文件路径:" + excelFileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* #复制sheet到新的excel文件中
|
||||
* @param workbook excel工作簿
|
||||
* @param tmpSheet 来源sheet
|
||||
* @param newExcelSheet 新生成的sheet
|
||||
*/
|
||||
public static void copyExcelSheet(XSSFWorkbook workbook, XSSFSheet tmpSheet, XSSFSheet newExcelSheet) {
|
||||
// 合并单元格
|
||||
mergeSheetAllRegion(tmpSheet, newExcelSheet);
|
||||
// 设置单元格列宽度
|
||||
// 获取最后一个单元格位置
|
||||
int len = tmpSheet.getRow(tmpSheet.getFirstRowNum()).getLastCellNum();
|
||||
for (int i = 0; i < len; i++) {
|
||||
newExcelSheet.setColumnWidth(i, tmpSheet.getColumnWidth(i));
|
||||
}
|
||||
// 复制每行内容
|
||||
Iterator<Row> it = tmpSheet.iterator();
|
||||
while (it.hasNext()) {
|
||||
XSSFRow tmpRow = (XSSFRow) it.next();
|
||||
// 创建新行
|
||||
XSSFRow newExcelRow = newExcelSheet.createRow(tmpRow.getRowNum());
|
||||
// 复制行
|
||||
copyExcelRow(workbook, tmpRow, newExcelRow);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* #合并单元格
|
||||
* @param tmpSheet 来源sheet
|
||||
* @param newExcelSheet 目标sheet
|
||||
*/
|
||||
private static void mergeSheetAllRegion(XSSFSheet tmpSheet, XSSFSheet newExcelSheet) {
|
||||
int num = tmpSheet.getNumMergedRegions();
|
||||
CellRangeAddress cellRange = null;
|
||||
for (int i = 0; i < num; i++) {
|
||||
cellRange = tmpSheet.getMergedRegion(i);
|
||||
newExcelSheet.addMergedRegion(cellRange);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* #复制excel中的行到新的sheet中
|
||||
* @param workbook 目标工作簿
|
||||
* @param tmpRow 来源excel行
|
||||
* @param newExcelRow 目标excel行
|
||||
*/
|
||||
public static void copyExcelRow(XSSFWorkbook workbook, XSSFRow tmpRow, XSSFRow newExcelRow) {
|
||||
// 设置行高
|
||||
newExcelRow.setHeight(tmpRow.getHeight());
|
||||
// 获取所有列
|
||||
Iterator<Cell> it = tmpRow.cellIterator();
|
||||
while (it.hasNext()) {
|
||||
XSSFCell tmpCell = (XSSFCell) it.next();
|
||||
// 创建单元格
|
||||
XSSFCell newExcelCell = newExcelRow.createCell(tmpCell.getColumnIndex());
|
||||
// 复制单元格
|
||||
copyExcelCell(workbook, tmpCell, newExcelCell);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* #复制单元格
|
||||
* @param workbook 目标工作簿
|
||||
* @param tmpCell 来源excel单元格
|
||||
* @param newExcelCell 目标excel单元格
|
||||
*/
|
||||
public static void copyExcelCell(XSSFWorkbook workbook, XSSFCell tmpCell, XSSFCell newExcelCell) {
|
||||
XSSFCellStyle newExcelStyle = workbook.createCellStyle();
|
||||
// 复制单元格样式
|
||||
newExcelStyle.cloneStyleFrom(tmpCell.getCellStyle());
|
||||
// 单元格样式
|
||||
newExcelCell.setCellStyle(newExcelStyle);
|
||||
if (tmpCell.getCellComment() != null) {
|
||||
newExcelCell.setCellComment(tmpCell.getCellComment());
|
||||
}
|
||||
// 不同数据类型处理
|
||||
CellType tmpCellType = CellType.forInt(tmpCell.getCellType());
|
||||
|
||||
newExcelCell.setCellType(tmpCellType);
|
||||
if (tmpCellType == CellType.NUMERIC) {
|
||||
if (DateUtil.isCellDateFormatted(tmpCell)) {
|
||||
newExcelCell.setCellValue(tmpCell.getDateCellValue());
|
||||
} else {
|
||||
newExcelCell.setCellValue(tmpCell.getNumericCellValue());
|
||||
}
|
||||
} else if (tmpCellType == CellType.STRING) {
|
||||
newExcelCell.setCellValue(tmpCell.getRichStringCellValue());
|
||||
} else if (tmpCellType == CellType.BLANK) {
|
||||
} else if (tmpCellType == CellType.BOOLEAN) {
|
||||
newExcelCell.setCellValue(tmpCell.getBooleanCellValue());
|
||||
} else if (tmpCellType == CellType.ERROR) {
|
||||
newExcelCell.setCellErrorValue(tmpCell.getErrorCellValue());
|
||||
} else if (tmpCellType == CellType.FORMULA) {
|
||||
newExcelCell.setCellFormula(tmpCell.getCellFormula());
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user