public DataTable GetDataFromExcel(string sheetName)
{
DataTable excelTable = new DataTable();
// Khai bao ket noi den tap ti excel
string connecionString = @"Provider=" + providerName + ";Data Soutce=" + excelPathFile + @"; Extended Properties = " + Convert.ToChar(34).ToString() + excelVersion + ";HDR= Yes;IMEX =1" + Convert.ToChar(34).ToString();
string connectionString = @"Provider=" + providerName +
";Data Source=" + excelPathFile
+ @";Extended Properties=" +
Convert.ToChar(34).ToString()
+ excelVersion +
";HDR=Yes;IMEX=1" +
Convert.ToChar(34).ToString();
try
{
using (OleDbConnection oleDbConnection = new OleDbConnection(connectionString))
{
OleDbCommand oleDbCommand = new OleDbCommand("select * from [" + sheetName + "$]", oleDbConnection);
OleDbDataAdapter olDbAdapter = new OleDbDataAdapter(oleDbCommand);
olDbAdapter.Fill(excelTable);
}
}
catch (Exception ex)
{
errorMessage = ex.Message;
excelTable = null;
}
return excelTable;
}