user1210826
user1210826

Reputation: 1152

How to change excel filename

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();

Upvotes: 0

Views: 5280

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156918

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);

Upvotes: 3

Related Questions