189 lines
8.2 KiB
Java
189 lines
8.2 KiB
Java
|
package shkd.repc.recon.formplugin;
|
|||
|
|
|||
|
import kd.bos.context.RequestContext;
|
|||
|
import kd.bos.dataentity.entity.DynamicObject;
|
|||
|
import kd.bos.dataentity.entity.DynamicObjectCollection;
|
|||
|
import kd.bos.dataentity.utils.StringUtils;
|
|||
|
import kd.bos.fileservice.FileItem;
|
|||
|
import kd.bos.fileservice.FileService;
|
|||
|
import kd.bos.fileservice.FileServiceFactory;
|
|||
|
import kd.bos.form.control.events.ItemClickEvent;
|
|||
|
import kd.bos.form.events.AfterDoOperationEventArgs;
|
|||
|
import kd.bos.form.plugin.AbstractFormPlugin;
|
|||
|
import kd.bos.orm.query.QCP;
|
|||
|
import kd.bos.orm.query.QFilter;
|
|||
|
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
|||
|
import kd.sdk.plugin.Plugin;
|
|||
|
import org.apache.poi.ss.usermodel.FillPatternType;
|
|||
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|||
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
|||
|
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|||
|
import org.apache.poi.xssf.usermodel.*;
|
|||
|
|
|||
|
import java.awt.*;
|
|||
|
import java.io.ByteArrayInputStream;
|
|||
|
import java.io.ByteArrayOutputStream;
|
|||
|
import java.io.InputStream;
|
|||
|
import java.io.OutputStream;
|
|||
|
import java.text.SimpleDateFormat;
|
|||
|
import java.util.ArrayList;
|
|||
|
import java.util.Date;
|
|||
|
import java.util.EventObject;
|
|||
|
import java.util.List;
|
|||
|
|
|||
|
/**
|
|||
|
* 二开导入模板
|
|||
|
*/
|
|||
|
public class ContractMaterialTemplatePlugin extends AbstractFormPlugin implements Plugin {
|
|||
|
|
|||
|
|
|||
|
@Override
|
|||
|
public void registerListener(EventObject e) {
|
|||
|
super.registerListener(e);
|
|||
|
//工具栏控件
|
|||
|
this.addItemClickListeners("qeug_advcontoolbarap21");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void afterDoOperation(AfterDoOperationEventArgs evt) {
|
|||
|
String operateKey = evt.getOperateKey();
|
|||
|
if (StringUtils.equals("exporttemplate",operateKey)){
|
|||
|
//根据导入模板配置导出数据
|
|||
|
QFilter number = new QFilter("number", QCP.equals, "recon_contractbill_IMPT_ENTRY");
|
|||
|
DynamicObject template = BusinessDataServiceHelper.loadSingle("bos_importentry_template", number.toArray());
|
|||
|
if (null == template) {
|
|||
|
this.getView().showMessage("请先维护引入模板");
|
|||
|
return;
|
|||
|
}
|
|||
|
//表头字段
|
|||
|
List<DynamicObject> list = new ArrayList<>();
|
|||
|
DynamicObjectCollection entry = template.getDynamicObjectCollection("treeentryentity");
|
|||
|
if (null != entry && entry.size() != 0) {
|
|||
|
for (int i = 0; i < entry.size(); i++) {
|
|||
|
DynamicObject dynamicObject = entry.get(i);
|
|||
|
if (dynamicObject.getBoolean("isimport")) {
|
|||
|
list.add(dynamicObject);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
if (list.size() == 0) return;
|
|||
|
//构建导出前四行数据
|
|||
|
|
|||
|
// 创建Excel工作簿
|
|||
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
|||
|
XSSFSheet sheet = workbook.createSheet("明细 # qeug_orderformentry");
|
|||
|
|
|||
|
// 第一行:明细标题
|
|||
|
XSSFRow row1 = sheet.createRow(0);
|
|||
|
XSSFCell cell1 = row1.createCell(0);
|
|||
|
cell1.setCellValue("明细 # qeug_orderformentry");
|
|||
|
XSSFCellStyle style1 = workbook.createCellStyle();
|
|||
|
XSSFFont font1 = workbook.createFont();
|
|||
|
font1.setFontName("宋体");
|
|||
|
font1.setFontHeightInPoints((short) 11);
|
|||
|
style1.setFont(font1);
|
|||
|
style1.setAlignment(HorizontalAlignment.CENTER);
|
|||
|
style1.setVerticalAlignment(VerticalAlignment.CENTER);
|
|||
|
sheet.setColumnWidth(0, 31 * 256); // 设置列宽
|
|||
|
row1.setHeightInPoints(14.4f);
|
|||
|
cell1.setCellStyle(style1);
|
|||
|
|
|||
|
// 第二行:说明文本
|
|||
|
XSSFRow row2 = sheet.createRow(1);
|
|||
|
XSSFCell cell2 = row2.createCell(0);
|
|||
|
cell2.setCellValue("1、请将鼠标移到灰色标题行查看字段录入要求。2、红色带星号(*)的字段为必录字段。");
|
|||
|
XSSFCellStyle style2 = workbook.createCellStyle();
|
|||
|
XSSFFont font2 = workbook.createFont();
|
|||
|
font2.setFontName("Calibri");
|
|||
|
font2.setFontHeightInPoints((short) 11);
|
|||
|
style2.setFont(font2);
|
|||
|
style2.setAlignment(HorizontalAlignment.LEFT);
|
|||
|
style2.setVerticalAlignment(VerticalAlignment.CENTER);
|
|||
|
style2.setWrapText(true); // 自动换行
|
|||
|
row2.setHeightInPoints(60);
|
|||
|
sheet.setColumnWidth(0, 31 * 256); // 设置列宽
|
|||
|
cell2.setCellStyle(style2);
|
|||
|
|
|||
|
// 第三行:list中的entitynumber
|
|||
|
XSSFRow row3 = sheet.createRow(2);
|
|||
|
for (int i = 0; i < list.size(); i++) {
|
|||
|
DynamicObject dynamicObject = list.get(i);
|
|||
|
String entityNumber = null;
|
|||
|
String importProp = dynamicObject.getString("importprop");
|
|||
|
if (!("").equals(importProp)) {
|
|||
|
entityNumber = dynamicObject.getString("entitynumber") + "." + dynamicObject.getString("importprop");
|
|||
|
} else {
|
|||
|
entityNumber = dynamicObject.getString("entitynumber");
|
|||
|
}
|
|||
|
|
|||
|
XSSFCell cell = row3.createCell(i);
|
|||
|
cell.setCellValue(entityNumber);
|
|||
|
XSSFCellStyle style3 = workbook.createCellStyle();
|
|||
|
XSSFFont font3 = workbook.createFont();
|
|||
|
font3.setFontName("宋体");
|
|||
|
font3.setFontHeightInPoints((short) 11);
|
|||
|
style3.setFont(font3);
|
|||
|
style3.setAlignment(HorizontalAlignment.CENTER);
|
|||
|
style3.setVerticalAlignment(VerticalAlignment.CENTER);
|
|||
|
sheet.setColumnWidth(i, 31 * 256); // 设置列宽
|
|||
|
row3.setHeightInPoints(14.4f);
|
|||
|
cell.setCellStyle(style3);
|
|||
|
}
|
|||
|
|
|||
|
// 第四行:list中的entityname
|
|||
|
XSSFRow row4 = sheet.createRow(3);
|
|||
|
for (int i = 0; i < list.size(); i++) {
|
|||
|
DynamicObject dynamicObject = list.get(i);
|
|||
|
String entityName = dynamicObject.getString("entitydescription");
|
|||
|
XSSFCell cell = row4.createCell(i);
|
|||
|
cell.setCellValue(entityName);
|
|||
|
XSSFCellStyle style4 = workbook.createCellStyle();
|
|||
|
XSSFFont font4 = workbook.createFont();
|
|||
|
font4.setFontName("Calibri");
|
|||
|
font4.setFontHeightInPoints((short) 11);
|
|||
|
font4.setColor(IndexedColors.RED.getIndex());
|
|||
|
style4.setFont(font4);
|
|||
|
style4.setAlignment(HorizontalAlignment.CENTER);
|
|||
|
style4.setVerticalAlignment(VerticalAlignment.CENTER);
|
|||
|
style4.setFillForegroundColor(new XSSFColor(new Color(192, 192, 192), new DefaultIndexedColorMap())); // 灰色背景
|
|||
|
style4.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|||
|
sheet.setColumnWidth(i, 31 * 256); // 设置列宽
|
|||
|
row4.setHeightInPoints(14.4f);
|
|||
|
cell.setCellStyle(style4);
|
|||
|
}
|
|||
|
// 上传Excel文件并获取下载路径
|
|||
|
String uploadedFilePath = uploadExcel(workbook);
|
|||
|
String downloadUrl = RequestContext.get().getClientFullContextPath() + "/attachment/download.do?path=" + uploadedFilePath;
|
|||
|
this.getView().openUrl(downloadUrl);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* 上传Excel文件并返回路径
|
|||
|
*/
|
|||
|
private String uploadExcel(XSSFWorkbook workbook) {
|
|||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
|
|||
|
String fileName = "导入模板_" + sdf.format(new Date()) + ".xlsx";
|
|||
|
String pathName = "/offices/" + fileName;
|
|||
|
try {
|
|||
|
OutputStream outputStream = new ByteArrayOutputStream();
|
|||
|
workbook.write(outputStream);
|
|||
|
InputStream inputStream = parse(outputStream);
|
|||
|
|
|||
|
FileService fs = FileServiceFactory.getAttachmentFileService();
|
|||
|
return fs.upload(new FileItem(fileName, pathName, inputStream));
|
|||
|
} catch (Exception e) {
|
|||
|
//logger.error("上传Excel失败", e);
|
|||
|
throw new RuntimeException("上传Excel失败", e);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 解析输出流为输入流
|
|||
|
*/
|
|||
|
public ByteArrayInputStream parse(final OutputStream out) throws Exception {
|
|||
|
ByteArrayOutputStream outputStream = (ByteArrayOutputStream) out;
|
|||
|
return new ByteArrayInputStream(outputStream.toByteArray());
|
|||
|
}
|
|||
|
}
|