繁体   English   中英

将动态标签文本导出到Excel

[英]Export dynamic label text to Excel

我有一个小程序,可以在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);
    }

我的结果:

在此处输入图片说明

您始终会添加最后创建的标签的文本,因为您仅传递了其引用。 相反,您应该传递带有所有标签的引用的列表,这些标签要将Text属性导出到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);
        }
    }

还要在Export类中更改方法Do ,以接受List<Label>而不是Label

    public void Do(string excelName, List<Label> names)
    {
        for (int i = 0; i <= names.Count; i++)
        {
                AddNames(i,0,names[i]);
        }
    }
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--;

}

您每次都要在create()方法中的for循环周围构造一个新标签,并将该标签分配给同一字段(lbl)。 到您完成时,lbl是您创建的最后一个标签。 如果可以确定只包含您要导出的标签,则可以将标签添加到List中,或将flowLayoutPanel1.Controls传递给go()方法。

TBH有点笨拙,不建议过分依赖于UI的机制-建议您将UI绑定到数据的模型深思熟虑,否则效果会更好。完成了,那是你的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM