【问题标题】:Exporting nested list items导出嵌套列表项
【发布时间】:2013-03-27 23:30:06
【问题描述】:

我有一个程序可以自动生成 2D 按钮网格并将网格存储在嵌套列表中,我正在尝试将此列表导出到 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]);
            }
        }

接下来我要做的就是不要将此列表导出到 excel 中。

网格:

按钮:

//Export to MS Excel button
private void btnExport_Click(object sender, EventArgs e)
{
    ep.Do("sheet.xsl", rowList);//(ERROR 0)
}

类:

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

错误: 0:参数 2:无法从 'System.Collections.Generic.List' 转换为 'System.Collections.Generic.List' C:..

1: 'SmartRota.ExportHeadWaiter.AddData(int, int, System.Collections.Generic.List)' 的最佳重载方法匹配有一些无效参数 C:..

2:错误 3“System.Collections.Generic.List”不包含“BackColor”的定义,并且找不到接受“System.Collections.Generic.List”类型的第一个参数的扩展方法“BackColor” (您是否缺少 using 指令或程序集引用?) C:..

3: 和上面一样

【问题讨论】:

    标签: c# winforms visual-studio-2010 export-to-excel


    【解决方案1】:

    您的代码有很多问题,主要是您的函数接收的参数的Type

    例如: 1. rowListList&lt;Button&gt; 并且您将它传递给函数 Do() 但是,函数 Do 需要 List&lt;Button[][]&gt;

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

    3 .调用 ToArgb() 以 int 形式返回,但您试图将其放入 Color

    没有试图真正理解 你想要做什么,我猜这就是你想要声明你的函数的方式:

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

    和:

    public void AddData(int row, int col, Button button)
    

    【讨论】:

    • 知道如何解决这个问题
    • 从正确声明函数参数开始。然后尝试了解您为什么尝试将 List&lt;Button&gt; 传递给 Do 函数,该函数可能需要 List&lt;Button[]&gt;(基于您提供的代码)。
    • 我更正了,但它说 AddData(i, j, hwGrid[i][j]);和 ep.Do("sheet.xsl", rowList);有一些无效的参数
    • 关于 AddData,如果你像我告诉你的那样,它不应该这样写。关于 Do(..., rowList) - 正如我所说,您传递的是 List
    • 你的意思是我需要将rowlist变成一个数组
    猜你喜欢
    • 2012-08-09
    • 2011-11-05
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2013-11-26
    • 2020-04-14
    • 1970-01-01
    • 2018-03-15
    相关资源
    最近更新 更多