如何在 elseif 语句中显示消息框?

How to show messagebox in elseif statement?

我对 messagebox 有疑问。

表格 1:

using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)

            {
                Class1 excel = new Class1(dialog.FileName, 1);
                string path = dialog.FileName + ".txt";
                TextWriter tw = new StreamWriter(path, true);

                tw.Write(excel.ReadCell(0, 0));
                tw.Close();
            }
        }
    }
}

第 1 类:

using Microsoft.Office.Interop.Excel;
using _Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    class Class1
    {
        string path = "";
        _Application excel = new _Excel.Application();
        Workbook wb;
        Worksheet ws;
        private object excelWorksheet;

        public Class1(string path, int Sheet)
        {
            this.path = path;
            wb = excel.Workbooks.Open(path);
            ws = (Worksheet)wb.Worksheets[Sheet];
        }
        public string ReadCell(int row, int column)
        {
            column += 1;  
            do
            {
                row++;
                if(((_Excel.Range)ws.Cells[row, column]).Value2 == null)
                
                    break;
                _Excel.Range range = (_Excel.Range)ws.Cells[row, column];
                if (range.Value.ToString() == "Z_KomSilnice_L (24200)/7")
                {
                    range = (_Excel.Range)ws.Cells[row, column + 1];
                    return range.Value.ToString();
                }
                else
                {
                    MessageBox.Show("Hodnota nenalezena!");
                }

            } while (true); 

            return ""; 
        }
    }
    }

当我写这样的代码时,总是出现消息框。 但只有当值不在 excel 文件中时,我才需要他出现。

   if (range.Value.ToString() == "Z_KomSilnice_L (24200)/7")
                {
                    range = (_Excel.Range)ws.Cells[row, column + 1];
                    return range.Value.ToString();
                }
                else
                {
                    MessageBox.Show("Hodnota nenalezena!");
                }

问题是这会检查第一个单元格,发现找不到该值并写入一个消息框。 所以我知道问题出在哪里,为什么会出现问题,但我不知道如何组合才能使它起作用。

谢谢大家的指点。

如果我没理解错的话;

如果您已经找到该值,您将返回,而不是在“其他”部分读取每一列时给出相同的消息。

除了do{}while(true)以外的所有数据都找不到的时候给个消息才是正确的。

像这样;

public string ReadCell(int row, int column)
    {
        column += 1;  
        do
        {
            row++;
            if(((_Excel.Range)ws.Cells[row, column]).Value2 == null)
            
                break;
            _Excel.Range range = (_Excel.Range)ws.Cells[row, column];
            if (range.Value.ToString() == "Z_KomSilnice_L (24200)/7")
            {
                range = (_Excel.Range)ws.Cells[row, column + 1];
                return range.Value.ToString();
            }

        } while (true); 
        
        MessageBox.Show("Hodnota nenalezena!");

        return ""; 
    }