导入文件--解决找不表[0]
金蝶云社区-assassinl10
assassinl10
1人赞赏了该文章 385次浏览 未经作者许可,禁止转载编辑于2017年12月28日 18:28:37

[Description("动态表单插件:Excel导入")]
public class ExcelInDynamic : AbstractDynamicFormPlugIn
{
///


/// 上传上来的文件名:完整的文件名,包含了物理路径
///

private string _fullFileName = string.Empty;
public override void AfterBindData(EventArgs e)
{
this.View.GetControl("FImport").Enabled = false;
}
///
/// 文件上传完毕,触发此事件:通过此事件获取上传上来的文件名
///

///
public override void CustomEvents(CustomEventsArgs e)
{
if (e.Key.EqualsIgnoreCase("FFileUpdate"))
{
this.View.GetControl("FFileUpdate").SetCustomPropertyValue("NeedCallback", true);
this.View.GetControl("FFileUpdate").SetCustomPropertyValue("IsRequesting", false);
if (e.EventName.EqualsIgnoreCase("FileChanged"))
{// 文件上传完毕
// 取文件上传参数,文件名
JSONObject uploadInfo = KDObjectConverter.DeserializeObject(e.EventArgs);
if (uploadInfo != null)
{
JSONArray json = new JSONArray(uploadInfo["NewValue"].ToString());
if (json.Count > 0)
{
// 取上传的文件名
string fileName = (json[0] as Dictionary)["ServerFileName"].ToString();
this._fullFileName = this.GetFullFileName(fileName);
// 解锁确定按钮
this.View.GetControl("FImport").Enabled = true;
}
else
{
// 锁定确定按钮
this.View.GetControl("FImport").Enabled = false;
}
}
}
}
}
///
/// 按钮点击事件
///

///
public override void ButtonClick(ButtonClickEventArgs e)
{
if (e.Key.EqualsIgnoreCase("FImport"))
{// 确定
this.View.ReturnToParentWindow(new FormResult(this._fullFileName));
this.View.Close();
}
else if (e.Key.EqualsIgnoreCase("FCancel"))
{// 取消
if (!string.IsNullOrEmpty(this._fullFileName))
{
DelFile(this._fullFileName);
}
this.View.Close();
}
}
///
/// 产生完整的文件名,包含了物理路径
///

///
///
private string GetFullFileName(string fileName)
{
string dir = "FileUpLoadServices\\UploadFiles";
return PathUtils.GetPhysicalPath(dir, fileName);
}

#region 删除单个文件

private void DelFile(string path)
{
FileInfo file = new FileInfo(path);
if (file.Exists)
{
file.Delete(); //删除单个文件
}
}

#endregion
}

----------------------------------------------------------------------------------------
// 显示文件上传界面,上传Excel文件
DynamicFormShowParameter showParam = new DynamicFormShowParameter();
showParam.FormId = "PMP_ExcelIn";
showParam.Caption = "文件导入";
this.View.ShowForm(showParam,
new Action((formResult) =>
{
if (formResult != null && formResult.ReturnData != null)
{
string fullFileName = formResult.ReturnData.ToString();
this.DoImportExcel(fullFileName);
}
}));

------------------------------------------------------------------------------------------------

// 利用ExcelOperation对象,把xml文件,转为DataSet对象
// 参数说明:
// filePath : 完整的文件名,包含了物理目录
// dataStartIndex : 数据开始行索引,从0开始。通常第一行为标题,第二行开始为数据行
// colNameIndex : 列名所在行索引,从0开始。如此参数为0,表明第一行为列名行
using (ExcelOperation helper = new ExcelOperation(this.View))
{
//DataSet ds = helper.ReadFromFile(fullFileName, 1, 0); //---会报找不到表[0] 错误
DataSet ds = helper.ReadFromFileCustom(fullFileName, 1, 0);
DelFile(fullFileName); //删除文件
// 取第一个表格中的数据导入
DataTable dt = ds.Tables[0];
this.Model.ClearNoDataRow();

//数据从第二行开始,做判断用
int row = 2;
judgeStr.Clear();

#region 导入列校验
if (!dt.Columns.Contains("材料名称"))
{
base.View.ShowErrMessage("请确认是否包含【材料名称】列");
return;
}

#endregion
//数据格式校验
foreach (DataRow dataRow in dt.Rows)
{
if (dataRow.Table.Columns.Contains("材料名称") &&
String.IsNullOrEmpty(dataRow["材料名称"].ToString().Trim()))
{
judgeStr.AppendLine("Excel第" + row + "行‘【材料名称】’不能为空");
}

}
}