Monday, June 3, 2019

Rename the Excel Sheet Name in C#


Rename Excel Sheet C#;Excel sheet rename using C#;Excel sheet rename programmatically c#




Rename Excel Sheet C# Code


1) The following code renames an excel worksheet in C#.

To rename an excel sheet we need to open the excel file, rename the sheets, save and close it.

For open an Excel 2007 workbook ,we have to add the Microsoft Excel 12.0 Object Library (For Excel 2007, but it may vary depends on the Excel version you are using) in you project.


From the following pictures to show how to add Excel reference library in your project.
Select Microsoft Excel 12.0 Object Library under COM Tab and click OK button

TEST.xlsx with sheet name "Sheet1"
Now we can start coding to open and rename the first excel sheet from "Sheet1" to "General".

C#  Code

1. Add a button in a windows form



2. Add the below using statement
using Excel = Microsoft.Office.Interop.Excel; 


3. Add the below function

private void RenameExcelSheet(string sFileName)
        {

            Excel.Application app = new Excel.Application();            

            Excel.Workbook excelWorkbook;

            Excel.Worksheet excelWorkSheet;

            try
            {                

                excelWorkbook = app.Workbooks.Open(sFileName);

                if (excelWorkbook.Sheets.Count > 0)
                {

                    excelWorkSheet = excelWorkbook.Sheets[1];
                    //Rename the sheet 
                    excelWorkSheet.Name = "General";
                }

                //Save the excel 
                excelWorkbook.Save();
                //Close the excel
                excelWorkbook.Close();

            }

            catch (Exception ex)
            {
                MessageBox.Show( "Export Excel Failed: " + ex.Message);
            }


            finally
            {
                //Clean up objects 

                app.Quit();
                app = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();

            }
        }

4. Double click the button and paste the code in Click event of btnClick.


private void btnClick_Click(object sender, EventArgs e)
        {
            RenameExcelSheet("C:\\Viji\\Test.xlsx");

        }


4. Execute the project and click the Rename Excel button.


After rename



It's so simple !!!!

Code Comments:

using Excel = Microsoft.Office.Interop.Excel; - we assign the excel reference to a variable Excel.



10 comments:

  1. I used the above code but its giving runtime error in server.any ideas

    ReplyDelete
  2. You are my savior. Easiest thing to code took a while to figure out. Thanks much.

    ReplyDelete
  3. not working, sheets cannot be use like a method!

    ReplyDelete
    Replies
    1. Hi Dizzy,

      Thanks for your comment. Wasn't quite active in the blog and finally have updated the working code.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. very nice and provide me informative content thanks for sharing for more information about Rename Sheet in Excel

    ReplyDelete
  6. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. personal budget template

    ReplyDelete
  7. Nice blog! Blog is very useful for everyone who want to store excel data in online excel database. Trunao provide online tool to convert your for Excel spreadsheet into a database.

    ReplyDelete