Commit b4158d7d authored by wangqiang's avatar wangqiang

IED计划添加项目上下文对象;添加ExcelUtils对象

parent 087cdb96
......@@ -2,9 +2,24 @@ package com.yonde.dcs.plan.common.utils;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
/**
* @author xfchai
* @ClassName ExcelUtils.java
......@@ -13,6 +28,8 @@ import org.springframework.web.multipart.MultipartFile;
*/
@Slf4j
public class ExcelUtils {
private static final int xlTypePDF = 0;
/**
* 通过名称读取Excel
*
......@@ -46,4 +63,461 @@ public class ExcelUtils {
return null;
}
}
@SneakyThrows
public static void setSheetHidden(String excelPath, int sheetNumber) {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(excelPath));
workbook.setSheetHidden(sheetNumber, true);
workbook.write(new File(excelPath));
workbook.close();
}
@SneakyThrows
public static void setSheetVisibility(String excelPath, String targetSheet, String sheetName) {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(excelPath));
workbook.setSheetVisibility(workbook.getSheetIndex(sheetName), SheetVisibility.VISIBLE);
workbook.write(new File(targetSheet));
workbook.close();
}
@SneakyThrows
public static void setSheetVisibility(String excelPath, int sheetNumber) {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(excelPath));
workbook.setSheetVisibility(sheetNumber, SheetVisibility.VISIBLE);
workbook.write(new File(excelPath));
workbook.close();
}
/**
* 签图片
*
* @param dataMap
* @param cell
* @param patriarch
* @param workbook
* @throws IOException
*/
public static void signImage(Map<String, String> dataMap, Cell cell, HSSFPatriarch patriarch, HSSFWorkbook workbook) throws IOException {
ByteArrayOutputStream byteArrayOut = null;
BufferedImage bufferImg = null;
int colNumber = 0;
int rowNumber = 0;
HSSFClientAnchor anchor = null;
for (Map.Entry<String, String> entry : dataMap.entrySet()) {
if (cell.getStringCellValue().contains(entry.getKey())) {
byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File(entry.getValue()));
ImageIO.write(bufferImg, entry.getValue().substring(entry.getValue().lastIndexOf(".") + 1), byteArrayOut);
colNumber = cell.getAddress().getColumn();
rowNumber = cell.getAddress().getRow();
/**
* 该构造函数有8个参数
* 前四个参数是控制图片在单元格的位置,分别是图片距离单元格left,top,right,bottom的像素距离
* 后四个参数,前连两个表示图片左上角所在的cellNum和 rowNum,后天个参数对应的表示图片右下角所在的cellNum和 rowNum,
* excel中的cellNum和rowNum的index都是从0开始的
*
*/
anchor = new HSSFClientAnchor(0, 0, 0, 0,
(short) colNumber, rowNumber, (short) (colNumber + 6), rowNumber + 2);
anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
//插入图片
//TODO 只支持png图片
patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
cell.setCellValue("");
}
}
}
/**
* 往指定的excel中写入 备注 按列写
*
* @param excelPath
* @param rowLine
* @param columnLine
* @param sheetNumber
* @param noteList
* @throws IOException
*/
public static void writeNotes(String excelPath, int rowLine, int columnLine, int sheetNumber, List<String> noteList) throws IOException {
//读取源文件
File file = new File(excelPath);
FileInputStream inputStream = new FileInputStream(file);
//构建工作簿
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
HSSFRow row = null;
HSSFCell cell = null;
for (int i = 0; i < noteList.size(); i++) {
row = sheet.getRow(rowLine + i);
cell = row.getCell(columnLine);
cell.setCellValue(noteList.get(i));
}
workbook.write(new File(excelPath));
workbook.close();
}
/**
* 往excel 写入文本
*
* @param excelPath excel路径
* @param sheetNumber 第几个sheet 从0开始
* @param dataList 要写入的数据
* @param columnPosition 第一条数据为开始行数,后面的为列数
* @throws IOException
*/
public static void writeRowData(String excelPath, int sheetNumber, List<Integer> columnPosition, List<List<String>> dataList) throws IOException {
//读取源文件
File file = new File(excelPath);
FileInputStream inputStream = new FileInputStream(file);
//构建工作簿
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
// 第一行:key-row, value-第几行
Integer rowLine = columnPosition.get(0);
HSSFRow row = null;
HSSFCell cell = null;
for (int i = 0; i < dataList.size(); i++) {
List<String> single = dataList.get(i);
row = sheet.getRow(rowLine + i);
for (int j = 0; j < single.size(); j++) {
cell = row.getCell(columnPosition.get(j + 1));
if (StringUtils.isNotEmpty(single.get(j))) {
cell.setCellValue(single.get(j));
} else {
cell.setCellValue("");
}
}
}
workbook.write(new File(excelPath));
workbook.close();
}
/**
* 替换
*
* @param dataMap
* @param filePath
* @param isSignImage
*/
@SneakyThrows
public static void replace(Map<String, String> dataMap, String filePath, boolean isSignImage) {
//读取源文件
File file = new File(filePath);
FileInputStream inputStream = new FileInputStream(file);
//构建工作簿
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
int sheetCount = workbook.getNumberOfSheets();
dataMap.put("${total}", String.valueOf(sheetCount));
int formRowCount = 0;
int cellCount = 0;
HSSFSheet sheet = null;
Row row = null;
Cell cell = null;
HSSFPatriarch patriarch = null;
for (int i = 0; i < sheetCount; i++) {
dataMap.put("${ye}", String.valueOf(i+1));
//读取表单
sheet = workbook.getSheetAt(i);
patriarch = sheet.createDrawingPatriarch();
formRowCount = sheet.getPhysicalNumberOfRows();
for (int j = 0; j < formRowCount; j++) {
//读取行
row = sheet.getRow(j);
cellCount = row.getPhysicalNumberOfCells();
for (int k = 0; k < cellCount; k++) {
//读取每一行的单元格
cell = row.getCell(k);
if (Objects.isNull(cell)) {
continue;
}
if (isSignImage) {
//签图片
signImage(dataMap, cell, patriarch, workbook);
} else {
//普通替换
replaceWords(dataMap, cell,workbook);
}
}
}
}
workbook.write(new File(filePath));
workbook.close();
inputStream.close();
}
/**
* 替换文字
*
* @param dataMap
* @param cell
*/
public static void replaceWords(Map<String, String> dataMap, Cell cell,HSSFWorkbook workbook) {
if (cell.getCellTypeEnum().equals(CellType.STRING)) {
String cellValue = cell.getStringCellValue();
String replaceValue = "";
for (Map.Entry<String, String> entry : dataMap.entrySet()) {
if (cellValue.contains(entry.getKey())) {
if (!Objects.isNull(entry.getValue())) {
replaceValue = entry.getValue();
if (Objects.equals("true", replaceValue)) {
replaceValue = "是";
} else if (Objects.equals("false", replaceValue)) {
replaceValue = "否";
}
}
cell.setCellValue(cellValue.replace(entry.getKey(), replaceValue));
CellStyle oldCellStyle = workbook.createCellStyle();
// 从现有样式克隆style,只修改Font,其它style不变
oldCellStyle.cloneStyleFrom(cell.getCellStyle());
// 获取原有字体
Font oldFont = workbook.getFontAt(oldCellStyle.getFontIndexAsInt());
// 创建新字体
Font newFont = workbook.createFont();
newFont.setFontName(oldFont.getFontName());
newFont.setFontHeightInPoints(oldFont.getFontHeightInPoints());
newFont.setColor(IndexedColors.BLACK.getIndex());
// 设置字体
oldCellStyle.setFont(newFont);
// 设置样式
cell.setCellStyle(oldCellStyle);
cellValue = cell.getStringCellValue();
}
}
}
}
/***
*
* Excel转化成PDF
*
* @param inputFile
* @param pdfFile
* @return
*/
private static void Ex2PDF(String inputFile, String pdfFile) {
ActiveXComponent ax = new ActiveXComponent("Excel.Application");
Dispatch excel = null;
try {
ComThread.InitSTA(true);
log.info("开始转化Excel为PDF...");
ax.setProperty("Visible", false);
// 禁用宏
ax.setProperty("AutomationSecurity", new Variant(3));
Dispatch excels = ax.getProperty("Workbooks").toDispatch();
excel = Dispatch
.invoke(excels, "Open", Dispatch.Method,
new Object[]{inputFile, new Variant(false), new Variant(false)}, new int[9])
.toDispatch();
// 转换格式
// PDF格式=0
Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[]{new Variant(0),
// 0=标准 (生成的PDF图片不会变模糊) 1=最小文件
pdfFile, new Variant(xlTypePDF)
// (生成的PDF图片糊的一塌糊涂)
}, new int[1]);
} catch (Exception e) {
log.error("========Error:Excel文件转换失败:" + e.getMessage());
} finally {
Dispatch.call(excel, "Close", new Variant(false));
log.info("关闭excel文件");
if (ax != null) {
ax.invoke("Quit", new Variant[]{});
}
ComThread.Release();
ComThread.quitMainSTA();
}
}
/**
* 删除制定的excel sheet表单
*
* @param excelPath
* @param sheetNumber
* @throws IOException
*/
@SneakyThrows
public static void deleteSheet(String excelPath, int sheetNumber) {
//读取源文件
File file = new File(excelPath);
FileInputStream inputStream = new FileInputStream(file);
//构建工作簿
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
workbook.removeSheetAt(sheetNumber);
workbook.write(new File(excelPath));
workbook.close();
}
/**
* #合并多个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文件合并成功");
}
/**
* #复制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 = 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 {
}
}
}
package com.yonde.dcs.plan.core.controller;
import com.yonde.dex.context.plugin.common.entity.DxContextVOHolder;
import org.springframework.stereotype.Controller;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
......@@ -25,7 +26,7 @@ import org.springframework.stereotype.Controller;
@RequestMapping("/ExtIDEPlan")
@ResponseBody()
@Controller(ExtIDEPlanController.BEAN_NAME)
public class ExtIDEPlanController<V extends ExtIDEPlanVO, S extends ExtIDEPlanService<V>> extends ExtIDEPlanControllerShadow<V, S> {
public class ExtIDEPlanController<V extends ExtIDEPlanVO & DxContextVOHolder, S extends ExtIDEPlanService<V>> extends ExtIDEPlanControllerShadow<V, S> {
}
......
package com.yonde.dcs.plan.core.controller.shadow;
import com.yonde.dex.context.plugin.common.entity.DxContextVOHolder;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import java.util.List;
......@@ -29,7 +30,7 @@ import com.yonde.dex.basedata.entity.api.ApiResult;
* @version: V
* @date: 2024-8-29 9:31:22
**/
public class ExtIDEPlanControllerShadow<V extends ExtIDEPlanVO, S extends ExtIDEPlanService<V>> implements AbstractBaseController<V, S> ,AbstractLifecycleBaseController<V, S> ,AbstractVersionBaseController<V, S> ,DxObjFileLinkController<V, S>{
public class ExtIDEPlanControllerShadow<V extends ExtIDEPlanVO & DxContextVOHolder, S extends ExtIDEPlanService<V>> implements AbstractBaseController<V, S> ,AbstractLifecycleBaseController<V, S> ,AbstractVersionBaseController<V, S> ,DxObjFileLinkController<V, S>{
public static final String BEAN_NAME = "extIDEPlanController";
......
......@@ -2,12 +2,14 @@ package com.yonde.dcs.plan.core.service;
import com.yonde.dcs.plan.common.vo.ExtIDEPlanVO;
import com.yonde.dcs.plan.core.service.shadow.ExtIDEPlanServiceShadow;
import com.yonde.dex.context.plugin.common.entity.DxContextVOHolder;
/**
* @description: ExtIDEPlan-service
* @author: dexadmin
* @version: V
* @date: 2024-8-29 9:31:22
**/
public interface ExtIDEPlanService<V extends ExtIDEPlanVO> extends ExtIDEPlanServiceShadow<V> {
public interface ExtIDEPlanService<V extends ExtIDEPlanVO & DxContextVOHolder> extends ExtIDEPlanServiceShadow<V> {
}
package com.yonde.dcs.plan.core.service.impl;
import com.yonde.dex.context.plugin.common.entity.DxContextVOHolder;
import org.springframework.stereotype.Service;
import io.swagger.annotations.ApiOperation;
import java.util.List;
......@@ -21,7 +22,7 @@ import javax.annotation.Resource;
**/
@Slf4j
@Service(ExtIDEPlanServiceImpl.BEAN_NAME)
public class ExtIDEPlanServiceImpl<V extends ExtIDEPlanVO> implements ExtIDEPlanService<V>{
public class ExtIDEPlanServiceImpl<V extends ExtIDEPlanVO & DxContextVOHolder> implements ExtIDEPlanService<V>{
public static final String BEAN_NAME = "extIDEPlanServiceImpl";
......
......@@ -2,17 +2,19 @@ package com.yonde.dcs.plan.core.service.shadow;
import com.yonde.dcs.plan.common.vo.ExtIDEPlanVO;
import com.yonde.dcs.plan.feign.ExtIDEPlanServiceFeign;
import com.yonde.dex.context.plugin.common.entity.DxContextVOHolder;
import com.yonde.dex.dao.service.BaseIdEntityService;
import com.yonde.dex.secretcode.plugin.core.service.SecretCodePluginService;
import com.yonde.dex.version.plugin.core.service.IterationService;
import com.yonde.dex.context.plugin.core.service.ContextPluginService;
import com.yonde.dex.lcycle.plugin.core.service.LifecycleBaseService;
import com.yonde.dex.dfs.objfilelink.plugin.core.service.ObjFileLinkPluginService;
/**
* @description: ExtIDEPlan-service
* @author: dexadmin
* @version: V
* @date: 2024-8-29 9:31:22
* @date: 2024-8-30 10:28:37
**/
public interface ExtIDEPlanServiceShadow<V extends ExtIDEPlanVO> extends ExtIDEPlanServiceFeign<V>, ObjFileLinkPluginService<V> ,LifecycleBaseService<V> ,BaseIdEntityService<V> ,SecretCodePluginService<V> ,IterationService<V> {
public interface ExtIDEPlanServiceShadow<V extends ExtIDEPlanVO & DxContextVOHolder> extends ExtIDEPlanServiceFeign<V>, ContextPluginService<V> ,ObjFileLinkPluginService<V> ,LifecycleBaseService<V> ,BaseIdEntityService<V> ,SecretCodePluginService<V> ,IterationService<V> {
}
......@@ -8,7 +8,7 @@ import com.yonde.dex.dao.service.BaseIdEntityService;
* @description: ExtIEDPlanDocLink-service
* @author: dexadmin
* @version: V
* @date: 2024-8-29 9:31:22
* @date: 2024-8-30 10:28:37
**/
public interface ExtIEDPlanDocLinkServiceShadow<V extends ExtIEDPlanDocLinkVO> extends ExtIEDPlanDocLinkServiceFeign<V>, BaseIdEntityService<V> ,DxLinkDataService<V> {
......
......@@ -3,7 +3,9 @@ package com.yonde.dcs.plan.core.service.shadow;
import com.yonde.dcs.plan.common.vo.ExtPlanVO;
import com.yonde.dcs.plan.feign.ExtPlanServiceFeign;
import com.yonde.dex.dao.service.BaseIdEntityService;
import com.yonde.dex.secretcode.plugin.core.service.SecretCodePluginService;
import com.yonde.dex.version.plugin.core.service.IterationService;
import com.yonde.dex.context.plugin.core.service.ContextPluginService;
import com.yonde.dex.lcycle.plugin.core.service.LifecycleBaseService;
import com.yonde.dex.dfs.objfilelink.plugin.core.service.ObjFileLinkPluginService;
import com.yonde.dex.tree.plugin.core.service.BaseTreeService;
......@@ -11,8 +13,8 @@ import com.yonde.dex.tree.plugin.core.service.BaseTreeService;
* @description: ExtPlan-service
* @author: dexadmin
* @version: V
* @date: 2024-7-26 9:34:06
* @date: 2024-8-30 10:28:36
**/
public interface ExtPlanServiceShadow<V extends ExtPlanVO> extends ExtPlanServiceFeign<V>, ObjFileLinkPluginService<V> ,BaseTreeService<V> ,LifecycleBaseService<V> ,BaseIdEntityService<V> ,IterationService<V> {
public interface ExtPlanServiceShadow<V extends ExtPlanVO> extends ExtPlanServiceFeign<V>, ContextPluginService<V> ,ObjFileLinkPluginService<V> ,BaseTreeService<V> ,LifecycleBaseService<V> ,BaseIdEntityService<V> ,IterationService<V> ,SecretCodePluginService<V> {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment