【问题标题】:First Column of excel file not reading oledb reader in window application, excel file data not readingexcel文件的第一列未在窗口应用程序中读取oledb阅读器,excel文件数据未读取
【发布时间】:2015-04-28 02:50:57
【问题描述】:

我在使用 oledb 阅读器读取 excel 表时遇到问题,第一列未由阅读器返回,并在最后一列标题 F14 显示,并且列已为空。 但是当我打开 excel 表并双击标题行边框进行自动调整和自动调整大小保存 excel 并再次读取时,所有列都完美返回。

我尝试阅读使用 php 应用程序生成的 Excel 表,下载该 excel 后,我们将其放在我的应用程序中以从 excel 中读取数据,但出现上述问题。

我已经做了很多研发工作,即使我在使用 web 应用程序生成 excel 时在 excel 表中给出了宽度。我的代码是这样的

private bool Import_To_Grid(string FilePath, string Extension)
        {
            try
            {
                string conStr = "";
                switch (Extension)
                {
                    case ".xls": //Excel 97-03
                        conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"]
                                 .ConnectionString;
                        break;
                    case ".xlsx": //Excel 07 and above
                        conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"]
                                  .ConnectionString;
                        break;
                }
                conStr = String.Format(conStr, FilePath);
                OleDbConnection connExcel = new OleDbConnection(conStr);
                OleDbCommand cmdExcel = new OleDbCommand();
                OleDbDataAdapter oda = new OleDbDataAdapter();

                cmdExcel.Connection = connExcel;

                //Get the name of First Sheet
                connExcel.Open();
                Exceldt = new DataTable();
                DataTable dtExcelSchema;
                dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                connExcel.Close();

                //Read Data from First Sheet
                connExcel.Open();
                cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
                oda.SelectCommand = cmdExcel;

                oda.Fill(Exceldt);
                connExcel.Close();

                //Bind Data to GridView
                dgv_showexcel.DataSource = Exceldt;
                BindDataToCmbClass(); //binddata to class for filter
                cmb_userclass.SelectedIndex = 0;
                return true;
            }
            catch (Exception ex) { MessageBox.Show("Its Error" + " " + ex.ToString()); return false; }
        }


Connection string
<add name="Excel03ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';" />
<add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';" />

【问题讨论】:

  • 很紧急,我没有得到任何关于此的任何信息,如果对此有任何想法,请与我分享,谢谢

标签: c# asp.net excel oledb


【解决方案1】:

当我们下载或写入锁定第一个单元格的模式时,我们需要在第一列和第一行默认设置这个 bcz 标题位置的 IMEX=3。

【讨论】:

    【解决方案2】:

    我刚刚解决了同样的问题。或许能让某人处于有利地位。

    在我的例子中,excel 文件在列上包含一个过滤器。所以在读取文件时,会得到名为_xlnm#_FilterDatabase的隐藏表。

    实际上我希望有一张纸,所以我正在查看架构表的第一行。但是我得到两张表,其中一张是过滤表,这张表不包含所有列。所以我没有得到第一列。

    要解决此问题,请遍历架构并获取第一个不包含 FilterDatabase 的工作表名称。

    代码讨论:

    DataTable excelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    string sheetName = null;
    for (int i = 0; i < dtExcelSchema.Rows.Count; i++)
    {
        string tableNameOnRow = dtExcelSchema.Rows[i]["TABLE_NAME"].ToString();
        if (!tableNameOnRow.Contains("FilterDatabase"))
        {
           sheetName = tableNameOnRow; 
           break;
        }
     }
     OleDbDataAdapter da = new OleDbDataAdapter();
     DataSet ds = new DataSet(); cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多