1:
2: using System;
3: using System.Collections.Generic;
4: using System.Linq;
5: using System.Text;
6: using System.IO;
7: using Excel = Microsoft.Office.Interop.Excel;
8:
9: namespace ConsoleApplication4
10: {
11: class Program
12: {
13: static void Main(string[] args)
14: {
15: if (args.Length == 3)
16: {
17: string dir = args[0];
18: string oldName = args[1];
19: string newName = args[2];
20:
21: var excelFiles = (new DirectoryInfo(dir))
22: .GetFiles()
23: .Where(file => file.Extension.EndsWith("xlsx"));
24:
25: foreach (var excelFile in excelFiles)
26: {
27: changeSheetNameAndSave(excelFile.FullName, oldName, newName);
28: }
29:
30: Console.WriteLine("finished converting, press enter to exist");
31: Console.ReadLine();
32: }
33: else
34: {
35: Console.ForegroundColor = ConsoleColor.Yellow;
36: Console.WriteLine("you have to input 3 parameters: folder path(example: c:\\zzz\\ccc\\xxx),old sheet name to be replaced, new sheet name");
37: Console.ForegroundColor = ConsoleColor.White;
38: Console.ReadLine();
39: }
40: }
41:
42: static void changeSheetNameAndSave(string excelFileName, string oldSheetName, string newSheetName)
43: {
44: var excelApplication = new Excel.Application();
45: excelApplication.Workbooks.Open(excelFileName);
46: var workBook = excelApplication.ActiveWorkbook;
47: foreach (var sheet in workBook.Sheets)
48: {
49: if (((dynamic)sheet).Name == oldSheetName)
50: {
51: ((dynamic)sheet).Name = newSheetName;
52: break;
53: }
54: }
55:
56: workBook.Save();
57: workBook.Close();
58: }
59: }
60: }