2
I would like to know how to read an excel file and then map to a datagridview.
Code I tried on :
var fileName = @"C:\Users\HP8200\Desktop\test.xlsx";
var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\""; ;
using (var conn = new OleDbConnection(connectionString)) {
conn.Open();
var sheets = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "PARAC1" });
using (var cmd = conn.CreateCommand()) {
cmd.CommandText = "SELECT * FROM [" + sheets.Rows[0]["PARAC1"].ToString() + "] ";
var adapter = new OleDbDataAdapter(cmd);
var ds = new DataSet();
adapter.Fill(ds);
And have you tried filling gridview with this dataset? : Grivew1.datasource= ds.Table[0];
– Renato Afonso
@Renatoafonso this does not work
– Pedro Azevedo
"This does not work". Very explicit. Error? Shows where you put the line of code I suggested.
– Renato Afonso