CSFrameworkV6-导出Excel模版、导入数据设计
作者:C/S框架网|www.csframewo  发布日期:2023/04/20 13:13:41
  CSFrameworkV6-导出Excel模版、导入数据设计

适用版本:CSFrameworkV6 旗舰版
导出模版文件、导入模版数据下拉菜单设计:

贴图图片-导出导入excel




重写 InitButtons 方法,添加2个菜单按钮:


C# Code:

public override void InitButtons()
{
  
base.InitButtons();
  
  
var btnExportFile = this.ToolbarRegister.CreateButton(ButtonNameList.btnExportFile, LanLib.Get("导出模版文件"), ToolBarGroup.数据操作, Globals.LoadBitmap("32_excel_template.png"), new Size(57, 28), false, true, DoExportTemplate);
  
var btnImportFile = this.ToolbarRegister.CreateButton(ButtonNameList.btnImportFile, LanLib.Get("导入模版数据"), ToolBarGroup.数据操作, Globals.LoadBitmap("32_excel_import.png"), new Size(57, 28), false, true, DoImportExcelData);
  
  
var btnOwner = _buttons.GetButtonByName(ButtonNameList.btnExportDataDict);//父级按钮:导入/导出按钮
  
btnOwner.AddSubButton(btnExportFile);
  btnOwner.AddSubButton(btnImportFile);
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761




导出Excel模版文件Click事件:


C# Code:


public virtual void DoExportTemplate(IButtonInfo sender)
{
  
var file = Path.Combine(Globals.ApplicationPath, @"excel_templates\客户资料模版.xlsx");
  
if (File.Exists(file))
  {
    SaveFileDialog dlg
= new SaveFileDialog();
    dlg.Filter
= "Excel文件|*.xlsx";
    dlg.FileName
= DateTime.Today.ToString("yyyyMMdd") + " - 客户资料模版.xlsx";
    
if (dlg.ShowDialog().Value)
    {
      File.Copy(file, dlg.FileName,
true);
      
if (File.Exists(dlg.FileName))
      Msg.ShowInformation(
"导出模版文件成功!");
      
else
      Msg.Warning(
"导出失败!");
    }
  }
  
else
  {
    Msg.Warning(
"模版文件丢失(客户资料.xlsx),请联系管理员!");
  }
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761




导入Excel模版数据Click事件:


C# Code:


//导入客户资料按钮
public virtual void DoImportExcelData(IButtonInfo sender)
{
  IImporterTarget target
= new ImportCustomerFromExcel();
  frmImportExcel.ExecuteImporter(target);
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761




ImportCustomerFromExcel 类:



C# Code:


/// <summary>
/// 导入Excel数据
/// </summary>
public class ImportCustomerFromExcel : IImporterTarget
{
  bllCustomer _BLL;
  List
<dt_Customer> _List = new List<dt_Customer>();
  List
<dt_CommonDataDict> _Banks = new List<dt_CommonDataDict>();
  
  
public ImportCustomerFromExcel()
  {
    _BLL
= new bllCustomer();
    _Banks
= DataDictCache.Cache.CommonDataDict.Where(w => w.DataType == (int)CommonDataDictTypes.Bank).ToList();
    
    
this.TargetType = typeof(dt_Customer);
    
this.SourceType = typeof(CustomerDataSourceModel);
  }
  
  
/// <summary>
  
/// 字段映射关系
  
/// </summary>
  
/// <returns></returns>
  
public override List<FieldMapping> GetMapping()
  {
    List
<FieldMapping> list = new List<FieldMapping>();
    list.Add(
new FieldMapping("客户编码", nameof(dt_Customer.CustomerCode), typeof(String)));
    list.Add(
new FieldMapping("客户名称", nameof(dt_Customer.NativeName), typeof(String)));
    list.Add(
new FieldMapping("英文名", nameof(dt_Customer.EnglishName), typeof(String)));
    list.Add(
new FieldMapping("联系人", nameof(dt_Customer.ContactPerson), typeof(String)));
    list.Add(
new FieldMapping("手机号码", nameof(dt_Customer.PhoneNo), typeof(String)));
    list.Add(
new FieldMapping("电话号码", nameof(dt_Customer.Tel), typeof(String)));
    list.Add(
new FieldMapping("电子邮箱", nameof(dt_Customer.Email), typeof(String)));
    list.Add(
new FieldMapping("地址", nameof(dt_Customer.Address1), typeof(String)));
    list.Add(
new FieldMapping("开户银行", nameof(dt_Customer.Bank), typeof(String)));
    list.Add(
new FieldMapping("银行账号", nameof(dt_Customer.BankAccount), typeof(String)));
    list.Add(
new FieldMapping("是否在用", nameof(dt_Customer.InUse), typeof(String)));
    list.Add(
new FieldMapping("是否开票", nameof(dt_Customer.FlagInvoice), typeof(String)));
    list.Add(
new FieldMapping("税率", nameof(dt_Customer.TaxRate), typeof(Decimal)));
    list.Add(
new FieldMapping("备注", nameof(dt_Customer.Remark), typeof(String)));
    
return list;
  }
  
  
public override void ClearData()
  {
    
//_BLL.DeleteAll();
    
}
    
    
protected override bool BeginImport(IList dataSource)
    {
      
var list = dataSource.ToList<CustomerDataSourceModel>();
      
      
//检查Excel客户编码是否重复
      
var listDuplicated = list.GroupBy(g => g.客户编码).GetDuplicatedItems();
      
if (listDuplicated.Count > 0)
      {
        _ReportList.Add(
new Library.CommonForms.OperateReportItem
        {
          IsError
= true,
          StepText
= "检查Excel数据",
          Content
= "客户编码重复!" + String.Join(',', listDuplicated),
          RefID
= "",
          });
          
return false;
        }
        
return true;
      }
      
      
/// <summary>
      
/// 导入一条记录
      
/// </summary>
      
/// <param name="objectTarge"></param>
      
/// <param name="isCompleted"></param>
      
/// <param name="errMsg"></param>
      
/// <returns></returns>
      
protected override bool PostImportedData(object objectTarge, bool isCompleted, out string errMsg)
      {
        errMsg
= "";
        
        
try
        {
          
var customer = objectTarge as dt_Customer;
          
          
#region 校验数据
          
          StringBuilder sb
= new StringBuilder();
          
          
if (String.IsNullOrWhiteSpace(customer.CustomerCode))
          sb.AppendLine(
"客户编码不能为空!");
          
if (String.IsNullOrWhiteSpace(customer.NativeName))
          sb.AppendLine(
"客户名称不能为空!");
          
if (String.IsNullOrWhiteSpace(customer.PhoneNo))
          sb.AppendLine(
"手机号码不能为空!");
          
if (String.IsNullOrWhiteSpace(customer.BankAccount))
          sb.AppendLine(
"银行账号不能为空!");
          
if (String.IsNullOrWhiteSpace(customer.Bank))
          sb.AppendLine(
"开户银行不能为空!");
          
          
if (DataDictCache.Cache.CustomerLookup.Any(a => a.CustomerCode == customer.CustomerCode))
          sb.AppendLine($
"客户编码<{customer.CustomerCode}>重复!");
          
          
if (sb.Length > 0)
          {
            errMsg
= sb.ToString();
            
return false;
          }
          
          
#endregion
          
          
//给目标数据设置初始值
          
customer.AttributeCodes = "CUS";
          customer.isid
= IdHelper.GetId();
          
          _List.Add(customer);
          
          
//导入完成,一次提交数据
          
if (isCompleted)
          {
            
var data = new MasterDataUpdate<dt_Customer>();
            data.listAdded.AddRange(_List);
            
bool ok = _BLL.Update(data);
            
if (ok) DataDictCache.RefreshCache<dt_Customer>();
          }
          
          
return true;
        }
        
catch (Exception ex)
        {
          errMsg
= ex.Message;
          
return false;
        }
      }
    }
    
    
    
//来源:C/S框架网 | www.csframework.com | QQ:23404761




Excel模型:


C# Code:

/// <summary>
/// Excel数据源表结构
/// </summary>
public class CustomerDataSourceModel
{
  
public string 客户编码 { get; set; }
  
public string 客户名称 { get; set; }
  
public string 英文名 { get; set; }
  
public string 联系人 { get; set; }
  
public string 手机号码 { get; set; }
  
public string 电话号码 { get; set; }
  
public string 电子邮箱 { get; set; }
  
public string 地址 { get; set; }
  
public string 开户银行 { get; set; }
  
public string 银行账号 { get; set; }
  
public string 是否在用 { get; set; }
  
public string 是否开票 { get; set; }
  
public Decimal? 税率 { get; set; }
  
public string 备注 { get; set; }
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761




导入结果


贴图图片-导出导入excel1

C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务
上一篇 下一篇