Completely Free .NET word API

Forums » .NET » ASP.NET »

How to merge two excel sheets in c#

Posted Date: 03 Mar 2010      Posted By:: Alwyn Duraisingh Profile photo      Member Level: Gold  Member Rank: 5     Points: 1   Responses: 3

I have an excel workbook1 with two sheets as sheet1 and sheet2
I have an excel workbook2 with two sheets as sheet1 and sheet2
Now i want to get the sheet1 from Workbook1 and sheet2 from Workbook2
in a new excel Workbook3.

Can anyone send me the code to do this task?

Please mark this as Answer, if this helps

Alwyn Duraisingh.M 
<< Database Administrator >>
Jesus saves! The rest of us better make backups...


#479199    Author: Anil Kumar Pandey      Member Level: Diamond      Member Rank: 1     Date: 03/Mar/2010   Rating: 2 out of 52 out of 5     Points: 2

you need to First Read the value of Sheet 1 from one file and then Sheet 2 from another file.

check this code for writing excel

check this for reading

string strConnstr = "Server=;Database = dbEBSCONew;User id=gpts;password=gpts;";
SqlConnection con = new SqlConnection(strConnstr);

string strQuery;

Microsoft.Office.Interop.Excel.Application sXL = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook = null;
Microsoft.Office.Interop.Excel.Sheets sheets = null;
Microsoft.Office.Interop.Excel.Worksheet workSheet = null;

//Opening the Record file
workBook = sXL.Workbooks.Open("D:\\Customlookupsheet", 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
sheets = workBook.Worksheets;
workSheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item("Occupation");
int rowIndex = 3;

toolStripProgressBar1.Minimum = 0;
toolStripProgressBar1.Maximum = workSheet.UsedRange.Rows.Count;
while (rowIndex <= workSheet.UsedRange.Rows.Count)
Microsoft.Office.Interop.Excel.Range rngParam = workSheet.get_Range("A" + rowIndex.ToString(), "A" + rowIndex.ToString());
string strParam = Convert.ToString(rngParam.Cells.Value2);

Microsoft.Office.Interop.Excel.Range rngParam1 = workSheet.get_Range("B" + rowIndex.ToString(), "B" + rowIndex.ToString());
string strParam1 = Convert.ToString(rngParam1.Cells.Value2);

strQuery = "Insert into tbOccupation values('" + strParam + "','" + strParam1 + "')";

int x = SqlHelper.ExecuteNonQuery(con, CommandType.Text, strQuery);
rowIndex += 1;

Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM

#479206    Author: Raj Sharma      Member Level: Gold      Member Rank: 0     Date: 03/Mar/2010   Rating: 2 out of 52 out of 5     Points: 2


You can go with this tutorial.


#479210    Author: Kolan      Member Level: Gold      Member Rank: 0     Date: 03/Mar/2010   Rating: 2 out of 52 out of 5     Points: 2

Post Reply     

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.

  • Return to Discussion Forum
  • Start new thread

  • Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India