Click here to Skip to main content
15,895,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one excel document and multiple sheets.sheets have many columns and row.i upload the document into my project.and the multiple sheets is bind to tables(sheet name is set as table name) using for loop.after the bind i change the table name to source1,source2,source3 (three sheets). Now how to bind(merge) the three tables in single table (source1).

What I have tried:

i have tried to rename the all tables to same name but the duplicate table is not allowed.how to bind the same table.
Posted
Updated 11-Aug-16 5:39am
v2

1 solution

Its not possible to rename all the datatable names to same value.
use DataTable.Merge Method [^] to merge all the datatable's data from the dataset.

refer this example:
assuming your dataset contains 3 tables as below
C#
DataSet dataSetExcel = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("ID");
dt1.Columns.Add("Name");
dt1.Rows.Add(1, "one");

DataTable dt2 = new DataTable();
dt2.Columns.Add("ID");
dt2.Columns.Add("Name");
dt2.Rows.Add(2, "two");

DataTable dt3 = new DataTable();
dt3.Columns.Add("ID");
dt3.Columns.Add("Name");
dt3.Rows.Add(3, "three");
dataSetExcel.Tables.AddRange(new DataTable[] {dt1,dt2,dt3 });


try like this to merge all the data to a new DataTable

C#
DataTable dtFinal = new DataTable();
           for (int i = 0; i < dataSetExcel.Tables.Count; i++)
           {
               if (i == 0)
                   dtFinal = dataSetExcel.Tables[i].Copy();
               else
                   dtFinal.Merge(dataSetExcel.Tables[i]);
           }


or, you shall merge the data from sheet 2 and sheet 3 to sheet 1 and delete the sheet 2 and 3

C#
dataSetExcel.Tables[0].Merge(dataSetExcel.Tables[1]);
          dataSetExcel.Tables[0].Merge(dataSetExcel.Tables[2]);
          dataSetExcel.Tables.RemoveAt(1); //  duplicate code to avoid index exception
          dataSetExcel.Tables.RemoveAt(1); //  duplicate code to avoid index exception
          dataSetExcel.AcceptChanges();
 
Share this answer
 
Comments
Raja Ganapathy 16-Aug-16 0:29am    
I have used below code to convert Excelsheet to Dataset and named table as table1,table2,table3. and generate xml xml tags have combined all three tables.i have add column in table and start tag and tag as <table1></table1>... <table n></table n> now the starting and ending tag must be same.then only back end sql is get the value so how to merge the table.i try this code if any other method is available pls suggest me.sorry for the late reply and same question ask again and again.


for (int k = 0; k < ds.Tables.Count; k++)
{
int c = k + 1;
string sheetname = ds.Tables[k].ToString();
if (ds.Tables[k].ToString() == "ExcelSheetName" + c)
{
ds.DataSetName = "Search"; ds.Tables[k].TableName = "Table" + c; ds.Tables["Table" + c].AcceptChanges();
}
}
Karthik_Mahalingam 16-Aug-16 0:33am    
if (ds.Tables[k].TableName == "ExcelSheetName" + c)
Raja Ganapathy 16-Aug-16 0:54am    
this is example. "ExcelSheetName"+c it is excel sheet name uploader give the name to the excel sheet name(common)like Form 1,Form 2 upto Form N.Sheet is declare as common and the +c is add the number Form 1 Form 2 Form 3 ...
Karthik_Mahalingam 16-Aug-16 1:16am    
so you need to convert "ExcelSheetName" to "Form"?
Raja Ganapathy 16-Aug-16 1:20am    
Form is table name in dataset.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900