-4

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?

  • 1
    possible 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 Answers2

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
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
  • re check again @Pierre-LucPineault – NewKidOnTheBlock May 14 '15 at 18:29
  • @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