Link to home
Start Free TrialLog in
Avatar of Gani tpt
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
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Do you have any database?
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/
Avatar of Dustin Saunders
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:

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];

Open in new window

Avatar of Gani tpt
Gani tpt

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...?.
any update...
ASKER CERTIFIED SOLUTION
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
pls. refer and find attached excel sheet for your reference..

Attached Excel contains SectionA(Sheet),SectionB(sheet2) and Output as Datatable(Sheet3)

So, i need output as datatable as mentioned in Output(sheet3)
Employee.xlsx
Any help at your spare time...
Working..Thanks..