C# 导出嵌套列表项

C# 导出嵌套列表项,c#,winforms,visual-studio-2010,export-to-excel,C#,Winforms,Visual Studio 2010,Export To Excel,我有一个程序,可以自动生成按钮的二维网格,并将网格存储在嵌套列表中,我正在尝试将此列表导出到MS Excel。然而,我正在尝试的代码抛出了许多错误。我可以在不使用列表的情况下实现这一点,但我需要使用嵌套列表来清除列表,并在网格大小增加或减少时再次填充它。我使用的逻辑是否可行 详情如下: //This is not the complete code List<List<Button>> buttonss = new List<List

我有一个程序,可以自动生成按钮的二维网格,并将网格存储在嵌套列表中,我正在尝试将此列表导出到MS Excel。然而,我正在尝试的代码抛出了许多错误。我可以在不使用列表的情况下实现这一点,但我需要使用嵌套列表来清除列表,并在网格大小增加或减少时再次填充它。我使用的逻辑是否可行

详情如下:

        //This is not the complete code
        List<List<Button>> buttonss = new List<List<Button>>();
        List<Button> rowList = new List<Button>();

        //Some method that creates a grid of buttons 
        buttons = new Button[row][];
        for (int r = 0; r < row; r++)
        { 
            buttons[r] = new Button[col];
            buttonss.Add(rowList);    
            for (int c = 0; c < col; c++)
            {
                buttons[r][c] = new Button();
                rowList.Add(buttons[r][c]);
            }
        }
类别:

//Export class
public void Do(string excelName, System.Collections.Generic.List<Button[][]> Grid)
{
    for (int i = 0; i <= Grid.Count(); i++)
    {
        for (int j = 0; j <= Grid[i].Count(); j++)
        {

            AddData(i, j, Grid[i][j]);//(ERROR HERE [1])

        }
    }
    //app.SaveWorkspace(excelName);
}

public void AddData(int row, int col, System.Collections.Generic.List<Button[][]> button)
{
    if (button == null) return;
    row++;
    col++;

    Range range = worksheet.Cells[row + 2, col + 2];
    if (!defaultBackgroundIsWhite)
    {
        range.Interior.Color = button.BackColor.ToArgb();//(ERROR HERE[2])
    }
    else
        range.Interior.Color = button.BackColor.Name != "Control" ? button.BackColor.ToArgb() : System.Drawing.Color.White.ToArgb();//(ERROR HERE)
    // range.NumberFormat = "";
    worksheet.Cells[row + 2, col + 2] = button.Text;//(ERROR HERE[3])
    row--;
    col--;
}
//导出类
public void Do(字符串excelName,System.Collections.Generic.List网格)
{

对于(int i=0;i您的代码有很多问题,主要是函数接收的参数的类型

例如: 1.
行列表
是一个
列表
,您正在将其传递给函数
Do()
,但函数确实需要一个
列表

2.更糟糕的是,
AddData
希望收到一个按钮数组,但是
AddData
中的整个代码认为您只有一个按钮而没有数组

3.调用
ToArgb()
返回int,但您试图将其置于
颜色中

在没有真正理解您要做什么的情况下,我猜这就是您想要声明函数的方式:

public void Do(string excelName, System.Collections.Generic.List<Button[]> Grid)

如果你想知道我该如何将其排序,首先要正确声明函数的参数。然后试着理解为什么你要将
列表
传递给
Do
函数,该函数可能需要一个
列表
(基于你提供的代码)。我纠正了这一点,但它说的是AddData(I,j,hwGrid[I][j]),还有ep.Do(“sheet.xsl”,rowList);在AddData之外有一些无效的参数,如果你按照我告诉你的那样写它就不应该了。关于Do(…,rowList)-正如我说的,你传递的是一个列表而不是一个列表,你的意思是我需要将rowList变成一个数组吗
public void Do(string excelName, System.Collections.Generic.List<Button[]> Grid)
public void AddData(int row, int col, Button button)