当前,我必须指定必须将值添加到的单元格。我如何喜欢以某种方式递增,也就是说,无论用户单击“添加到文件”按钮有多少次,它都应该递增以前的模式。
例如。我有 :

        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/

10-12 18:29