将dynamic标签文本导出到Excel

我有一个小程序,在flowLayoutPanel1中生成一些dynamic标签我试图将这些标签的文本导出到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); } 

我的结果:

在这里输入图像说明

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

您总是添加最后创build的标签的文本,因为您只传递其引用。 您应该将带有所有要将文本属性导出到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]); } } 

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

这是一个有点笨重的TBH,而且很大程度上依赖于UI的机制,所以不build议 – 用你的UI数据绑定的模型,你会更好,但是如果你想得到它完成了,那是你的问题。