Ok, I've search all the other questions regarding this topic but have not yet found a good answer. I need to read an excel file and rename my excel worksheet programmatically. I'm using 2007 excel. Any help? Any Tip?
Asked
Active
Viewed 1,576 times
-4
-
1possible duplicate of [Reading Excel files from C#](http://stackoverflow.com/questions/15828/reading-excel-files-from-c-sharp) – JAL May 14 '15 at 18:32
-
You can use [Excel Interop](https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel%28v=office.15%29.aspx) – chancea May 14 '15 at 19:04
2 Answers
4
Personally, I'd use a free library such as EPPlus for this. I think it is much cleaner and easier to use then interop.
Something like this:
FileInfo finfo = new FileInfo(@"C:\Temp\Book1.xlsx");
using (var excelPackage = new ExcelPackage(finfo))
{
ExcelWorksheet ws = excelPackage.Workbook.Worksheets["Sheet1"];
ws.Name = "NewWorksheet Name";
excelPackage.Save();
}

Stewart_R
- 13,764
- 11
- 60
- 106
-
-
I did that already, but I'm getting red line under ..= new ExcelPackage – NewKidOnTheBlock May 14 '15 at 18:57
-
You need to first install EPPlus via Nuget then you can use `using OfficeOpenXml;` but, if its installed if you just place the cursor inside the `ExcelPackage()` word then hold down ctrl and hit . then Visual Studio should give you that option. – Stewart_R May 14 '15 at 19:10
-
1
Worksheet oSheet = (Worksheet)oWB.Worksheets["TheSheetYouWant"];
oSheet.Name = "NewName";

Little Fox
- 1,212
- 13
- 39
-
@NewKidOnTheBlock reread the question you've badly put together. This answers "I need to rename my excel worksheet programmatically." – Pierre-Luc Pineault May 14 '15 at 18:27
-
-
@NewKidOnTheBlock Indeed, if you edit the question _after_ an answer, it will get outdated. Instead of changing your requirements, how about you read [How to ask](http://stackoverflow.com/help/how-to-ask) and reformat it nicely with all the required info. – Pierre-Luc Pineault May 14 '15 at 18:31