【问题标题】:unable to read a particular cell from excel using reader无法使用阅读器从 excel 中读取特定单元格
【发布时间】:2012-10-26 00:52:00
【问题描述】:

我正在将 excel 导入 sql server db,excel 表有三列:

身份证(仅限数字)|数据|护照

在导入之前,我想检查某些内容,例如:

  • 护照应该以字母开头,其余字符必须是数字
  • id 只能是数字
  • 我可以检查护照,但即使我使用与检查护照相同的代码,我也无法检查身份证。

                 using (DbDataReader dr = command.ExecuteReader())
            {
                // SQL Server Connection String
                string sqlConnectionString = "Data Source=DITSEC3;Initial Catalog=test;Integrated Security=True";
    
                con.Open();
                DataTable dt7 = new DataTable();
                dt7.Load(dr);
                DataRow[] ExcelRows = new DataRow[dt7.Rows.Count];
                DataColumn[] ExcelColumn = new DataColumn[dt7.Columns.Count];
    
                //=================================================
                for (int i1 = 0; i1 < dt7.Rows.Count; i1++)
                {
    
                    if (dt7.Rows[i1]["passport"] == null)
                    {
                        dt7.Rows[i1]["passport"] = 0;
    
                    }
                    if (dt7.Rows[i1]["id"] == null)
                    {
                        dt7.Rows[i1]["id"] = 0;
                    }
    
                    string a = Convert.ToString(dt7.Rows[i1]["passport"]);
                    string b = dt7.Rows[i1]["id"].ToString();
    
                    if (!string.IsNullOrEmpty(b))
                    {
                        int idlen = b.Length;
    
                        for (int j = 0; j < idlen; j++)
                        {
                            if (Char.IsDigit(b[j]))
                            {
                                //action
                            }
                            if(!Char.IsDigit(b[j]))
                            {
                                flag = flag + 1;
                                int errline = i1 + 2;
                                Label12.Text = "Error at line: " + errline.ToString();
                                //Label12.Visible = true;
                            }
                        }
                        if (!String.IsNullOrEmpty(a))
                        {
                            int len = a.Length;
    
                            for (int j = 1; j < len; j++)
                            {
                                if (Char.IsLetter(a[0]) && Char.IsDigit(a[j]) && !Char.IsSymbol(a[j]))
                                {
                                    //action
                                }
                                else
                                {
                                    flag = flag + 1;
                                    int errline = i1 + 2;
                                    Label12.Text = "Error at line: " + errline.ToString();
                                    //Label12.Visible = true;
                                }
    
    
                        }
                    }
    
    
    
    
                     }
    

    由于某些奇怪的原因,当我使用断点时,我可以看到 id 的值,只要 id 是 excel 中的数字,当流到达 id 为 25h547 的单元格时,如果 b 转为“”值,这有什么原因吗?如果你需要,我可以给你完整的代码。

    【问题讨论】:

    • 检查 Visual Studio 的十六进制设置
    • this一个,可能是同样的问题
    • 不,我认为这不是问题
    • 它的 ["id"] 在代码中无处不在,如您在上面看到的,我使用断点并发现 dt7 具有 excel 表的所有值,除了包含数字和字母表的值跨度>
    • 我在想也许“id”会被视为保留字,所以要求尝试使用"[id]" 而不是"id"

    标签: c# asp.net sql-server


    【解决方案1】:

    specify the imex mode in connectionstring to handle mixed values

    见:Mixed values in excel rows

    缺失值。 Excel 驱动程序读取一定数量的行(通过 默认,8 行)在指定源中猜测数据类型 每一列。当一列似乎包含混合数据类型时, 特别是与文本数据混合的数字数据,驱动程序决定 偏爱多数数据类型,并为单元格返回空值 包含其他类型的数据。 (在平局中,数字类型 获胜。)Excel 工作表中的大多数单元格格式选项似乎都没有 影响此数据类型的确定。您可以修改此行为 Excel 驱动程序通过指定导入模式。要指定导入模式, 将 IMEX=1 添加到连接中扩展属性的值 属性窗口中 Excel 连接管理器的字符串

    【讨论】:

      【解决方案2】:

      似乎正在发生的事情是,当数据导入保存数据表并且列中的第一条记录是字母数字时,如果第一个是数字,它将假定列中的所有记录都是字母数字,它将假定所有列中的记录是数字的,因此对于列中某处出现的字母数字记录将是空白的。我自己通过修改连接字符串解决了这个问题:“Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text”

      "IMEX=1;"告诉驱动程序始终将“混合”(数字、日期、字符串等)数据列作为文本读取。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多