簡體   English   中英

導出嵌套列表項

[英]Exporting nested list items

我有一個程序,自動生成按鈕的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:與上述相同的eroor

那么你的代碼存在很多問題,主要是你的函數接收的參數Type

例如:1。 rowList是一個List<Button> ,你將它傳遞給函數Do() ,函數Do需要一個List<Button[][]>

2。 更糟糕的是, AddData期望接收一個Buttons數組,但AddData的整個代碼認為你只有一個按鈕不是數組。

3。 調用ToArgb()返回int,但是你試圖將它放在Color

如果沒有試圖真正理解你想要做什么 ,我猜你這是怎么想聲明的功能:

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

和:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM