Pages

Friday, October 18, 2013

Read Excel Cell value using OLEDB connection.

string documentPath = ConfigurationManager.AppSettings["SiaTempPath"] + "SIATemp.xlsb";

  string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + documentPath +

";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\"";

OleDbConnection connection = new OleDbConnection(connectionString);
 

// open connection to excel file
connection.Open();

OleDbCommand cmd = new OleDbCommand();

OleDbDataAdapter adapter = new OleDbDataAdapter();

DataSet ds = new DataSet();



// webconfig entry: <add key="SiaCellCoordinates" value="D19" /> string siaCoordinate = ConfigurationManager.AppSettings["SiaCellCoordinates"].ToString();

cmd.CommandText = "SELECT * FROM [SIA$" + siaCoordinate + ":" + siaCoordinate + "]";



cmd.Connection = connection;
// code to assign command for OleDbDataAdapter



adapter.SelectCommand = cmd;
ds.Tables.Add("xlsImport", "Excel");

adapter.Fill(ds, "xlsImport");

//Required Value
ds.Tables["xlsImport"].Rows[0][0].ToString()


______________________________________________________________
Enjoy Coding ;-)

1 comment: