Introduction
I am a novice in C# programming. This blog will help beginners to solve issues with .xlsx file importing and exporting.
Issue
I did exactly the same as Mr.Harminder said in the linked blog to import an excel file (.xlsx) into my application.
The purpose of my program is also to import the excel data into grid view.
The below code was working in my colleague's system but not working with my system.
private void button3_Click(object sender, EventArgs e) {
string filePath = string.Empty;
string fileExt = string.Empty;
OpenFileDialog file = new OpenFileDialog(); //open dialog to choose file
if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) //if there is a file choosen by the user
{
filePath = file.FileName; //get the path of the file
fileExt = Path.GetExtension(filePath); //get the file extension
if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0) {
try {
DataTable dtExcel = new DataTable();
dtExcel = ReadExcel(filePath, fileExt); //read excel file
dataGridView1.Visible = true;
dataGridView1.DataSource = dtExcel;
} catch (Exception ex) {
MessageBox.Show(ex.Message.ToString());
}
} else {
MessageBox.Show("Please choose .xls or .xlsx file only.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); //custom messageBox to show error
}
}
}
Solution
After much analysis we understood that Microsoft Access Database Engine has been installed in my colleague's system, but not in my system.
After installing the same, the excel file is updated successfully into grid view.
Install Microsoft Access Database Engine, a set of components that facilitate the transfer of data between existing Microsoft Office files such as Microsoft Office Access files and Microsoft Office Excel (*.xls, *.xlsx, and *.xlsb) files to other data sources.
Please make sure that you have installed Microsoft Access Database Engine before you start working with Excel files.