Пример #1
0
        public void Import()
        {
            //根据视图名读取ExcelConfig信息。

            MDataRow excelInfo = null;
            int      index = 0, headCrossRowNum = 0;
            string   sheetName = null;

            try
            {
                excelInfo = ExcelConfig.GetExcelRow(ObjName);
                if (excelInfo != null)
                {
                    index           = excelInfo.Get <int>(Config_Excel.StartIndex, 0);
                    headCrossRowNum = excelInfo.Get <int>(Config_Excel.HeadCrossRowNum, 0);
                    sheetName       = excelInfo.Get <string>(Config_Excel.Description);
                }
            }
            catch (Exception err)
            {
                Log.WriteLogToTxt(err);//避免其它地方没有升级数据库表脚本。
            }
            MDataTable dt = ExcelHelper.ReadExcel(null, sheetName, index, headCrossRowNum, excelInfo != null);

            if (!dt.Columns.Contains("错误信息"))
            {
                dt.Columns.Add("错误信息", System.Data.SqlDbType.NVarChar);
            }
            dt.TableName = excelInfo != null ? ObjName : TableName;
            bool         result  = false;
            string       msg     = string.Empty;
            ImportResult iResult = BeforeImport(dt, excelInfo, out msg);

            if (iResult == ImportResult.Continue)
            {
                result = FormatExcel(dt, excelInfo);
                if (result)
                {
                    result = ExcelConfig.AcceptChanges(dt, excelInfo, ObjName);// dt.AcceptChanges(AcceptOp.Auto);
                }
            }
            else
            {
                result = iResult == ImportResult.True;
            }
            if (!result)
            {
                if (dt.DynamicData != null && dt.DynamicData is Exception)
                {
                    msg  = ((Exception)dt.DynamicData).Message;
                    msg += "(PS:可能模板不匹配)";
                }
                if (excelStream == null)
                {
                    excelStream = ExcelHelper.SetError(dt);
                }
                if (string.IsNullOrEmpty(msg))
                {
                    msg = "导入失败" + (excelStream != null ? "(请查看输出的Excel错误信息)" : "(请检测是否模板错误)");
                }
            }
            else if (string.IsNullOrEmpty(msg))
            {
                msg = "导入成功";
            }
            dt.DynamicData = null;
            dt             = null;
            GC.Collect();

            jsonResult = JsonHelper.OutResult(result, msg);
        }
Пример #2
0
        public virtual void Import()
        {
            //根据视图名读取ExcelConfig信息。

            MDataRow excelInfo = null;
            int      index = 0, headCrossRowNum = 0;
            string   sheetName = null;

            try
            {
                excelInfo = ExcelConfig.GetExcelRow(ObjName);
                if (excelInfo != null)
                {
                    index           = excelInfo.Get <int>(Config_Excel.StartIndex, 0);
                    headCrossRowNum = excelInfo.Get <int>(Config_Excel.HeadCrossRowNum, 0);
                    sheetName       = excelInfo.Get <string>(Config_Excel.Description);
                }
            }
            catch (Exception err)
            {
                Log.WriteLogToTxt(err);//避免其它地方没有升级数据库表脚本。
            }
            MDataTable dt = ExcelHelper.ReadExcel(null, sheetName, index, headCrossRowNum, excelInfo != null);

            if (!dt.Columns.Contains(LangConst.ErrorInfo))
            {
                dt.Columns.Add(LangConst.ErrorInfo, System.Data.SqlDbType.NVarChar);
            }
            dt.TableName = excelInfo != null ? ObjName : TableName;
            bool         result  = false;
            string       msg     = string.Empty;
            ImportResult iResult = BeforeImport(dt, excelInfo, out msg);

            if (iResult == ImportResult.Continue)
            {
                result = FormatExcel(dt, excelInfo);
                if (result)
                {
                    dt.Columns.Remove(LangConst.ErrorInfo);
                    result = ExcelConfig.AcceptChanges(dt, excelInfo, ObjName);// dt.AcceptChanges(AcceptOp.Auto);
                }
            }
            else
            {
                result = iResult == ImportResult.True;
            }
            if (!result)
            {
                if (dt.DynamicData != null && dt.DynamicData is Exception)
                {
                    msg  = ((Exception)dt.DynamicData).Message;
                    msg += LangConst.ImportTemplateNotMatch;
                }
                if (excelStream == null)
                {
                    excelStream = ExcelHelper.SetError(dt);
                }
                if (string.IsNullOrEmpty(msg))
                {
                    msg = LangConst.ImportError + (excelStream != null ? LangConst.ImportCheckErrorInfo : LangConst.ImportCheckTemplateIsRight);
                }
            }
            else if (string.IsNullOrEmpty(msg))
            {
                msg = LangConst.ImportSuccess;
            }
            dt.DynamicData = null;
            dt             = null;
            GC.Collect();

            jsonResult = JsonHelper.OutResult(result, msg);
        }