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
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" |
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.
I used the above code but its giving runtime error in server.any ideas
ReplyDeleteYou are my savior. Easiest thing to code took a while to figure out. Thanks much.
ReplyDeletenot working, sheets cannot be use like a method!
ReplyDeleteHi Dizzy,
DeleteThanks for your comment. Wasn't quite active in the blog and finally have updated the working code.
This comment has been removed by the author.
ReplyDeletevery nice and provide me informative content thanks for sharing for more information about Rename Sheet in Excel
ReplyDeleteThis 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
ReplyDeleteNice 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.
ReplyDeleteAivivu đại lý vé máy bay, tham khảo
ReplyDeleteVe may bay di My
vé máy bay phú yên sài gòn
vé máy bay đà nẵng hà nội tháng 10
vé máy bay huế đà lạt
vé máy bay đi quy nhơn tháng 5
taxi ở sân bay nội bài
combo đà lạt tháng 12
4B695
ReplyDeletesightcare official website
pubg uc satın al