parent
7ff7e30065
commit
e27972add2
|
@ -1,15 +1,22 @@
|
|||
package shkd.repc.recos.formplugin;
|
||||
|
||||
import kd.bos.context.RequestContext;
|
||||
import kd.bos.dataentity.entity.DynamicObject;
|
||||
import kd.bos.dataentity.entity.DynamicObjectCollection;
|
||||
import kd.bos.dataentity.serialization.SerializationUtils;
|
||||
import kd.bos.dataentity.utils.StringUtils;
|
||||
import kd.bos.entity.EntityMetadataCache;
|
||||
import kd.bos.entity.MainEntityType;
|
||||
import kd.bos.entity.datamodel.IDataModel;
|
||||
import kd.bos.entity.plugin.Plugin;
|
||||
import kd.bos.fileservice.FileItem;
|
||||
import kd.bos.fileservice.FileService;
|
||||
import kd.bos.fileservice.FileServiceFactory;
|
||||
import kd.bos.form.FormShowParameter;
|
||||
import kd.bos.form.IFormView;
|
||||
import kd.bos.form.control.AttachmentPanel;
|
||||
import kd.bos.form.control.Control;
|
||||
import kd.bos.form.control.events.BeforeItemClickEvent;
|
||||
import kd.bos.form.control.events.UploadListener;
|
||||
import kd.bos.form.events.AfterDoOperationEventArgs;
|
||||
import kd.bos.form.events.BeforeDoOperationEventArgs;
|
||||
|
@ -29,12 +36,15 @@ import kd.repc.recos.business.measure.ReMeasureIdxUtil;
|
|||
import kd.repc.recos.formplugin.measure.base.ReMeasureEditTplPlugin;
|
||||
import kd.repc.recos.formplugin.measure.measureci.ReMeasureCIEditPlugin;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.xssf.usermodel.*;
|
||||
import shkd.repc.recos.domain.Calculation;
|
||||
|
||||
import java.io.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -46,10 +56,180 @@ public class RecosMeasurecosFormPlugin extends AbstractFormPlugin implements Upl
|
|||
public void registerListener(EventObject e) {
|
||||
super.registerListener(e);
|
||||
//注册附件监听
|
||||
this.addItemClickListeners("tbmain");
|
||||
AttachmentPanel attachmentPanel = getControl("attachmentpanelap");
|
||||
attachmentPanel.addUploadListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeItemClick(BeforeItemClickEvent evt) {
|
||||
// TODO Auto-generated method stub
|
||||
super.beforeItemClick(evt);
|
||||
if (evt.getItemKey().equals("qeug_out")) {
|
||||
this.getView().invokeOperation("save");//调用保存
|
||||
getPageAllData("recos_measurecost");
|
||||
}
|
||||
}
|
||||
public void getPageAllData(String formId){
|
||||
//创建excel的map集合
|
||||
Map<String, List> result = new HashMap<>();
|
||||
|
||||
Object id = this.getModel().getValue("id");
|
||||
QFilter filter = new QFilter("measurecostid", QCP.equals, id);
|
||||
DynamicObjectCollection query = QueryServiceHelper.query("recos_measureci", "id",filter.toArray());//获取页面对应的建安测算
|
||||
for (DynamicObject queryOne : query) {
|
||||
List list=new ArrayList();
|
||||
List listcaption=new ArrayList();
|
||||
listcaption.add("科目长名称");
|
||||
listcaption.add("科目编码");
|
||||
listcaption.add("科目名称");
|
||||
listcaption.add("工程量单位");
|
||||
listcaption.add("工程量");
|
||||
listcaption.add("调整系数");
|
||||
listcaption.add("单价");
|
||||
listcaption.add("合价(含税)");
|
||||
list.add(listcaption);
|
||||
Object fid = queryOne.get("id");
|
||||
DynamicObject single = BusinessDataServiceHelper.loadSingle(fid,"recos_measureci");
|
||||
DynamicObjectCollection cientry = single.getDynamicObjectCollection("cientry");
|
||||
String sheetname= cientry.get(0).getDynamicObject("entry_producttype").getString("name");//产品类型名称
|
||||
for (DynamicObject dynamicObject : cientry) {
|
||||
DynamicObject entry_costaccount = dynamicObject.getDynamicObject("entry_costaccount");//科目
|
||||
if (entry_costaccount.getBoolean("isleaf")){
|
||||
List listvalue=new ArrayList();
|
||||
listvalue.add(entry_costaccount.getString("fullname"));//科目长名称
|
||||
listvalue.add(dynamicObject.getString("entry_longnumber"));//科目编码
|
||||
listvalue.add(dynamicObject.getString("entry_accountname"));//科目名称
|
||||
DynamicObject entry_workloadunit = dynamicObject.getDynamicObject("entry_workloadunit");
|
||||
if (entry_workloadunit!=null){
|
||||
listvalue.add(dynamicObject.getDynamicObject("entry_workloadunit").getString("name"));//单位
|
||||
}else {
|
||||
listvalue.add("");
|
||||
}
|
||||
listvalue.add(dynamicObject.getBigDecimal("entry_workload"));//工程量
|
||||
BigDecimal entry_adjustcoefficient = dynamicObject.getBigDecimal("entry_adjustcoefficient").multiply(new BigDecimal(100));
|
||||
listvalue.add(entry_adjustcoefficient.toString()+"%");//调整系数
|
||||
listvalue.add(dynamicObject.getBigDecimal("entry_price"));//单价
|
||||
listvalue.add(dynamicObject.getBigDecimal("entry_amount"));//合价(含税)
|
||||
|
||||
list.add(listvalue);
|
||||
}
|
||||
}
|
||||
|
||||
result.put(sheetname,list);
|
||||
}
|
||||
|
||||
// 创建excel
|
||||
XSSFWorkbook workbook = excel(result, "项目估算表");
|
||||
// 上传
|
||||
String path = upload("data", workbook);
|
||||
|
||||
//下载
|
||||
getView().openUrl(RequestContext.get().getClientFullContextPath() + "/attachment/download.do?path=" + path);
|
||||
}
|
||||
|
||||
|
||||
private XSSFWorkbook excel(Map<String, List> result, String title) {
|
||||
//创建excel工作簿
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
int index=0;
|
||||
for (Map.Entry<String, List> entry : result.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
List<List<String>> excel= entry.getValue();
|
||||
|
||||
|
||||
//创建工作表sheet
|
||||
XSSFSheet sheet = workbook.createSheet();
|
||||
|
||||
//设置默认列宽
|
||||
sheet.setDefaultColumnWidth(20);
|
||||
sheet.setColumnWidth(0, 50 * 256);
|
||||
// 创建标题
|
||||
XSSFRow headRow = sheet.createRow(0);
|
||||
headRow.setHeightInPoints(30);
|
||||
XSSFCell headCell = headRow.createCell(0);
|
||||
headCell.setCellValue(title);
|
||||
|
||||
// 设置首行标题的一些风格样式
|
||||
XSSFCellStyle titleStyle = workbook.createCellStyle();
|
||||
// 设置水平居中
|
||||
titleStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
// 设置垂直居中
|
||||
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
|
||||
headCell.setCellStyle(titleStyle);
|
||||
|
||||
// 合并第1行的前几列,合并列数 = excel的列数
|
||||
CellRangeAddress titleCellAddresses = new CellRangeAddress(0, 0, 0, excel.get(0).size()-1);
|
||||
sheet.addMergedRegion(titleCellAddresses);
|
||||
|
||||
// 单据列表数据风格样式,设置字体为黑体,字号15
|
||||
XSSFCellStyle billStyle = workbook.createCellStyle();
|
||||
XSSFFont billFont = workbook.createFont();
|
||||
billFont.setFontName("黑体");
|
||||
billFont.setFontHeight(15);
|
||||
billStyle.setFont(billFont);
|
||||
//写入单据列表数据
|
||||
for (int i = 0; i < excel.size(); i++) {
|
||||
// i+1是因为前面第1行加了一个标题,单据列表数据是从Excel的第2行开始的,所以要+1
|
||||
XSSFRow nrow = sheet.createRow(i+1);
|
||||
for (int u=0;u<excel.get(i).size();u++){
|
||||
XSSFCell ncell = nrow.createCell(u);
|
||||
ncell.setCellStyle(billStyle);
|
||||
Object s = excel.get(i).get(u);
|
||||
if (s instanceof BigDecimal) {
|
||||
BigDecimal b=(BigDecimal)s;
|
||||
ncell.setCellValue(b.doubleValue());
|
||||
}else {
|
||||
ncell.setCellValue(excel.get(i).get(u));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
workbook.setSheetName(index,key);
|
||||
index++;
|
||||
}
|
||||
|
||||
return workbook;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param entityName
|
||||
* @param workbook
|
||||
* @return path 路径
|
||||
*/
|
||||
private String upload (String entityName, XSSFWorkbook workbook) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
|
||||
String fileName = entityName + 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();
|
||||
String path = fs.upload(new FileItem(fileName, pathName, inputStream));
|
||||
|
||||
return path;
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* outputStream转inputStream
|
||||
* @param out outputStream
|
||||
* @return inputStream
|
||||
* @throws Exception
|
||||
*/
|
||||
public ByteArrayInputStream parse(final OutputStream out) throws Exception {
|
||||
ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
|
||||
final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
|
||||
return swapStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeDoOperation(BeforeDoOperationEventArgs args) {
|
||||
super.beforeDoOperation(args);
|
||||
|
|
Loading…
Reference in New Issue