I want to Compiling data from multiple excel to Datagridview
I'm use CheckListBox for Provider excel Path But not work . dataGridView display data single excel path
Help me please , Thank you.
C#
foreach (var item in checkedListBox1.CheckedItems)
{
string constr = "Provider = Microsoft.ACE.OLEDB.12.0; data source = '" + item + "'" +
@";extended properties=" + "\"excel 8.0;hdr=yes;\"";
string sheetname = "[MySheet$]";
conOle = new OleDbConnection(constr);
comOle = new OleDbCommand("Select * From " + sheetname + "", conOle);
conOle.Open();
datpOle = new OleDbDataAdapter(comOle);
}
dtable = new DataTable();
datpOle.Fill(dtable);
dataGridView2.DataSource = dtable;
Gowtham CpRank #1,552 · 1K reputationPosted Sep 17, 2024, 5:23 AM EDT
Hello Paradorn,
Glad the solution worked! The issue with cell types is often due to mixed data types in Excel. You can handle this by using the
IMEX=1property in the connection string, which forces all data to be treated as text.Here’s an updated solution:
DataTable dtable = new DataTable(); // Initialize outside the loop foreach (var item in checkedListBox1.CheckedItems) { string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + item.ToString() + "';" + "Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;\""; // Added IMEX=1 to treat all data as text string sheetname = "[MySheet$]"; using (OleDbConnection conOle = new OleDbConnection(constr)) { using (OleDbCommand comOle = new OleDbCommand("SELECT * FROM " + sheetname, conOle)) { conOle.Open(); using (OleDbDataAdapter datpOle = new OleDbDataAdapter(comOle)) { DataTable tempTable = new DataTable(); // Temporary table for each Excel file datpOle.Fill(tempTable); dtable.Merge(tempTable); // Merge the temp table into the main DataTable } } } } dataGridView2.DataSource = dtable; // Bind the merged data to DataGridViewIf you need more fine-grained control over column types, you may need to process the
DataTablefurther after loading the data to adjust the types accordingly.Hope this helps!
Best regards,
Gowtham Cp
Gowtham CpRank #1,552 · 1K reputationPosted Sep 17, 2024, 12:28 AM EDT
Hi Paradorn ,
You need to merge the data from all the Excel files instead of overwriting it each time. Right now, you're re-initializing the
DataTable(dtable) in every loop, which is why only the data from the last file is displayed. The solution is to initialize theDataTableoutside the loop and use theMerge()method to add rows from each file into the sameDataTable. Here’s the code:DataTable dtable = new DataTable(); // Initialize outside the loop foreach (var item in checkedListBox1.CheckedItems) { string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + item.ToString() + "';" + "Extended Properties=\"Excel 8.0;HDR=YES;\""; string sheetname = "[MySheet$]"; using (OleDbConnection conOle = new OleDbConnection(constr)) { using (OleDbCommand comOle = new OleDbCommand("SELECT * FROM " + sheetname, conOle)) { conOle.Open(); using (OleDbDataAdapter datpOle = new OleDbDataAdapter(comOle)) { DataTable tempTable = new DataTable(); // Temporary table for each Excel file datpOle.Fill(tempTable); dtable.Merge(tempTable); // Merge the temp table into the main DataTable } } } } dataGridView2.DataSource = dtable; // Bind the merged data to DataGridViewThis way, all selected Excel files will be merged into one
DataTable, and theDataGridViewwill display the combined data.Hope it helps!
Paradorn59 reputationPosted Sep 17, 2024, 3:31 AM EDT
Hi Gowtham Cp
Thank you for reply
it's work and so great ,
Then I have a problem with the cell type of each file
I am looking for a solution
Paradorn59 reputationPosted Sep 17, 2024, 10:57 AM EDT
Hello Gowtham Cp
Thanks for all the solutions.