public DataSet readCSV(string file) { if (!File.Exists(file)) return null; string pathOnly = Path.GetDirectoryName(file); DataSet ds = new DataSet(); OleDbConnection conn = null; //string fileConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathOnly + "\\;Extended Properties=\"Text;HDR=Yes;FORMAT=Delimited\""; string fileConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ pathOnly +";Extended Properties=Excel 8.0"; OleDbConnection ocn = new OleDbConnection(fileConnStr); try { try { conn = new OleDbConnection(); conn.ConnectionString = fileConnStr; conn.Open(); } catch { MessageBox.Show("Veritabanı bağlantı kuramadı."); return null; } string sql = String.Format("SELECT * FROM {0}", Path.GetFileName(file)); OleDbDataAdapter oda = new OleDbDataAdapter(sql, conn); oda.SelectCommand.ExecuteNonQuery(); oda.Fill(ds, "TableName"); } catch (Exception e) { Console.WriteLine(e.Message); return null; } finally { if (conn != null) conn.Dispose(); } return ds; }}}
private void btnExcelReader_Click(object sender, EventArgs e) { string dosya; //string cevap; openFileDialog1.ShowDialog(); dosya = openFileDialog1.FileName.ToString(); ClsExcelReader er = new ClsExcelReader(); DataSet ds = er.readCSV(dosya); dataGridViewScanner.DataSource = ds; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)