You must Sign In to post a response.

How to merge two excel sheets in c#

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?


#479199    Author: Anil Kumar Pandey    03 Mar 2010 Member Level: Diamond   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    03 Mar 2010 Member Level: Gold   Points : 2


You can go with this tutorial.


#479210    Author: Kolan  03 Mar 2010 Member Level: Gold   Points : 2

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.