简体   繁体   English

Excel文件的第一列未读取窗口应用程序中的oledb阅读器,Excel文件数据未读取

[英]First Column of excel file not reading oledb reader in window application, excel file data not reading

I facing problem to read excel sheet using oledb reader, first column not returning by reader and showing at last column head F14 and column has been empty. 我遇到了使用oledb阅读器阅读excel工作表的问题,阅读器未返回第一列,并显示最后的列标题F14和列为空。 But when I open excel sheet and double click on header row border for auto adjust and auto re size save excel and again read then all columns returning perfectly. 但是,当我打开excel工作表并双击标题行边框以进行自动调整和自动调整大小时,保存excel并再次读取,然后所有列均完美返回。

Excel sheet which I try to read that generates using php application and after download that excel we put on my application to read data from excel but above issue come. 我尝试读取使用php应用程序生成的Excel工作表,并下载了excel后,我们放到我的应用程序中以从excel读取数据,但以上问题来了。

I already did lots of R&D even I give width in excel sheet while generating excel using web application. 我已经做了很多研发工作,甚至在使用Web应用程序生成excel的同时在excel表格中给出了宽度。 My code is like this 我的代码是这样的

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';" />

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

I just solved same problem. 我只是解决了同样的问题。 Maybe stand someone in good stead. 也许会让一个人站稳脚跟。

In my case, the excel file contains a filter on a column. 就我而言,excel文件在列上包含一个过滤器。 So when reading the file, getting the hidden sheet that named as _xlnm#_FilterDatabase . 因此,在读取文件时,获取名为_xlnm#_FilterDatabase的隐藏工作表。

Actually I expect one sheet, so I am looking at the first row on the schema table. 实际上,我希望有一张纸,所以我正在查看模式表的第一行。 But I get two sheets which one is the filter sheet and this sheet does not contains all columns. 但是我得到两张纸,其中一张是过滤纸,而这张纸并不包含所有列。 So I do not get the first column. 因此,我没有获得第一列。

To solve this, loop through schema and get the first sheet name that not contains FilterDatabase . 要解决此问题,请遍历架构并获取不包含FilterDatabase的第一个工作表名称。

Code talks: 代码讨论:

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 + "]";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM