简体   繁体   中英

In C# how to access excel headers using OLEDB (without Automation)?

This is my code where I am trying to access first row, first column

     string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;
                                      Data Source=" + fileName + @";Extended Properties=""Excel 8.0;HDR=NO;""";
            string CreateCommand = "SELECT * FROM [Sheet1$]";
            OleDbConnection conn = new OleDbConnection(connectionString);

              conn.Open();
              OleDbCommand cmd = new OleDbCommand(CreateCommand, conn);
             //   cmd.ExecuteNonQuery();
               DbDataReader dr= cmd.ExecuteReader();

              int i = 0;

               while (dr.Read())
               {

                   string ab = dr.GetValue(i).ToString();
                   MessageBox.Show(ab);
                   i++;
               }

Did you try HDR=YES ? That's what tells the OLEDB provider that you do have a header row.

http://connectionstrings.com/excel

I've always used the built in functions of GetSchema to enumerate Sheets and Headers. It's really quite slick and no non-sense. Good Luck!

OleDbConnection xl = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=filename.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"");
xl.Open();

//Get columns
DataTable dtColumns = xl.GetSchema("Columns", new string[] { null, null, sheetName, null });
List<string> columns = new List<string>();
foreach (DataRow dr in dtColumns.Rows)
   columns.Add(dr[3].ToString());

xl.Close();

Wouldn't you want to set the HDR=No?

Telling the OLEDB provider that the first row contains headers will cause the provider to use the headers as the names for the fields. (I'm thinking about dumping the info into a datatable, after which you get the information @ DataTable.Columns["[HEADER]"].Row....)

Since you're using a simple data reader, and you want the "header" fields to be read as data, specify that these are not headers.

string xlPath = @"D:\Temparary.xlsx";    //location of xlsx file

string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + xlPath + ";Extended Properties=\"Excel 12.0 Xml; HDR=YES; IMEX=1;\"";

OleDbConnection con = new OleDbConnection(constr);

OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]",con);

con.Open();

OleDbDataReader dreader = cmd.ExecuteReader();

if (dreader.HasRows)
{
    dreader.Read();
    Label2.Text = dreader.GetValue(0).ToString();

}

dreader.Close();

con.Close();
// CODE TO SET UP THE CONNECTION BETWEEN EXCEL AND VS2005 
// IN EXTENDED PROPERTIES SET HDR = YES FOR READING FIRST ROW AND HEADERS.
// IN EXTENDED PROPERTIES SET IMEX = 1 TO READ INTERMIXED DATA.

excelCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ExcelDBtrial.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'");
excelCon.Open();
exDA = new OleDbDataAdapter("Select * from [Sheet1$]", excelCon);
exDA.Fill(exDT);

//CODE TO ADD TABLE HEADERS INTO THE HEADERS COMBOBOX
foreach (DataColumn dc in exDT.Columns)
    headerCB.Items.Add(dc.ToString());

I had a different problem, but was able to access the first row of Excel data and remove it via this OleDBAdapter Excel QA I posted via stack overflow.

If you are trying to access first row, first column, just populate a dataset per the post I mentioned and add to the bottom:

    // DataSet:          
    Object o = ds.Tables["xlsImport"].Rows[0]["LocationID"];
    Object oa = ds.Tables["xlsImport"].Rows[0]["PartID"];            
    Object row0Col3 = ds.Tables["xlsImport"].Rows[0][3];

    string valLocationID = o.ToString();
    string valPartID = oa.ToString();
    string rowZeroColumn3 = row0Col3.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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