I need to read the excel file. All the examples that I found either do not work, or are too difficult to understand, as for a beginner.
How to open a file to drive data into a DataGridView
?
I need to read the excel file. All the examples that I found either do not work, or are too difficult to understand, as for a beginner.
How to open a file to drive data into a DataGridView
?
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
Here is the same topic , another example: Import Excel File to DataSet .
private void button1_Click(object sender, EventArgs e) { OleDbConnection MyConnection; DataSet DtSet; OleDbDataAdapter MyCommand; MyConnection = new OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\fileExcel.xls';Extended Properties=Excel 8.0;"); MyCommand = new OleDbDataAdapter("select * from [Sheet1$]", MyConnection); MyCommand.TableMappings.Add("Table", "Net"); DtSet = new DataSet(); MyCommand.Fill(DtSet); dataGridView1.DataSource = DtSet.Tables[0]; MyConnection.Close(); }
Work through the data source schema. I throw this site from the working code of the project:
DataTable COL = null; DataTable dtSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { }); DataTable columns = null; foreach (DataRow row in dtSchema.Rows) { ... COL = con.GetSchema("Columns", restrictions); //колонки ... }
and it’s gone, you can describe some select, then use it, for example: (I omitted the initialization of these)
adapter = new OleDbDataAdapter(select, strConnection); dtExcel = new DataTable("Excel_Table");
good luck
There is a nuance - which version of Excel is parsed (by default, version 8 is running, it will parse without problems. Perhaps there will be errors with earlier versions)
Source: https://ru.stackoverflow.com/questions/162166/
All Articles