Ejemplo n.º 1
0
        private DataTable ReadFile(string filepath, out string errormsg)
        {
            DataTable dt    = DataImportHelper.ImportExcelFile(filepath, Path.GetExtension(filepath), 0);
            bool      error = false;

            errormsg = "";
            //移除空行
            int            coulumnCount   = dt.Columns.Count;
            List <DataRow> removeDataRows = new List <DataRow>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int count = 0;
                for (int j = 0; j < coulumnCount; j++)
                {
                    if (dt.Rows[i].IsNull(j) || dt.Rows[i][j].ToString().Trim() == "")
                    {
                        count = count + 1;
                    }
                }
                if (count == coulumnCount)                //整行为空
                {
                    removeDataRows.Add(dt.Rows[i]);
                }
            }
            foreach (DataRow item in removeDataRows)
            {
                dt.Rows.Remove(item);
            }
            //验证文件列符合导入标准
            string[] strArray = { "学段", "进班年份", "毕业年份", "班级", "学籍号", "地区学号", "姓名", "身份证号" };
            if (dt.Columns.Count == strArray.Length)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (strArray[j] != dt.Rows[0][j].ToString().Trim())
                    {
                        errormsg = "上传文件的列名或列序与模板的列名或列序不匹配";
                        dt       = new DataTable();
                        error    = true;
                        break;
                    }
                }
            }
            else
            {
                dt       = new DataTable();
                errormsg = "上传文件的列数与模板的列数不匹配";
                error    = true;
            }
            if (error == false)
            {
                #region dt detail
                //dt.Columns["登录帐号"].ColumnName = "LoginName";
                //dt.Columns["学校"].ColumnName = "XXID";
                dt.Columns["学段"].ColumnName   = "ClassStage";
                dt.Columns["进班年份"].ColumnName = "ClassCreateYear";
                dt.Columns["毕业年份"].ColumnName = "ClassGraduationYear";
                dt.Columns["班级"].ColumnName   = "ClassName";
                dt.Columns["学籍号"].ColumnName  = "StuCode";
                dt.Columns["地区学号"].ColumnName = "StuNumber";
                dt.Columns["姓名"].ColumnName   = "StuName";
                dt.Columns["身份证号"].ColumnName = "StuIdentity";
                #endregion
            }
            return(dt);
        }