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

发布于 2024-10-10 04:17:41 字数 66 浏览 4 评论 0 原文

我创建了一个包含许多工作表(如sheet1、sheet2等)的Excel工作簿。如何在C#中动态重命名这些选项卡名称?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2024-10-17 04:17:41

您没有具体说明如何访问 Excel 文件。但是,如果您使用 此处 中的示例可能对您有用>Microsoft.Office.Interop.Excel。请注意,它打开文件中的第一个工作表,行:(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(@"\excel\xl_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();

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(@"\excel\xl_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();
我的鱼塘能养鲲 2024-10-17 04:17:41

一小段注释:
如果您不需要指定它们,您可以删除所有这些可选参数并使用缩写形式:

xlBook = (Workbook)xl.Workbooks.Open(laPath);

Regards,
约尔格

One short note:
If you don't need to specify them, you can get rid of all these optional parameters and use the short form:

xlBook = (Workbook)xl.Workbooks.Open(laPath);

Regards,
Jörg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文