gpt4 book ai didi

c# - 根据每次写入的数据递增excel单元格

转载 作者:太空宇宙 更新时间:2023-11-03 21:07:04 24 4
gpt4 key购买 nike

目前我必须指定要添加值的单元格。我怎么喜欢以某种方式增加,比如无论用户点击添加到文件按钮多少次,它都应该只增加以前的模式。例如。我有:

        xlWorkSheet.Cells[1, 1] = comboBox2.Text;
xlWorkSheet.Cells[1, 2] = textBox5.Text;
xlWorkSheet.Cells[1, 3] = textBox2.Text;
xlWorkSheet.Cells[1, 4] = comboBox3.Text;
xlWorkSheet.Cells[1, 5] = textBox3.Text;
xlWorkSheet.Cells[1, 6] = comboBox1.Text;

单击“添加到 xls 文件”按钮后,我现在如何使其遵循这种模式并保存到文件,而不必将其写入代码:

        xlWorkSheet.Cells[2, 1] = comboBox2.Text;
xlWorkSheet.Cells[2, 2] = textBox5.Text;
xlWorkSheet.Cells[2, 3] = textBox2.Text;
xlWorkSheet.Cells[2, 4] = comboBox3.Text;
xlWorkSheet.Cells[2, 5] = textBox3.Text;
xlWorkSheet.Cells[2, 6] = comboBox1.Text;

我又不能每次都复制粘贴,那太乏味了。欢迎所有的回答和评论。提前致谢

最佳答案

您可以使用属性。

public int IndexProp {get; set;}

public void AddToExcelBtn_Click(object sender, EventArgs e)
{
//... some other code

Index +=1;
xlWorkSheet.Cells[IndexProp , 1] = comboBox2.Text;
xlWorkSheet.Cells[IndexProp , 2] = textBox5.Text;
xlWorkSheet.Cells[IndexProp , 3] = textBox2.Text;
xlWorkSheet.Cells[IndexProp , 4] = comboBox3.Text;
xlWorkSheet.Cells[IndexProp , 5] = textBox3.Text;
xlWorkSheet.Cells[IndexProp , 6] = comboBox1.Text;
}

关于c# - 根据每次写入的数据递增excel单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40533943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com