C# 第一次单击按钮时,数据不是保存在excel中,而是第二次保存在WinForms中

C# 第一次单击按钮时,数据不是保存在excel中,而是第二次保存在WinForms中,c#,winforms,excel,file,button,C#,Winforms,Excel,File,Button,我编写了一段代码,可以访问excel文件,并将excel文件中的数据提取到一个名为“liste”的二维数组中。它还将用户的输入从程序的文本框和组合框传输到excel文件中。问题是,我的程序不会在用户第一次输入所需数据时传输数据,而是在我第二次按下“kaydet”按钮时传输数据。我已经调试并检查了if语句,它们与我输入的数据配合得很好,但在我第一次单击“kaydet”按钮时似乎没有保存。我在C中使用WinForms# 任何帮助都将不胜感激。 多谢各位 private void button_kay

我编写了一段代码,可以访问excel文件,并将excel文件中的数据提取到一个名为“liste”的二维数组中。它还将用户的输入从程序的文本框和组合框传输到excel文件中。问题是,我的程序不会在用户第一次输入所需数据时传输数据,而是在我第二次按下“kaydet”按钮时传输数据。我已经调试并检查了if语句,它们与我输入的数据配合得很好,但在我第一次单击“kaydet”按钮时似乎没有保存。我在C中使用WinForms#

任何帮助都将不胜感激。 多谢各位

private void button_kaydet_Click(object sender, EventArgs e){

        //the text that is obtained from the text boxes and combo boxes.

        string txt_LRU = this.txtbx_LRUno.Text.ToString();
        string txt_yi = this.txtbx_yi.Text.ToString();
        string txt_td = this.cmbx_td.Text.ToString();
        string txt_toptarih = this.dtp_toplanti.Text.ToString();
        string txt_bastarih = this.dtp_bas.Text.ToString();
        string txt_teslimtarih = this.dtp_teslim.Text.ToString();
        string txt_ilgilinot = this.txtbx_ilgiliNot.Text.ToString();
        string txt_acikislem = this.txtbx_acikislem.Text.ToString();
        string txt_referedosya = this.txtbx_referedosya.Text.ToString();
        string txt_parcano = this.txtbx_parcano2.Text.ToString();

        int lru_row = 0;
        int kontrol = 0;

        for (int f = 1; f < row_number + 1; f++)
        {
            //this if statements checks if the user's input already exists in the excel                   file
            if (txt_LRU == liste[f - 1, 1])
            {
                kontrol = 1;
                lru_row = f + 1;
                //when first starting in the liste[0][0] has these indexes,in the excel cells
                //liste[0][0] actually starts at Cell[1][2]

                string hata_mesaji = "LRU " + txt_LRU + " already exists,would you like to update ?";
                DialogResult dialogResult = MessageBox.Show(hata_mesaji, "Error", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {


                    //remember that Cell[column][row]

                    sheet1.Cells[2][lru_row] = txt_LRU;
                    sheet1.Cells[3][lru_row] = txt_parcano;
                    sheet1.Cells[4][lru_row] = txt_yi;
                    sheet1.Cells[5][lru_row] = txt_acikislem;
                    sheet1.Cells[7][lru_row] = txt_td;
                    sheet1.Cells[8][lru_row] = txt_toptarih;

                    MessageBox.Show("The data has been updated");

                }

                else if (dialogResult == DialogResult.No)
                    MessageBox.Show("The data has not been updated");
            }

        }
            //if there is no existing LRU then save the data into a new row in excel
            if (kontrol == 0)
            {

                int x = row_number + 1;
                string satir_no = x.ToString();
                sheet1.Cells[1][row_number + 2] = satir_no;
                sheet1.Cells[2][row_number + 2] = txt_LRU;
                sheet1.Cells[3][row_number + 2] = txt_parcano;
                sheet1.Cells[4][row_number + 2] = txt_yi;
                sheet1.Cells[5][row_number + 2] = txt_acikislem;
                sheet1.Cells[7][row_number + 2] = txt_td;
                sheet1.Cells[8][row_number + 2] = txt_toptarih;
                sheet1.Cells[9][row_number + 2] = txt_bastarih;
                sheet1.Cells[10][row_number + 2] = txt_teslimtarih;
                sheet1.Cells[11][row_number + 2] = txt_ilgilinot;


            }


        //to save and close the excel file
        uyg.DisplayAlerts = false;
        kitap.Save();
        kitap.Close();
        uyg.DisplayAlerts = true;
        uyg.Quit();

       DialogResult dialogResult2 = MessageBox.Show("Would you like to see the excel        file?", "Bilgilendirme", MessageBoxButtons.YesNo);
        if (dialogResult2 == DialogResult.Yes)
        {
            Process.Start(@"C:\\Users\\casperpc\\Desktop\\hey.xls");

        }
        else if (dialogResult2 == DialogResult.No)
        {

        }
private void按钮\u kaydet\u单击(对象发送方,事件参数e){
//从文本框和组合框中获取的文本。
字符串txt_LRU=this.txtbx_LRUno.Text.ToString();
字符串txt_yi=this.txtbx_yi.Text.ToString();
字符串txt_td=this.cmbx_td.Text.ToString();
字符串txt_toptarih=this.dtp_toplanti.Text.ToString();
string txt_bastarih=this.dtp_bas.Text.ToString();
字符串txt_teslimtarih=this.dtp_teslim.Text.ToString();
字符串txt_ilgilinot=this.txtbx_ilgilinot.Text.ToString();
字符串txt_acikislem=this.txtbx_acikislem.Text.ToString();
字符串txt_referedosya=this.txtbx_referedosya.Text.ToString();
字符串txt_parcano=this.txtbx_parcano2.Text.ToString();
int lru_行=0;
int-kontrol=0;
对于(int f=1;f<行数+1;f++)
{
//此if语句检查用户的输入是否已存在于excel文件中
如果(txt_LRU==liste[f-1,1])
{
康特罗=1;
lru_行=f+1;
//当第一次在列表中启动时,[0][0]在excel单元格中具有这些索引
//列表[0][0]实际上从单元格[1][2]开始
字符串hata_mesaji=“LRU”+txt_LRU+”已存在,是否要更新;
DialogResult DialogResult=MessageBox.Show(hata_mesaji,“Error”,MessageBoxButtons.YesNo);
如果(dialogResult==dialogResult.Yes)
{
//记住那个单元格[列][行]
表1.单元格[2][lru行]=txt\U lru;
表1.单元格[3][lru_row]=txt_parcano;
表1.单元格[4][lru_行]=txt_-yi;
表1.单元格[5][lru_行]=txt_acikislem;
表1.单元格[7][lru_行]=txt_td;
表1.单元格[8][lru_行]=txt_toptarih;
MessageBox.Show(“数据已更新”);
}
else if(dialogResult==dialogResult.No)
MessageBox.Show(“数据尚未更新”);
}
}
//如果没有现有LRU,则将数据保存到excel中的新行中
如果(kontrol==0)
{
int x=行数+1;
字符串satir_no=x.ToString();
表1.单元格[1][行号+2]=序号;
表1.单元格[2][行数+2]=txt\U LRU;
表1.单元格[3][行号+2]=txt\U parcano;
表1.单元格[4][行号+2]=txt\U yi;
表1.单元格[5][row_number+2]=txt_acikislem;
表1.单元格[7][行号+2]=txt\U td;
表1.单元格[8][row_number+2]=txt_toptarih;
表1.单元格[9][行数+2]=txt\U bastarih;
表1.单元格[10][row_number+2]=txt_teslimtarih;
表1.单元格[11][row_number+2]=txt_ilgilinot;
}
//保存并关闭excel文件的步骤
viu.DisplayAlerts=false;
kitap.Save();
kitap.Close();
viu.DisplayAlerts=true;
维吾尔语,Quit();
DialogResult dialogResult2=MessageBox.Show(“是否要查看excel文件?”,“Bilgilendirme”,MessageBox按钮.YesNo);
如果(dialogResult2==DialogResult.Yes)
{
Process.Start(@“C:\\Users\\casperpc\\Desktop\\hey.xls”);
}
else if(dialogResult2==DialogResult.No)
{
}

}

您好,这听起来很简单,但您在更新后保存了Excel文档吗?是的,我的程序会自动保存传输到Excel文件的输入。我没有发布全部代码,可能是因为代码太拥挤,但我现在已经更新了代码。