命名Excel工作表使用数组C#

我试图命名创build一个新的Excel工作表,并有variables名称来自一个数组。 码…

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Worksheet newWorksheet; string[] Numbers = new string[12] { "1","2","3","4","5","6","7","8","9","10","11","12"}; string[] Months = new string[12] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; for (int i = 0; i < Months.Length; i++) { newWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlApp.Worksheets.Add(); Microsoft.Office.Interop.Excel.Worksheet Numbers[i] = (Worksheet)xlApp.Worksheets["Sheet" + i]; Numbers[i].Name = Months[i]; } 

我试图让新的工作表的variables名称是数组“数字”中的数字。 当我尝试声明一个数组时,我得到一个错误。 有没有办法做到这一点?

谢谢。

你的代码有点混乱。 两个不同的数组使用名称Month

 string[] Months 

 Microsoft.Office.Interop.Excel.Worksheet Months[i] 

结束这个非常混乱的代码

 Months[i].Name = Months[i]; 

重命名你的一个数组。