Just nu i M3-nätverket
Jump to content

Läsa ur Excel med OleDb


johnJ

Recommended Posts

Tja!

 

Försöker läsa in ett excel blad in i en datatable. Men vissar celler får null värde fast de innehåller data. Har provat att göra om alla celler i excel till typ Allmän eller Text utan resultat. Någon som har erfarenhet av detta? Slut på idéer.

 

		string sConnectionString =  
			"Provider=Microsoft.Jet.OLEDB.4.0;" + 
			"Data Source=c:\\test.xls;" + 
			"Extended Properties=\"Excel 8.0;HDR=No;\";"; 
		OleDbConnection objConn = new OleDbConnection(sConnectionString);   
		objConn.Open();   

		DataTable sheets = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
		string SheetName = sheets.Rows[0]["TABLE_NAME"].ToString();

		OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [" +SheetName + "]", objConn);   

		OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();   
		objAdapter1.MissingMappingAction = MissingMappingAction.Passthrough;
		objAdapter1.MissingSchemaAction = MissingSchemaAction.Add;
		objAdapter1.SelectCommand = objCmdSelect;   
		DataTable dt = new DataTable();  
		objAdapter1.FillSchema(dt,SchemaType.Source);
		objAdapter1.Fill(dt); 

		objConn.Close(); 

		dataGrid1.DataSource = dt;	

 

Mvh. John

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...