套打.二开案例.数据表格动态列+导出excel对齐输出
【场景】数据表格动态列,是根据列数平分宽度的。
部分客户想导出excel时对应的列是对齐的。
【案例】自定义单据,实现套打导出对齐
<0>数据准备
![Image_20230103152459.webp](/download/0100cfc69d25b2074ea9b4da93a0796ed7bf.webp)
<1>动态列案例
实现逻辑,记录这一批单据的最大列数,所有单据都按照最大列数显示,其中把有数据的列放在前面,没有数据的空出来
```csharp
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.NotePrint;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System.Collections.Generic;
using System.Linq;
namespace DynamicFormPlugIn.NotePrint
{
[Kingdee.BOS.Util.HotUpdate]
public class NotePrintColumnListPlugIn :AbstractListPlugIn
{
public class KeyConst
{
/// <summary>
/// 模板ID
/// </summary>
public static string TemplateId = "0ba2c446-58ed-4d33-a2aa-6ef9d3926bc0";
/// <summary>
/// 数据表格标识
/// </summary>
public static string DataGridId = "dataGrid1";
/// <summary>
/// 动态列标识
/// </summary>
public static string DynamicColumnId = "column3";
public static string EntityKey = "FEntity";
public static string FieldKey_Type = "F_BHR_Text";
public static string FieldKey_Size = "F_BHR_Decimal";
}
public class NotePrintArg
{
/// <summary>
/// 列数
/// </summary>
public int ColumnCnt;
/// <summary>
/// 类型-ID字典
/// </summary>
public Dictionary<string, ColumnInfo> TypeIdMap;
/// <summary>
/// 针对不同物料尺寸的个数(key)
/// </summary>
public Dictionary<int, int> IdSizeCntMap;
public NotePrintArg()
{
TypeIdMap = new Dictionary<string, ColumnInfo>();
IdSizeCntMap = new Dictionary<int, int>();
}
}
public class ColumnInfo
{
public int Id;
public string Caption;
public string Key;
}
private NotePrintArg notePrintArg = null;
/// <summary>
/// 动态列方法
/// </summary>
/// <param name="e"></param>
public override void OnQueryDynamicColumns(QueryDynamicColumnsEventArgs e)
{
if (e.DataGridId == KeyConst.DataGridId && e.DynamicColumnId == KeyConst.DynamicColumnId && e.DataSourceId == KeyConst.EntityKey)
{
notePrintArg = new NotePrintArg();
PrepareDynamicSize(notePrintArg);
if (notePrintArg == null || notePrintArg.ColumnCnt == 0)
return;
List<DynamicColumn> dynamicCols = new List<DynamicColumn>();
//创建以"NotePrint_TypeId"的自定义取值标识,用作绑定数据包显示
int columnCnt = 0;
foreach(var column in notePrintArg.TypeIdMap.Values)
{
var dyanmicColumn = new DynamicColumn()
{
Caption = column.Caption,
FieldKey = string.Format("NotePrint_{0}", column.Id),
IsStatistic = false
};
++columnCnt;
dynamicCols.Add(dyanmicColumn);
}
for (int i = columnCnt + 1; i <= notePrintArg.ColumnCnt; ++i)
{
var dyanmicColumn = new DynamicColumn()
{
Caption = string.Empty,
FieldKey = string.Format("NotePrint_{0}", i),
IsStatistic = false
};
dynamicCols.Add(dyanmicColumn);
}
e.DynamicColumns = dynamicCols;
}
}
/// <summary>
/// 套打数据包
/// </summary>
/// <param name="e"></param>
public override void OnPrepareNotePrintData(PreparePrintDataEventArgs e)
{
if (!e.NotePrintTplId.EqualsIgnoreCase(KeyConst.TemplateId))
return;
if (!e.DataSourceId.EqualsIgnoreCase(KeyConst.EntityKey) || e.DataObjects.Length == 0)
return;
if (notePrintArg == null || notePrintArg.ColumnCnt == 0)
return;
var objectType = e.DynamicObjectType;
for (int i = 1; i <= notePrintArg.ColumnCnt; ++i)
{
var key = string.Format("NotePrint_{0}", i);
objectType.RegisterProperty(key, typeof(string));
}
List<DynamicObject> displayData = new List<DynamicObject>();
var newRow = new DynamicObject(objectType);
var dataRow = e.DataObjects[0];
foreach (var prop in dataRow.DynamicObjectType.Properties)
{
newRow[prop] = dataRow[prop];
}
for (int i = 1; i <= notePrintArg.ColumnCnt; ++i)
{
var key = string.Format("NotePrint_{0}", i);
if (notePrintArg.IdSizeCntMap.ContainsKey(i))
{
newRow[key] = notePrintArg.IdSizeCntMap[i];
}
else
{
newRow[key] = string.Empty;
}
}
foreach (var sizeQtyItem in notePrintArg.IdSizeCntMap)
{
int columnId = sizeQtyItem.Key;
var key = string.Format("NotePrint_{0}", columnId);
newRow[key] = sizeQtyItem.Value;
}
displayData.Add(newRow);
e.DataObjects = displayData.ToArray();
}
private void ResetNotePrintInfo()
{
printJobList = null;
curJobIdx = 0;
curJobItemIdx = 0;
exportBillIds = null;
exportIdx = 0;
}
#region 打印相关处理
private List<PrintJob> printJobList;
private int curJobIdx = 0;
private int curJobItemIdx = 0;
public override void BeforeNotePrintCommand(BeforeNotePrintEventArgs e)
{
ResetNotePrintInfo();
if (e.PrintJobs == null)
return;
//保存下点击时对应模板的打印任务
List<PrintJob> filterPrintJobs = new List<PrintJob>();
foreach (var printJob in e.PrintJobs)
{
//节纸不支持不同单据的动态列不同,只会加载一次模板
if (printJob == null || printJob.IsEconomizePaper)
continue;
List<PrintJobItem> jobItems = new List<PrintJobItem>();
foreach (var jobItem in printJob.PrintJobItems)
{
if (!jobItem.TemplateId.EqualsIgnoreCase(KeyConst.TemplateId))
continue;
jobItems.Add(jobItem);
}
if (jobItems != null)
{
PrintJob filterJob = new PrintJob()
{
IsMergedPrint = printJob.IsMergedPrint,
PrintJobItems = jobItems
};
filterPrintJobs.Add(filterJob);
}
}
printJobList = filterPrintJobs;
curJobIdx = 0;
curJobItemIdx = 0;
}
#endregion
#region 导出相关处理
private List<string> exportBillIds;
private int exportIdx;
public override void BeforePrintExport(BeforePrintExportEventArgs e)
{
ResetNotePrintInfo();
if (e.ExportInfo == null)
return;
//todo 高级使用的是 e.ExportInfo.ExportItems
exportBillIds = e.ExportInfo.BillIds;
exportIdx = 0;
}
#endregion
private void PrepareDynamicSize(NotePrintArg arg)
{
if (printJobList != null)
{
PrepareDynamicSizeForPrint(arg);
}
else
{
PrepareDynamicSizeForExport(arg);
}
}
/// <summary>
/// 打印预览的参数准备
/// </summary>
/// <param name="arg"></param>
private void PrepareDynamicSizeForPrint(NotePrintArg arg)
{
/*
* 先要知道这个是哪个单,合并套打还是连续套打
* 这个代码确实有点复杂,如果不这么实现的话可以仅针对自定义菜单的时候才走这个逻辑
*/
if (printJobList.Count <= curJobIdx)
{
curJobIdx = 0;
curJobItemIdx = 0;
}
var printJob = printJobList[curJobIdx];
if (printJob == null || printJob.PrintJobItems == null)
return;
if (printJob.IsMergedPrint)
{
var idList = (from p in printJob.PrintJobItems select p.BillId).ToArray();
GetIdSize(idList.Distinct().ToArray(), arg);
++curJobIdx;
}
else
{
var printjobItem = printJob.PrintJobItems[curJobItemIdx];
var idList = new object[] { printjobItem.BillId };
GetIdSize(idList, arg);
++curJobItemIdx;
if (curJobItemIdx >= printJob.PrintJobItems.Count)
{
curJobItemIdx = 0;
++curJobIdx;
}
}
}
/// <summary>
/// 套打导出的参数准备
/// </summary>
/// <param name="arg"></param>
private void PrepareDynamicSizeForExport(NotePrintArg arg)
{
/*
* 先要知道这个是哪个单,合并套打还是连续套打
* 这个代码确实有点复杂,如果不这么实现的话可以仅针对自定义菜单的时候才走这个逻辑
*/
if (exportBillIds.Count <= exportIdx)
{
exportIdx = 0;
}
// 分录合并不支持,特此说明
var idList = new object[] { exportBillIds[exportIdx] };
GetIdSize(idList, arg);
++exportIdx;
if (exportIdx >= exportBillIds.Count)
{
++exportIdx;
}
}
/// <summary>
/// 计算需要的最大列数
/// </summary>
/// <returns></returns>
public int GetMaxColumnCnt()
{
//todo 获取当前这一批单据的最大项数
return 7;
}
/// <summary>
/// 获取对应单据内码的整单的尺寸集合(如果要计算选中分录的话,根据ListView.SelectRowsInfo过滤)
/// </summary>
/// <param name="pkArray"></param>
/// <param name="arg"></param>
private void GetIdSize(object[] pkArray, NotePrintArg arg)
{
DynamicObject[] billObjs = BusinessDataServiceHelper.Load(this.Context, pkArray, this.View.BillBusinessInfo.GetDynamicObjectType());
if (billObjs == null || billObjs.Length == 0)
return;
Entity entity = this.View.BillBusinessInfo.GetEntryEntity(KeyConst.EntityKey);
if (entity == null)
return;
arg.ColumnCnt = GetMaxColumnCnt();
var idSizeCntMap = arg.IdSizeCntMap;
Field typeField = this.View.BillBusinessInfo.GetField(KeyConst.FieldKey_Type);
Field sizeField = this.View.BillBusinessInfo.GetField(KeyConst.FieldKey_Size);
foreach (var billObj in billObjs)
{
DynamicObjectCollection entityObjectCollection = billObj[entity.EntryName] as DynamicObjectCollection;
foreach (var entityRow in entityObjectCollection)
{
//根据表单模型的单据数据包,获取每一分录的值,计算尺寸
string type = ObjectUtils.Object2String(entityRow[typeField.PropertyName]);
int size = ObjectUtils.Object2Int(entityRow[sizeField.PropertyName]);
if(!arg.TypeIdMap.ContainsKey(type))
{
ColumnInfo columnInfo = new ColumnInfo()
{
Id = arg.TypeIdMap.Values.Count + 1,
Caption = type,
Key = type,
};
arg.TypeIdMap[type] = columnInfo;
}
int id = arg.TypeIdMap[type].Id;
if(!arg.IdSizeCntMap.ContainsKey(id))
{
arg.IdSizeCntMap[id] = 0;
}
arg.IdSizeCntMap[id] += size;
}
}
}
}
}
```
![Image_20230103152327.webp](/download/0100df49b48084d840b7b93ecc7a1776ba5d.webp)
<2>如果为空不显示边线案例
[套打.脚本案例.内容为空时不打印边线](https://wenku.my7c.com/article/398079220309417216)
![Image_20230103152548.webp](/download/010058215955f9c24c57bdae2d05515fbc9c.webp)
【效果】
![Image_20230103152639.webp](/download/010035f33c3c391348f8af0cc746aa73b12c.webp)
套打.二开案例.数据表格动态列+导出excel对齐输出
【场景】数据表格动态列,是根据列数平分宽度的。部分客户想导出excel时对应的列是对齐的。【案例】自定义单据,实现套打导出对齐<0>数据准...
点击下载文档
本文2024-09-16 18:33:56发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-22977.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章