×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

贴出原代码,欢迎转载,讨论

   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:  }
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / Excel question: there are multiple excel files that all contain a sheet with same sheet name, such as AAA. Now I want to change the sheet name to BBB. I don't want to repeat open-rename-close the files, does anyone know how to change sheet name at once.
    Thank you.
    • 不知道有什么常规做法,但是花了几分钟写了一个.net程序可以帮你转换,如果需要,pm 你的邮箱,前提是你需要安装.net 4.0
      • 贴出原代码,欢迎转载,讨论
           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:  }
        • workBook.Save(); 要在改名字之后,要不白存一次
          49: if (((dynamic)sheet).Name == oldSheetName)
          50: {
          51: ((dynamic)sheet).Name = newSheetName;
          workBook.Save();

          52: break;
          53: }
          54: }
          55:
          56: 57: workBook.Close();
          • OK,明白你的意思,确实应该这么改一下,thanks
        • 快猫.
        • If we have both 2007 and 2003 spreadsheets?