.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Opening an Excel from C# and Insert a column at the last

Posted By:Suderson Sarathy       Posted Date: November 22, 2010    Points: 5    Category: C#    URL: http://www.dotnetspark.com  

This article contains the code which is used to open an excel file from the C#, inserts a column at the end of it and save the file and close it
 

 Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                                                        Workbook book;
                                                        book = app.Workbooks.Open(uploadpathforfpl2);
                                                        Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Sheets[1];
                                                        Range rng = sheet.UsedRange.Columns;
                                                        string LastColumnInTheSheet = Convert.ToChar(64 + sheet.UsedRange.Columns.Count).ToString();

                                                        try
                                                        {

                                                            rng = (Microsoft.Office.Interop.Excel.Range)sheet.get_Range(LastColumnInTheSheet + "1", LastColumnInTheSheet + sheet.UsedRange.Rows.Count);
                                                            rng = (Microsoft.Office.Interop.Excel.Range)sheet.Cells[1, sheet.UsedRange.Columns.Count + 1];
                                                            rng.Value = "visited Link ";

                                                        }

                                                        finally
                                                        {

                                                            book.Save();
                                                            book.Close(true, Type.Missing, Type.Missing);
                                                            app.Workbooks.Close();
                                                            app.Application.Quit();

                                                            // release all the application object from the memory
                                                            Marshal.ReleaseComObject(rng);
                                                            Marshal.ReleaseComObject(sheet);
                                                            Marshal.ReleaseComObject(book);
                                                            Marshal.ReleaseComObject(app);
                                                            GC.Collect();
                                                            GC.WaitForPendingFinalizers();

                                                        }
The code describes the operation of opening an excel using the Excel.Interop Services of microsoft. Add the reference to the cs file and paste it in a button event.
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend