导入Excel数据填充到单据体原创
金蝶云社区-比邻星
比邻星
0人赞赏了该文章 37次浏览 未经作者许可,禁止转载编辑于2024年09月30日 16:24:29
一、新建表单插件,点击按钮弹出动态表单,选择Excel进行导入
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.ServiceHelper.Excel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
namespace RD.K3.LD.Bussiness.PlugIn.NewBussiness.Import
{
    [System.ComponentModel.Description("导入Excel示例")]
    public class DeliverExpressImportJQ : AbstractBillPlugIn
    {
        StringBuilder str = new StringBuilder();
        public override void ButtonClick(ButtonClickEventArgs e)
        {
            base.ButtonClick(e);
            switch (e.Key.ToUpperInvariant())
            {
                case "F_PETL_BUTTON":
                    DynamicFormShowParameter showParam = new DynamicFormShowParameter();
                    showParam.FormId = "k67454b87db954fc8aa9829905ce8e04a";//动态表单唯一标识
                    this.View.ShowForm(showParam,
                    new Action<FormResult>((formResult) =>
                    {
                        if (formResult != null && formResult.ReturnData != null)
                        {
                            string fullFileName = formResult.ReturnData.ToString();//返回的Excel路径
                            this.FunDoImportEntry(fullFileName);
                        }
                    }));
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// excel实现导入方法
        /// </summary>
        /// <param name="param"></param>
        private void FunDoImportEntry(string fullFileName)
        {
            using (ExcelOperation helper = new ExcelOperation(this.View))
            {
                // 利用ExcelOperation对象,把xml文件,转为DataSet对象
                // 参数说明:
                // filePath : 完整的文件名,包含了物理目录
                // dataStartIndex : 数据开始行索引,从0开始。通常第一行为标题,第二行开始为数据行
                // colNameIndex : 列名所在行索引,从0开始。如此参数为0,表明第一行为列名行
                DataSet ds = helper.ReadFromFile(fullFileName, 1, 0);
                // 取第一个表格中的数据导入
                DataTable dt = ds.Tables[0];
                this.View.Model.DeleteEntryData("FEntity");
                int rowIndex = 0;
                foreach (DataRow dataRow in dt.Rows)
                {
                    string[] sArray_EntryId = dataRow["寄方备注"].ToString().Split(new char[1] { ',' });
                    // 单据体增加新行,并更新行索引
                    this.View.Model.CreateNewEntryRow("FEntity");
                    this.View.Model.SetEntryCurrentRowIndex("FEntity", rowIndex);
                    this.View.Model.SetValue("F_T_EntryId", sArray_EntryId[2] == null ? "" : sArray_EntryId[2], rowIndex);
                    this.View.Model.SetValue("F_T_ShipNoticeNo", sArray_EntryId[1]== null ? "" : sArray_EntryId[1], rowIndex);
                    this.View.Model.SetValue("F_T_ShipName", dataRow["快递公司"] == null ? "" : dataRow["快递公司"].ToString(), rowIndex);
                    this.View.Model.SetValue("F_T_ShipNo", dataRow["快递单号"] == null ? "" : dataRow["快递单号"].ToString(), rowIndex);
                    rowIndex++;
                }
                this.View.UpdateView("FEntity");
            }
        }
    }
}

动态表单插件

using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RD.K3.LD.Bussiness.PlugIn.NewBussiness.Import
{
    [Description("导入excel文件")]
    public class ExcelImport : AbstractDynamicFormPlugIn
    {
        /// <summary>
        /// 初始化,对其他界面传来的参数进行处理,对控件某些属性进行处理
        /// 这里不宜对数据DataModel进行处理
        /// </summary>
        /// <param name="e"></param>
        public override void OnInitialize(InitializeEventArgs e)
        {
        }
        /// <summary>
        /// 上传上来的文件名:完整的文件名,包含了物理路径
        /// </summary>
        private string _fullFileName = string.Empty;
        public override void AfterBindData(EventArgs e)
        {
            this.View.GetControl("F_PETL_Button").Enabled = false; // 确定按钮
        }
        /// <summary>
        /// 文件上传完毕,触发此事件:通过此事件获取上传上来的文件名
        /// </summary>
        /// <param name="e"></param>
        public override void CustomEvents(CustomEventsArgs e)
        {
            if (e.Key.EqualsIgnoreCase("F_PETL_FileUpdate")) //文件按钮
            {
                this.View.GetControl("F_PETL_FileUpdate").SetCustomPropertyValue("NeedCallback", true);
                this.View.GetControl("F_PETL_FileUpdate").SetCustomPropertyValue("IsRequesting", false);
                if (e.EventName.EqualsIgnoreCase("FileChanged"))
                {// 文件上传完毕
                    // 取文件上传参数,文件名
                    JSONObject uploadInfo = KDObjectConverter.DeserializeObject<JSONObject>(e.EventArgs);
                    if (uploadInfo != null)
                    {
                        JSONArray json = new JSONArray(uploadInfo["NewValue"].ToString());
                        if (json.Count > 0)
                        {
                            // 取上传的文件名
                            string fileName = (json[0] as Dictionary<string, object>)["ServerFileName"].ToString();
                            this._fullFileName = this.GetFullFileName(fileName);
                            // 解锁确定按钮
                            this.View.GetControl("F_PETL_Button").Enabled = true;
                        }
                        else
                        {
                            // 锁定确定按钮
                            this.View.GetControl("F_PETL_Button").Enabled = false;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 按钮点击事件
        /// </summary>
        /// <param name="e"></param>
        public override void ButtonClick(ButtonClickEventArgs e)
        {
            if (e.Key.EqualsIgnoreCase("F_PETL_Button"))
            {// 确定
                this.View.ReturnToParentWindow(new FormResult(this._fullFileName));
                this.View.Close();
            }
            else if (e.Key.EqualsIgnoreCase("F_PETL_Button1"))
            {// 取消
                this.View.Close();
            }
        }
        /// <summary>
        /// 产生完整的文件名,包含了物理路径
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private string GetFullFileName(string fileName)
        {
            string dir = "FileUpLoadServices\\UploadFiles";
            return PathUtils.GetPhysicalPath(dir, fileName);
        }
    }
}


赞 0