© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago
Salakhosh

How can i import excel file to dataTable using C#(WPF application) and oledb.

Hello so i have task where i have to browse an excel file and import its data to a data table but when im using oledb and reach query part it give me that Sheet1 doesnt exist and honestly idk what should i write. This is what i reached so far private void Browse_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel Files|xlsx,xls";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
PathBox.Text = ofd.FileName;
string path = ofd.FileName;
string connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={path};Extended Properties='Excel 12.0 Xml;HDR=YES;'";

using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();

DataTable dataTable = new DataTable();

// Assuming the data is in the first worksheet
string query = "SELECT * FROM [Sheet1$]";
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, connection))
{
adapter.Fill(dataTable);
}

connection.Close();

}
}
Attendance_template.xlsx18.94KB
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ How to import Excel File with row header to GridControl using C#. WPF application
C#CC# / help
3y ago
Import excel file in c#
C#CC# / help
3y ago