Read the cell data of the specified area in the Excel workfinder with C #
Read the Excel file using the OLEDBDataAdapter object in ADO.NET, the first row of units in the default worksheet are field names. If you do not want to use the first row of data in the work, it can be implemented in this article as a second instance in this article.
Establish a data source connection string of the Connection object: "provider = microsoft.jet.Oledb.4.0; data source = Excel file physical path "; extended profout = excel 8.0 "; these two instances are made by EXCEL 2000 or higher.
Example 1 Read all the data in Excel work
Read all the data in Excel Works 1 (Workbook Name: Sheet1), the SQL statement in the OLEDBDataAdapter object should be: "SELECT field list from [work table name $]".
Code example:
public void ReadExcel (string sExcelFile) {string sConnectionString = "Provider = Microsoft.Jet.Oledb.4.0; Data Source =" sExcelFile "; Extended Properties = Excel 8.0"; OleDbConnection connection = new OleDbConnection (sConnectionString);
OLEDBDATADAPTER ADP = New OLEDBDataAdapter ("SELECT * FROM [Sheet1 $], Connection;
DataSet DS = New Dataset (); adp.fill (DS, "BOOK1");
Grd_excel.datasource = ds.tables ["book1"]. DefaultView; grd_excel.database ();
Example 2 Reads Data in the Excel Works Map
First, in the workbook, the left button dragged and drop to define the area where you become a table, select 'Insert' -> 'Name' -> 'Definition' in the menu, in the 'Defined Name' dialog box appears in the name of the in-list: "TestTable", OK. Then the SQL statement should be: select * from testtable.
Code example:
public void ReadExcel (string sExcelFile) {string sConnectionString = "Provider = Microsoft.Jet.Oledb.4.0; Data Source =" sExcelFile "; Extended Properties = Excel 8.0"; OleDbConnection connection = new OleDbConnection (sConnectionString);
OLEDBDATADAPTER ADP = New OLEDBDataAdapter ("Select * from testtable", connection;
DataSet DS = New Dataset (); adp.fill (DS, "BOOK1");
Grd_excel.datasource = ds.tables ["book1"]. defaultview; grd_excel.database ();} related link: http://community.9cbs.net/expert/topic/3900/3900381.xml? Temp = .1787989