第一次单击按钮但第二次在WinForms中保存时,数据不会保存在Excel中

时间:2014-08-11 13:22:28

标签: c# winforms excel file button

我编写了一个访问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)
        {

        }

}

0 个答案:

没有答案