C# 使用C添加到excel文件#

C# 使用C添加到excel文件#,c#,winforms,excel,C#,Winforms,Excel,我已经编写了一个代码,允许用户将数据输入程序,并将其传输到excel文件。但是,当我的一个文本框为空时,我不希望添加任何内容。使用此代码,尽管存在if-else条件,但仍会添加一行。我已调试并使用断点和txt_LRU!=即使txt_LRU为空或不为空,null也为真。有人能告诉我为什么会发生这种情况吗 private void button_kaydet_Click(object sender, EventArgs e){ //the text that is obtained fro

我已经编写了一个代码,允许用户将数据输入程序,并将其传输到excel文件。但是,当我的一个文本框为空时,我不希望添加任何内容。使用此代码,尽管存在if-else条件,但仍会添加一行。我已调试并使用断点和txt_LRU!=即使txt_LRU为空或不为空,null也为真。有人能告诉我为什么会发生这种情况吗

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;




        //if there is non existing LRU then save the data into a new row in excel
        if (kontrol == 0)
        {
            if (txt_LRU != null || txt_LRU!="")
            {

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

            }   

            else if (txt_LRU == null || txt_LRU == "") 
                MessageBox.Show("Please add the LRU number "); 
            }


     //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)
    {

    }
}

此条件表示当您的
txt\u LRU
不为null或不为空时,您将执行一些操作。 当
txt\u LRU
null
时,它不等于空字符串。这种情况满足
条件

 if (txt_LRU != null || txt_LRU!="")
            { 
/*row added*/
     }
正确的条件是
非空和非空

 if (txt_LRU != null && txt_LRU!="")
            { 
/*row added*/
     }

PS:尝试使用
string.IsNullOrWhiteSpace(txt\u LRU)

此条件表示当
txt\u LRU
不为null或不为空时,您将执行操作。 当
txt\u LRU
null
时,它不等于空字符串。这种情况满足
条件

 if (txt_LRU != null || txt_LRU!="")
            { 
/*row added*/
     }
正确的条件是
非空和非空

 if (txt_LRU != null && txt_LRU!="")
            { 
/*row added*/
     }

PS:尝试使用
string.IsNullOrWhiteSpace(txt\u LRU)

此条件表示当
txt\u LRU
不为null或不为空时,您将执行操作。 当
txt\u LRU
null
时,它不等于空字符串。这种情况满足
条件

 if (txt_LRU != null || txt_LRU!="")
            { 
/*row added*/
     }
正确的条件是
非空和非空

 if (txt_LRU != null && txt_LRU!="")
            { 
/*row added*/
     }

PS:尝试使用
string.IsNullOrWhiteSpace(txt\u LRU)

此条件表示当
txt\u LRU
不为null或不为空时,您将执行操作。 当
txt\u LRU
null
时,它不等于空字符串。这种情况满足
条件

 if (txt_LRU != null || txt_LRU!="")
            { 
/*row added*/
     }
正确的条件是
非空和非空

 if (txt_LRU != null && txt_LRU!="")
            { 
/*row added*/
     }

PS:尝试使用
string.IsNullOrWhiteSpace(txt\u LRU)

当您从文本框中读取时,您正在为txt\u LRU赋值。但是,字符串只有在未初始化时才能为null。这就是为什么你会得到真实的答案


请参阅。

当您从文本框中读取时,您正在为txt_LRU赋值。但是,字符串只有在未初始化时才能为null。这就是为什么你会得到真实的答案


请参阅。

当您从文本框中读取时,您正在为txt_LRU赋值。但是,字符串只有在未初始化时才能为null。这就是为什么你会得到真实的答案


请参阅。

当您从文本框中读取时,您正在为txt_LRU赋值。但是,字符串只有在未初始化时才能为null。这就是为什么你会得到真实的答案


有关积分,请参见。

Null
与长度为零的字符串不同。一个简单的检查可以为您提供此信息

上面的代码很简单:

private void button1_Click(object sender, EventArgs e)
{
    string txt_string = this.textBox1.Text.ToString();
    if (txt_string == null)
        MessageBox.Show("Yes");
    else
        MessageBox.Show("No");
}

希望这有帮助。

不是为了积分。

Null
与长度为零的字符串不同。一个简单的检查可以为您提供此信息

上面的代码很简单:

private void button1_Click(object sender, EventArgs e)
{
    string txt_string = this.textBox1.Text.ToString();
    if (txt_string == null)
        MessageBox.Show("Yes");
    else
        MessageBox.Show("No");
}

希望这有帮助。

不是为了积分。

Null
与长度为零的字符串不同。一个简单的检查可以为您提供此信息

上面的代码很简单:

private void button1_Click(object sender, EventArgs e)
{
    string txt_string = this.textBox1.Text.ToString();
    if (txt_string == null)
        MessageBox.Show("Yes");
    else
        MessageBox.Show("No");
}

希望这有帮助。

不是为了积分。

Null
与长度为零的字符串不同。一个简单的检查可以为您提供此信息

上面的代码很简单:

private void button1_Click(object sender, EventArgs e)
{
    string txt_string = this.textBox1.Text.ToString();
    if (txt_string == null)
        MessageBox.Show("Yes");
    else
        MessageBox.Show("No");
}

希望这有帮助。

不客气。。。另外,else条件与IF条件相同,请检查。不客气。。。另外,else条件与IF条件相同,请检查。不客气。。。另外,else条件与IF条件相同,请检查。不客气。。。另外,else条件与IF条件相同,请检查。谢谢,我现在明白了:)谢谢,我现在明白了:)谢谢,我现在明白了:)谢谢,我现在明白了:)谢谢,我现在明白了:)