如何在C#中动态地重新命名Excel工作表名称名称、动态、如何在、工作

2023-09-02 21:46:04 作者:岁月流年﹌

我已经创建了一个Excel工作簿中有许多像张工作表Sheet1,Sheet2中,...等等。我如何在C#中动态重命名这些标签的名字?

I have created an excel workbook with many sheets like sheet1, sheet2,... etc. How can I rename these tab names dynamically in C#?

推荐答案

您没有spedify你如何访问Excel文件。然而,例如,从这里可能,如果你使用微软对您有用.Office.Interop.Excel 。请注意,打开文件的第一页,一行:(表)xlBook.Worksheets.get_Item(1)

You didn't spedify how do you access the excel file. However, example from here might be useful for you if you're using Microsoft.Office.Interop.Excel. Note that it opens first sheet in the file, line: (Worksheet)xlBook.Worksheets.get_Item(1)

using Excel = Microsoft.Office.Interop.Excel; 

    object oMissing = System.Reflection.Missing.Value;
    Excel.ApplicationClass xl=new Excel.ApplicationClass();
        Excel.Workbook xlBook;
        Excel.Worksheet xlSheet;
        string laPath = Server.MapPath(@"excelxl_table.xls");
        xlBook = (Workbook)xl.Workbooks.Open(laPath,oMissing,
          oMissing,oMissing,oMissing ,oMissing,oMissing,oMissing
         ,oMissing,oMissing,oMissing,oMissing,oMissi ng,oMissing,oMissing);
        xlSheet = (Worksheet)xlBook.Worksheets.get_Item(1);
        xlSheet.Name = "CIAO";
        xlBook.Save();
        xl.Application.Workbooks.Close();