简体   繁体   English

使用Array C#命名Excel工作表

[英]Name Excel Worksheet using Array C#

I'm trying to name create a new excel worksheet and have the variable name come from an array. 我正在尝试命名创建一个新的Excel工作表,并让变量名称来自一个数组。 Code... 码...

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];
            }

I am trying to have the variable name of the new worksheet be the number from the array "Numbers". 我想让新工作表的变量名是数字“Numbers”中的数字。 I get an error when trying since it thinks I am trying to declare an array. 我在尝试时遇到错误,因为它认为我正在尝试声明一个数组。 Is there any way to do this? 有没有办法做到这一点?

Thanks. 谢谢。

Your code is a bit confusing. 你的代码有点令人困惑。 You are using the name Month for two different arrays 您为两个不同的数组使用名称Month

string[] Months 

and

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

Ending up with this very confusing code 结束这个非常令人困惑的代码

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

Rename one of your arrays. 重命名一个阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM