Gani tpt
asked on
C# with Excel - How to join to excel two sheets data into one datatable
Hi,
I', using excel sheet. In that i have more sheets.
i just want combine or join from two data sheets into one single datatable
below screenshot for example and sample reference.
I want output as datatable (after merging two sheet columns
ScreenShot.png
I', using excel sheet. In that i have more sheets.
i just want combine or join from two data sheets into one single datatable
below screenshot for example and sample reference.
I want output as datatable (after merging two sheet columns
ScreenShot.png
I agree, this sort of data is going to be better suited for a database-- you can set up a SQLExpress instance for free and put the information there at the level that generates these spreadsheets.
You should be able to pull data from Excel with C# though, the code looks something like this:
You should be able to pull data from Excel with C# though, the code looks something like this:
string connectionString = "Driver={{Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)}};DBQ={0};";
string sheetName = "Sheet1";
string query = "SELECT * FROM [" + sheetName + "]";
DataSet results = new DataSet();
List<string> files = new List<string>();
//Add multiple files to our collection.
files.Add(string.Format(connectionString, "C:\\Temp\\book1.xlsx"));
files.Add(string.Format(connectionString, "C:\\Temp\\book2.xlsx"));
foreach (string file in files) //Add the sheets to the data set.
{
using (OdbcConnection c = new OdbcConnection(file))
{
OdbcDataAdapter da = new OdbcDataAdapter(query, c);
c.Open();
da.Fill(results);
}
}
for (int i = 1; i < results.Tables.Count - 1; i++) //Merge all the collected tables
results.Tables[i].Merge(results.Tables[0]);
return results.Tables[0];
ASKER
Thanks for your relpy.
I forgot to mention is below.
1. Excel file is only one. In that we will have two sheet.
2. from that excel we have to consider two sheets (sheet1 and sheet2 in single file)..not in two different excel file
In that case how to proceed your code's...?.
I forgot to mention is below.
1. Excel file is only one. In that we will have two sheet.
2. from that excel we have to consider two sheets (sheet1 and sheet2 in single file)..not in two different excel file
In that case how to proceed your code's...?.
ASKER
any update...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank.
but, i am getting error:
ERROR [42S02] [Microsoft][ODBC Excel Driver] The Microsoft Access database engine could not find the object 'Output'. Make sure the object exists and that you spell its name and the path name correctly. If 'sheet1' is not a local object, check your network connection or contact the server administrator.
but, i am getting error:
ERROR [42S02] [Microsoft][ODBC Excel Driver] The Microsoft Access database engine could not find the object 'Output'. Make sure the object exists and that you spell its name and the path name correctly. If 'sheet1' is not a local object, check your network connection or contact the server administrator.
ASKER
pls. refer and find attached excel sheet for your reference..
Attached Excel contains SectionA(Sheet),SectionB(s heet2) and Output as Datatable(Sheet3)
So, i need output as datatable as mentioned in Output(sheet3)
Employee.xlsx
Attached Excel contains SectionA(Sheet),SectionB(s
So, i need output as datatable as mentioned in Output(sheet3)
Employee.xlsx
ASKER
Any help at your spare time...
ASKER
Working..Thanks..
We can easily join then using SQL queries...
If not then you can read those excels into data tables using c# and the join them using LINQ.
You can get more details from -
http://www.c-sharpcorner.com/UploadFile/0c1bb2/join-two-datatable-using-linq-in-Asp-Net-C-Sharp/