>string sExcelConnString = > "provider=Microsoft.Jet.OLEDB.4.0; data source='" + sFileName + " '; " + "Extended Properties=Excel 8.0;"; >>then it works fine.
>string sExcelConnString = > "provider=Microsoft.Jet.OLEDB.4.0; data source='" + sFileName + " '; " + "Extended Properties=Excel 8.0;Hdr=Yes"; >>
using System; using System.Data; using System.Data.OleDb; using System.Windows.Forms; using System.Drawing; class excelRead { static void Main() { //string excelConAce = // "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=Yes\""; string excelConJet = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes\""; OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = "c:\\" ; ofd.Filter = "Excel files (*.xls, *.xlsx)|*.xls*" ; ofd.FilterIndex = 2 ; ofd.RestoreDirectory = true ; if(ofd.ShowDialog() == DialogResult.OK) { string datasource = String.Format(excelConJet,ofd.FileName); ShowExcelSheetData(datasource); } } public static void ShowExcelSheetData(string conStr) { OleDbConnection con = new OleDbConnection(conStr); con.Open(); DataTable t = con.GetSchema("Tables",new string[] {null,null,null,"TABLE"}); foreach(DataRow row in t.Rows) { DataTable sheet = new DataTable(); string sheetName = (string)row["TABLE_NAME"]; OleDbCommand cmd = new OleDbCommand( String.Format("select * from [{0}]", sheetName), con); OleDbDataReader rdr = cmd.ExecuteReader(); sheet.Load(rdr); Form f = new ShowDataForm(sheet,sheetName); f.ShowDialog(); } con.Close(); } } public class ShowDataForm : Form { public ShowDataForm(DataTable tbl, string caption) { this.dgv = new System.Windows.Forms.DataGridView(); this.dgv.Location = new System.Drawing.Point(0, 0); this.dgv.Dock = DockStyle.Fill; this.dgv.DataSource = tbl; this.Text = caption; this.Controls.Add(this.dgv); this.ClientSize = new System.Drawing.Size(1024, 768); } private System.Windows.Forms.DataGridView dgv; }Cetin