0

My question is how to we change the excel filename without we manual changing by our own? Example , i get a list of naming from excel format from vendor and he will put on the specified location . I need to run a program which using this file to generate a cancellation progress with a specific format of excel in MMYYSP15.

Here my code and i wish to add on the function as i need . Kindly advise

object oMissing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Excel.ApplicationClass xl = new Microsoft.Office.Interop.Excel.ApplicationClass();

Microsoft.Office.Interop.Excel.Workbook xlBook;
Microsoft.Office.Interop.Excel.Worksheet xlSheet;
//System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;

string laPath = System.IO.Path.GetFullPath("D:\\New & Renewal Summary Report 201409.xls");
xlBook = (Microsoft.Office.Interop.Excel.Workbook)xl.Workbooks.Open(laPath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.Worksheets.get_Item(1);
xlSheet.Name = "Sheet 1";
xlBook.Save();
xl.Application.Workbooks.Close();
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user1210826
  • 1,122
  • 1
  • 9
  • 10
  • If you want to change the name of the file, then use ```File.Move()``` More details there : http://stackoverflow.com/questions/3218910/rename-a-file-in-c-sharp – Gregoire D. Jan 12 '15 at 09:20
  • Hi Gregoire , so i just implement this method on the code ?System.IO.File.Move("oldfilename", "newfilename"); – user1210826 Jan 12 '15 at 09:22
  • Yes like @Patrick Hofman suggested below, where ```fromFileName``` would be ```laPath``` in your code, and ```toFileName``` would be the path with the new name you want. – Gregoire D. Jan 12 '15 at 09:29

1 Answers1

3

You have to call xlBook.SaveAs. You can't change the current file's name.

xlBook.SaveAs(Filename: yourFileName);

If you do not intend to do anything with the Excel workbook itself, a simple File.Move would do.

File.Move(fromFileName, toFileName);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325