将按钮网格数组导出到Ms Excel

时间:2013-03-20 19:44:16

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

嗨,我有一个小程序,使用一组按钮生成一个网格。点击按钮时,它们会改变颜色。如何将此布局导出到Ms Excel中,确切地说它在表单上是否可能?我希望Excel中的单元格代表按钮

我的程序如下所示:enter image description here

我如何创建网格:

int col = 7;
int row = 4;
int count = 0;
Button[][] buttons;

public void placeRows()
        {
            for (int r = 0; r < row; r++)
            {
                createColumns(r);
            }
        }

        public void createColumns(int r)
        {
            int s = r * 25; //gap
            for (int c = 0; c < col; c++)
            {
                buttons[r][c] = new Button();
                buttons[r][c].SetBounds(75 * c, s, 75, 25);
                buttons[r][c].Text = Convert.ToString(c);
                buttons[r][c].Click += new EventHandler(grid_Click);
                panel1.Controls.Add(buttons[r][c]);
            }
        }

private void Form1_Load(object sender, EventArgs e)
        {
            placeRows();
        }

 void grid_Click(object sender, EventArgs e)
         {
             Button button = sender as Button;

             if (count == 0)
             {
                 button.BackColor = Color.Red;
                 count++;
             }

             else if (count == 1)
             {
                 button.BackColor = Color.Blue;
                 count--;
             }
         }

private void btnExport_Click(object sender, EventArgs e)
         {
             //have no idea how to start this

         }

2 个答案:

答案 0 :(得分:1)

您可以使用Microsoft Excel COM组件。

首先,您必须添加Microsoft Excel XX.X对象库作为参考(在我的情况下,我使用的是12.0)。

下面的代码包含简单的类,它使用按钮数组创建新的工作表。如果要保存工作区,则应在app.SaveWorkspace(excelName);方法中取消注释Do

using Microsoft.Office.Interop.Excel;

namespace SimpleExcelExport
{
    class Export
    {
        public Export(bool defaultBackgroundIsWhite)
        {
            this.defaultBackgroundIsWhite = defaultBackgroundIsWhite;

            app = new Application();
            app.Visible = true;
            workbook = app.Workbooks.Add(1);
            worksheet = (Worksheet)workbook.Sheets[1];
        }       

        public void Do(string excelName, System.Windows.Forms.Button[][] array)
        {
            for (int i = 0; i <= array.GetUpperBound(0); i++)
            {
                for (int j = 0; j <= array[i].GetUpperBound(0); j++)
                {
                    AddData(i, j, array[i][j]);
                }
            }

            //app.SaveWorkspace(excelName);
        }            

        private void AddData(int row, int col, System.Windows.Forms.Button button)
        {
            if (button == null) return;
            row++;
            col++;
            Range range = worksheet.Cells[row, col];
            if (!defaultBackgroundIsWhite)
            range.Interior.Color = button.BackColor.ToArgb();
            else
                range.Interior.Color = button.BackColor.Name != "Control" ? button.BackColor.ToArgb() : System.Drawing.Color.White.ToArgb();
            range.NumberFormat = "";
            worksheet.Cells[row, col] = button.Text;
        }

        private Application app = null;
        private Workbook workbook = null;
        private Worksheet worksheet = null;
        private Range workSheet_range = null;
        private bool defaultBackgroundIsWhite;
    }
}

使用示例:

private void btnExport_Click(object sender, EventArgs e)
{
    Export ep = new Export(true);
    ep.Do("test.xsl", buttons);
}

答案 1 :(得分:-1)

你要做的是创建一些数据就绪的数组,这样你就可以做两件事:

  1. 根据该阵列生成按钮。
  2. 将此数组导出到MS Excel,如CSV
  3. 所以放弃那条线

    panel1.Controls.Add(buttons[r][c]);
    

    并使用数据填充数组。