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?


Comments

Author: Anil Kumar Pandey03 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


http://mannpandey.blogspot.com/2009/06/writing-excel-file-with-data-set.html



check this for reading


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

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;
toolStripProgressBar1.PerformStep();
}

Author: Raj Sharma03 Mar 2010 Member Level: Gold   Points : 2

Hi

You can go with this tutorial.
http://www.dotnetspider.com/resources/2082-article-explain-read-data-from-Excel-sheet-an.aspx

Thanks
Raj

Author: Kolan03 Mar 2010 Member Level: Gold   Points : 2

http://www.dotnetspider.com/resources/2082-article-explain-read-data-from-Excel-sheet-an.aspx