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: 12     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

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




Responses

#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

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


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM


 
#479206    Author: Raj Sharma      Member Level: Gold      Member Rank: 125     Date: 03/Mar/2010   Rating: 2 out of 52 out of 5     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


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

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




 
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.



Next : Need to use this asp.net setup in other s/m??
Previous : Permissions issue
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages

Subscribe to Subscribers
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India