[password]123456[/password]
[code]
[Description("动态表单插件:物料清单正查 --- 导出Excel")]
public class BomQueryForwardDynamicBillPlugin : AbstractDynamicFormPlugIn
{
private List_exportData = new List (); public override void BarItemClick(BarItemClickEventArgs e)
{
base.BarItemClick(e);
if (e.BarItemKey.EqualsIgnoreCase("tbExcelOut"))
{
ExcelOut();
}
}#region 数据导出
private void ExcelOut()
{
string entryKey = "FBottomEntity";EntryEntity entry = this.View.BillBusinessInfo.GetEntryEntity(entryKey);
EntityDataExport(this.View.Model.GetEntityDataObject(entry).ToList());
EntityAppearance entityAppearance = this.View.LayoutInfo.GetEntryEntityAppearance(entryKey);AnalysisContext context = new AnalysisContext()
{
View = this.View,
Context = this.View.Context,
FalseText = "否",
TrueText = "是"
}; //分析字段DataSet dataSet = new DataSet();
#region 新建表
DataTable dt = new DataTable(); //单据体
dt.TableName = "物料清单";Dictionary
fieldDictionary = new Dictionary (); //excel 列
ListcolumnNames = new List ();//存储Excel表头名称 #region 构建数据列 字段标识--列名
fieldDictionary.Add("FBomLevel", "BOM层级");
fieldDictionary.Add("FMaterialId2", "子项物料编码");
fieldDictionary.Add("FMaterialName2", "物料名称");
fieldDictionary.Add("FMaterialModel2", "规格型号");
fieldDictionary.Add("FBomId2", "BOM版本");
fieldDictionary.Add("FAuxPropId", "辅助属性");
fieldDictionary.Add("FErpClsID", "物料属性");
fieldDictionary.Add("F_YEA_Gy", "工艺");
fieldDictionary.Add("FUnitId2", "单位");
fieldDictionary.Add("FNumerator", "用量:分子");
fieldDictionary.Add("FDenominator", "用量:分母");
fieldDictionary.Add("FScrapRate", "变动损耗率");
fieldDictionary.Add("FPositionNo", "位置号");
fieldDictionary.Add("F_YEA_Station", "工位");
fieldDictionary.Add("FReplaceType", "替代方式");
fieldDictionary.Add("FSTOCKID", "发料仓");
fieldDictionary.Add("F_YEA_Gjqj", "关键器件");
fieldDictionary.Add("F_YEA_Coil", "继承判定");
fieldDictionary.Add("F_YEA_Control", "是否管控");
fieldDictionary.Add("F_YEA_Tqq", "到货周期");
fieldDictionary.Add("F_YEA_New", "新材料");
fieldDictionary.Add("FMemo", "备注");
fieldDictionary.Add("FEffectDate", "生效日期");#endregion
foreach (var dict in fieldDictionary)
{
#region 处理Excel 样式Func
predicate = null;
Field field = this.View.BusinessInfo.GetField(dict.Key);
Type type = field.GetType();
if (predicate.IsNullOrEmptyOrWhiteSpace())
{
predicate = x => x.Key == field.Key;
}
Appearance appearance2 = entityAppearance.Layoutinfo.Appearances.FirstOrDefault(predicate);
ExcelField item = new ExcelField
{
Caption = dict.Value,
FieldName = dict.Key,
ColumnType = typeof (string),
Width = Convert.ToInt32(appearance2.Width.ToString()),
Align =
(((type == typeof (DecimalField)) || (type == typeof (IntegerField))) ||
(type == typeof (AmountField)))
? 1
: 0,
};#endregion
columnNames.Add(item); //添加Excel 列名
dt.Columns.Add(dict.Key);//添加临时表 字段
}#endregion
#region 填充表体数据
for (int i = 0; i < this._exportData.Count(); i++)
{
DynamicObject dyobj = _exportData[i];
if (!dyobj.IsNullOrEmpty())
{
#region dt 新增数据DataRow dr = dt.NewRow();
foreach (DataColumn column in dt.Columns.Cast
())
{
Field field = this.View.BusinessInfo.GetField(column.ColumnName);
if (!field.IsNullOrEmptyOrWhiteSpace())
{
dr[column.ColumnName] =
Convert.ToString(context.GetFieldValue(field, field.GetType(), dyobj, field.PropertyName));
}
}dt.Rows.Add(dr);
#endregion
}
}#endregion
dataSet.Tables.Add(dt);
#region 生成Excel文件
ExcelOperation helper = new ExcelOperation(this.View);
helper.BeginExport();
helper.FillMergeHeaders(columnNames, 0); //插入表头
helper.ExportToFile(columnNames, dt, null);string fileName = string.Format("{0}_{1}", this.Model.BusinessInfo.GetForm().Name,
DateTime.Now.ToString("yyyyMMddHHmmssff"));string[] illegalStrs = new string[] {"/", "\\"};
foreach (var str in illegalStrs)
{
fileName = fileName.Replace(str, "");
}fileName = PathUtils.GetValidFileName(fileName);
string filePath = PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, fileName);
string outServicePath = PathUtils.GetServerPath(KeyConst.TEMPFILEPATH, fileName);
filePath += ".xls";
outServicePath += ".xls";
//生成Excel文件
helper.EndExport(filePath, SaveFileType.XLS);//下载文件
DynamicFormShowParameter param = new DynamicFormShowParameter();
param.FormId = "BOS_FileDownLoad";
param.OpenStyle.ShowType = ShowType.Modal;
param.CustomParams.Add("IsExportData", "true");
param.CustomParams.Add("url", outServicePath);this.View.ShowForm(param);
#endregion
}
#endregion
#region 处理 BOM 级别
private void EntityDataExport(List
bomPrintChildItems)
{
_exportData = new List();
if ((bomPrintChildItems != null) && (bomPrintChildItems.Count > 0))
{
foreach (DynamicObject obj2 in bomPrintChildItems)
{
if (obj2.GetDynamicValue("BomLevel", 0) == 0)
{
this.RebuildCollection(bomPrintChildItems.ToList(), obj2);
}
}
}
}private void RebuildCollection(List
lstDo, DynamicObject currentDo)
{
Listsource = (from c in lstDo
where Convert.ToString(c["ParentEntryId"]) == Convert.ToString(currentDo["EntryId"])
select c).ToList();
if ((source != null) && (source.Count >= 1))
{
if (currentDo.GetDynamicValue("BomLevel", null) == "0")
{
this._exportData.Add(currentDo);
}
using (List.Enumerator enumerator = source.GetEnumerator())
{
while (enumerator.MoveNext())
{
Funcpredicate = null;
DynamicObject tmpDo = enumerator.Current;
bool flag = this.View.Model.GetValue("FisShowNumber", -1, false, null);
DynamicObject dynamicObject = (DynamicObject) tmpDo.Clone(false, false);
if (flag)
{
Funcfunc = null;
int count = dynamicObject.GetDynamicObjectItemValue("BomLevel", 0);
if (tmpDo.GetDynamicValue("MaterialType", 0) == 3)
{
if (func == null)
{
func = z => this.countNumber(z.GetDynamicValue("BomLevel", null)) == count;
}
dynamicObject["BomLevel"] =
this._exportData.Where(func)
.Last()
.GetDynamicValue("BomLevel", null);
}
else
{
if (predicate == null)
{
predicate =
z =>
(z.GetDynamicValue("MaterialType", 0) != 3) &&
(z.GetDynamicValue("BomLevel", null) ==
tmpDo.GetDynamicValue("BomLevel", null));
}
dynamicObject["BomLevel"] = ((currentDo.GetDynamicValue("BomLevel", null) == "0")
? "1"
: currentDo.GetDynamicValue("BomLevel", null)) + "." +
(source.Where(predicate)
.ToList()
.IndexOf(tmpDo) + 1);
}
}
else
{
dynamicObject["BomLevel"] = this.GetTreeFormat(dynamicObject);
}
this._exportData.Add(dynamicObject);
lstDo.Remove(tmpDo);
this.RebuildCollection(lstDo, dynamicObject);
}
}
}
}private int countNumber(string p)
{
int num = 0;
foreach (char ch in p)
{
if (ch == '.')
{
num++;
}
}
return num;
}private string GetTreeFormat(DynamicObject dynamicObject)
{
StringBuilder builder = new StringBuilder();
long num = Convert.ToInt64(dynamicObject["BomLevel"].ToString());
for (int i = 0; i < num; i++)
{
builder.Append('.');
}
builder.Append(num.ToString());
return builder.ToString();
}#endregion
}
}
[/code]
YEA.BOS.Business.rar(24.88KB)