C#:
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
namespace ExcelVeriCekme
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "Excel Dosyaları|*.xlsx;*.xls",
Title = "Excel Dosyasını Seçiniz",
RestoreDirectory = true,
};
if (ofd.ShowDialog() == DialogResult.OK)
{
string dosyaYolu = ofd.FileName;
string dosyaAdi = ofd.SafeFileName;
OleDbConnection baglanti = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dosyaYolu + "; Extended Properties='Excel 12.0 xml;HDR=YES;'");
baglanti.Open();
// Buradaki sayfa1$ kısmı önemlidir.sayfa adı faklı ise program hata verecektir.
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sayfa1$]", baglanti);
DataTable dtExcel = new DataTable();
da.Fill(dtExcel);
dataGridView1.DataSource = dtExcel;
baglanti.Close();
}
}
}
}