1

flowLayoutPanel1我有一个小程序,可以在我尝试将这些标签的文本导出到 Excel时生成一些动态标签,但我得到的只是最后一个标签的值。

这是我的出口课程:

 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.Label names)
        {
            for (int i = 0; i <= 5; i++)
            {
                    AddNames(i,0,names);
            }
        }

        private void AddNames(int row, int col, System.Windows.Forms.Label lbls)
        {
            if (lbls == null) return;
            row++;
            col++;
            Range range = worksheet.Cells[row + 2, col + 2];
            range.NumberFormat = "";
            worksheet.Cells[row + 2, col + 2] = lbls.Text;
            row--;
            col--;

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

表单类代码:

    public Form1()
    {
        InitializeComponent();
    }

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

    Label lbl;
    private void create()
    {
        flowLayoutPanel1.Controls.Clear();
        //int length = ds.Tables[0].Rows.Count;
        for (int i = 0; i < 5; i++)
        {
            lbl = new Label();
            lbl.Name = i.ToString();
            lbl.Text = "Label "+i;
            lbl.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
            lbl.SetBounds(0, 20, 100, 25);
            lbl.BorderStyle = BorderStyle.FixedSingle;
            flowLayoutPanel1.Controls.Add(lbl);
        }
    }

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

我的结果:

在此处输入图像描述

4

3 回答 3

3

您总是添加最后创建的标签的文本,因为您只是传递它的引用。相反,您应该传递列表,其中包含您希望导出到 Excel 的文本属性的所有标签的引用。更改这些方法:

    List<Label> lbls;
    private void create()
    {
        flowLayoutPanel1.Controls.Clear();
        //int length = ds.Tables[0].Rows.Count;
        lbls = new List<Labels>();
        for (int i = 0; i < 5; i++)
        {
            Label lbl = new Label();
            lbl.Name = i.ToString();
            lbl.Text = "Label "+i;
            lbl.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
            lbl.SetBounds(0, 20, 100, 25);
            lbl.BorderStyle = BorderStyle.FixedSingle;
            flowLayoutPanel1.Controls.Add(lbl);
            lbls.Add(lbl);
        }
    }

还要更改类中的方法DoExport接受List<Label>代替Label

    public void Do(string excelName, List<Label> names)
    {
        for (int i = 0; i <= names.Count; i++)
        {
                AddNames(i,0,names[i]);
        }
    }
于 2013-03-20T23:16:46.643 回答
2
List<Label> lbls = new List<Label>();
private void create()
{
    flowLayoutPanel1.Controls.Clear();
    //int length = ds.Tables[0].Rows.Count;
    for (int i = 0; i < 5; i++)
    {
        lbl = new Label();
        lbl.Name = i.ToString();
        lbl.Text = "Label "+i;
        lbl.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
        lbl.SetBounds(0, 20, 100, 25);
        lbl.BorderStyle = BorderStyle.FixedSingle;
        lbls.Add(lbl);   //< -- add the label to the local list of Labels
        flowLayoutPanel1.Controls.Add(lbl);

    }
}

private void button1_Click(object sender, EventArgs e)
{
    int i = 0;
    Export ep = new Export(true);
    foreach(var lbl in lbls)
    {
        i++;
        ep.AddNames(i,0,lbl);
    }
}

public void AddNames(int row, int col, System.Windows.Forms.Label lbl)
{
if (lbl == null) return;
row++;
col++;
Range range = worksheet.Cells[row + 2, col + 2];
range.NumberFormat = "";
worksheet.Cells[row + 2, col + 2] = lbl.Text;
row--;
col--;

}
于 2013-03-20T23:15:35.670 回答
2

每次围绕 create() 方法中的 for 循环构建一个新标签,并将该标签分配给同一字段 (lbl)。完成后, lbl 是您创建的最后一个标签。您可以改为将标签添加到列表中,或者将 flowLayoutPanel1.Controls 传递给 go() 方法,前提是您可以确定它只包含您希望导出的标签。

这有点笨重 TBH,并且不推荐如此严重地依赖于 UI 的机制 - 你最好使用一个经过深思熟虑的模型,你的 UI 是数据绑定的,但如果你只想得到完成了,那是你的问题。

于 2013-03-20T23:16:02.417 回答