我创建了一个包含许多工作表(例如sheet1,Sheet2等)的excel工作簿。如何在C#中动态重命名这些选项卡名称?
最佳答案
您没有简化如何访问excel文件的操作。但是,如果您使用Microsoft.Office.Interop.Excel
,那么here的示例可能对您有用。请注意,它将打开文件中的第一张纸,行:(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();
关于c# - 如何在C#中动态重命名Excel工作表名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4561012/