C#.Net ile Excel Okuma

En basit şekilde C#.net ile   Excel Okuma

public DataTable ReadExcel(string ExcelFilePath)
{
	try
	{
		DataTable dt = new DataTable();
		OleDbConnection con;
		string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=YES;\"", ExcelFilePath);
		con = new OleDbConnection(@connectionString);
		con.Open();
		OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter("select * from [VERI$]", con);
		System.Data.DataSet excelDataSet = new DataSet();
		cmd.Fill(excelDataSet);
		con.Close();

		dt = excelDataSet.Tables[0];
		return dt;
	}
	catch (Exception ex)
	{
		throw ex;
	}
}

private void button1_Click(object sender, EventArgs e)
{
	openFileDialog1.ShowDialog();
	string ExcelFilePath = openFileDialog1.FileName;
	DataTable dt = ReadExcel(ExcelFilePath);
	dataGridView1.DataSource = dt;

	foreach (DataRow item in dt.Rows)
	{
		string text = item[0].ToString()
	}
}

 

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir