原创文档-Winform开发框架提供通用导入Excel功能界面|C/S框架网
原创文档-Winform开发框架提供通用导入Excel功能界面|C/S框架网
C/S框架网基于Winform开发框架所有版本提供通用导入Excel功能界面(frmImportExcel),解决导入不同表结构的Excel文件到数据库,为软件系统提供强大的技术支持。我们设计的ERP系统或MES系统,有不同的用户需求,需要系统支持导入用户自定义格式的Excel数据,若没有通用的导入界面,对于研发和维护存在很大的挑战。
参考Winform开发框架通用导入Excel功能界面:
1. 应用场景界面提供【Excel导入】按钮
2. Winform开发框架通用导入Excel功能界面
操作步骤:
【1】选择Excel文件,建议Office 2007格式(*.xls文件)
【2】点击【测试连接】
【3】选择一个Sheet表格(Sheet)
【4】点击【预览数据】,检查表格内的数据
【5】点击【开始导入】
按钮事件:
C# Code:
private void btnExcelImport_Click(object sender, EventArgs e)
{
//导入产品三层结构Excel数据
Import3LevelProductXLS target = new Import3LevelProductXLS();
//打开通用导入界面
frmImportExcel.ExecuteImporter(target, target.GetMapping());
if (!String.IsNullOrWhiteSpace(target.ErrorInfo))
{
Msg.Warning(target.ErrorInfo);//导入失败提示
}
else
{
//导入成功则刷新数据
if (target.LastImportCounter > 0)
btnQuery_Click(btnQuery, new EventArgs());
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
private void btnExcelImport_Click(object sender, EventArgs e)
{
//导入产品三层结构Excel数据
Import3LevelProductXLS target = new Import3LevelProductXLS();
//打开通用导入界面
frmImportExcel.ExecuteImporter(target, target.GetMapping());
if (!String.IsNullOrWhiteSpace(target.ErrorInfo))
{
Msg.Warning(target.ErrorInfo);//导入失败提示
}
else
{
//导入成功则刷新数据
if (target.LastImportCounter > 0)
btnQuery_Click(btnQuery, new EventArgs());
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
导入产品三层结构Excel数据-Import3LevelProductXLS类实现:
C# Code:
/// <summary>
/// 导入产品三层结构Excel数据
/// </summary>
public class Import3LevelProductXLS : IImporterTarget
{
private bllProduct _BLL;
private string _BatchNo;
public Import3LevelProductXLS()
{
_ReportTitle = "导入产成品";
_BLL = new bllProduct();
_DataTarget = _BLL.QueryTmpImport("-");
}
public override List<FieldMapping> GetMapping()
{
List<FieldMapping> list = new List<FieldMapping>();
list.Add(new FieldMapping("分类", sys_tmp_ImportProduct.分类));
list.Add(new FieldMapping("部套编码", sys_tmp_ImportProduct.部套编码));
list.Add(new FieldMapping("部套序号", sys_tmp_ImportProduct.部套序号));
list.Add(new FieldMapping("转序件序号", sys_tmp_ImportProduct.转序件序号));
list.Add(new FieldMapping("描述", sys_tmp_ImportProduct.描述));
list.Add(new FieldMapping("数量", sys_tmp_ImportProduct.数量));
list.Add(new FieldMapping("工时代码", sys_tmp_ImportProduct.工时代码));
list.Add(new FieldMapping("定额工时", sys_tmp_ImportProduct.定额工时));
list.Add(new FieldMapping("生产周期", sys_tmp_ImportProduct.生产周期));
list.Add(new FieldMapping("偏置期", sys_tmp_ImportProduct.偏置期));
list.Add(new FieldMapping("物类", sys_tmp_ImportProduct.物类));
list.Add(new FieldMapping("联动", sys_tmp_ImportProduct.联动));
list.Add(new FieldMapping("产品编码", sys_tmp_ImportProduct.产品编码));
list.Add(new FieldMapping("备注", sys_tmp_ImportProduct.备注));
list.Add(new FieldMapping("班组工时", sys_tmp_ImportProduct.班组工时));
list.Add(new FieldMapping("员工代码", sys_tmp_ImportProduct.员工代码));
list.Add(new FieldMapping("产品型号", sys_tmp_ImportProduct.产品型号));
return list;
}
public override void ClearData()
{
//导入资料前是否先清除目标表
//_BLL.DeleteAll();
}
protected override void BeginImport(DataTable dataSource)
{
_DataSource = dataSource;
_BatchNo = Globals.NewRowIDTime();
AddReport(false, _ReportTitle, "开始导入数据到临时表。", _BatchNo);
}
protected override void EndImport(int success, int failed)
{
AddReport(false, _ReportTitle, "导入到临时表结束。", _BatchNo);
if (success > 0)
{
AddReport(false, _ReportTitle, "开始导入正式表...", _BatchNo);
int a; int b; int c; string err;
bool ok = _BLL.ImportByTmpTable(_BatchNo, out a, out b, out c, out err);
this.LastImportCounter = a + b + c;
this.LastImportFailed = 0;
this.ErrorInfo = err;
if (!String.IsNullOrEmpty(err))
AddReport(true, _ReportTitle, err, _BatchNo);
AddReport(false, _ReportTitle, "导入到正式表结束。", _BatchNo);
AddReport(false, _ReportTitle, string.Format("导入成功记录数:{0}", this.LastImportCounter), _BatchNo);
}
}
/// <summary>
/// 导入一条记录
/// </summary>
/// <param name="drTarge"></param>
/// <returns></returns>
protected override bool PostImportedData(DataRow drTarge, out string errMsg)
{
errMsg = "";
try
{
//创建用于保存的临时表
DataTable dtSave = _DataTarget.Clone();
DataRow tmp = dtSave.Rows.Add(drTarge.ItemArray);
tmp[sys_tmp_ImportProduct.BatchNo] = _BatchNo;
tmp[sys_tmp_ImportProduct.联动] = ConvertEx.ToInt(tmp[sys_tmp_ImportProduct.联动]);
//提交一条记录
//建议使用存储过程,或一次提交多条记录更新(优化速度)
return bllBaseDataDict.Update(dtSave, typeof(sys_tmp_ImportProduct));
}
catch (Exception ex)
{
errMsg = ex.Message;
return false;
}
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// <summary>
/// 导入产品三层结构Excel数据
/// </summary>
public class Import3LevelProductXLS : IImporterTarget
{
private bllProduct _BLL;
private string _BatchNo;
public Import3LevelProductXLS()
{
_ReportTitle = "导入产成品";
_BLL = new bllProduct();
_DataTarget = _BLL.QueryTmpImport("-");
}
public override List<FieldMapping> GetMapping()
{
List<FieldMapping> list = new List<FieldMapping>();
list.Add(new FieldMapping("分类", sys_tmp_ImportProduct.分类));
list.Add(new FieldMapping("部套编码", sys_tmp_ImportProduct.部套编码));
list.Add(new FieldMapping("部套序号", sys_tmp_ImportProduct.部套序号));
list.Add(new FieldMapping("转序件序号", sys_tmp_ImportProduct.转序件序号));
list.Add(new FieldMapping("描述", sys_tmp_ImportProduct.描述));
list.Add(new FieldMapping("数量", sys_tmp_ImportProduct.数量));
list.Add(new FieldMapping("工时代码", sys_tmp_ImportProduct.工时代码));
list.Add(new FieldMapping("定额工时", sys_tmp_ImportProduct.定额工时));
list.Add(new FieldMapping("生产周期", sys_tmp_ImportProduct.生产周期));
list.Add(new FieldMapping("偏置期", sys_tmp_ImportProduct.偏置期));
list.Add(new FieldMapping("物类", sys_tmp_ImportProduct.物类));
list.Add(new FieldMapping("联动", sys_tmp_ImportProduct.联动));
list.Add(new FieldMapping("产品编码", sys_tmp_ImportProduct.产品编码));
list.Add(new FieldMapping("备注", sys_tmp_ImportProduct.备注));
list.Add(new FieldMapping("班组工时", sys_tmp_ImportProduct.班组工时));
list.Add(new FieldMapping("员工代码", sys_tmp_ImportProduct.员工代码));
list.Add(new FieldMapping("产品型号", sys_tmp_ImportProduct.产品型号));
return list;
}
public override void ClearData()
{
//导入资料前是否先清除目标表
//_BLL.DeleteAll();
}
protected override void BeginImport(DataTable dataSource)
{
_DataSource = dataSource;
_BatchNo = Globals.NewRowIDTime();
AddReport(false, _ReportTitle, "开始导入数据到临时表。", _BatchNo);
}
protected override void EndImport(int success, int failed)
{
AddReport(false, _ReportTitle, "导入到临时表结束。", _BatchNo);
if (success > 0)
{
AddReport(false, _ReportTitle, "开始导入正式表...", _BatchNo);
int a; int b; int c; string err;
bool ok = _BLL.ImportByTmpTable(_BatchNo, out a, out b, out c, out err);
this.LastImportCounter = a + b + c;
this.LastImportFailed = 0;
this.ErrorInfo = err;
if (!String.IsNullOrEmpty(err))
AddReport(true, _ReportTitle, err, _BatchNo);
AddReport(false, _ReportTitle, "导入到正式表结束。", _BatchNo);
AddReport(false, _ReportTitle, string.Format("导入成功记录数:{0}", this.LastImportCounter), _BatchNo);
}
}
/// <summary>
/// 导入一条记录
/// </summary>
/// <param name="drTarge"></param>
/// <returns></returns>
protected override bool PostImportedData(DataRow drTarge, out string errMsg)
{
errMsg = "";
try
{
//创建用于保存的临时表
DataTable dtSave = _DataTarget.Clone();
DataRow tmp = dtSave.Rows.Add(drTarge.ItemArray);
tmp[sys_tmp_ImportProduct.BatchNo] = _BatchNo;
tmp[sys_tmp_ImportProduct.联动] = ConvertEx.ToInt(tmp[sys_tmp_ImportProduct.联动]);
//提交一条记录
//建议使用存储过程,或一次提交多条记录更新(优化速度)
return bllBaseDataDict.Update(dtSave, typeof(sys_tmp_ImportProduct));
}
catch (Exception ex)
{
errMsg = ex.Message;
return false;
}
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网