如何在填充ListBox之前检查空数据条目

时间:2017-06-21 22:53:36

标签: c#

private void LoadExcelSheet(string path, int sheet){
    _Application excel = new Excel.Application();
    Workbook wb;
    Worksheet ws;
    string data = "";
    int row = 0;
    int col = 0;

    wb = excel.Workbooks.Open(path);
    ws = wb.Worksheets[sheet];
    listBox1.Items.Clear();

    for (row = 1; row < 10; row++){ 
        data = " ";
        for (col = 1; col < 3; col++) {
            data += ws.Cells[row, col].Value2 + " "; 
        }

       //wanted to filter out empty cells/data and at the same time count
       //number of items in the list... row should stop.. I think!

       if(data == null){
          break;
       }

    listBox1.Items.Add(data);  
}

无论我做什么,if声明似乎都不起作用。如果有人能指出我正确的方向,我将非常感激。

2 个答案:

答案 0 :(得分:1)

像这样使用它:

 if (data.Trim().Length < 1)
  {
      return;
  }

使用return而不是break

答案 1 :(得分:0)

添加这样的条件

Person