Вопрос

I'm creating my own search and replace function for excel with an extra ribbon. I want to find a certain string in an excel-document (not an existing document, visual studio opens excel automaticly when I run the application). The problem is that he can't find the value we give in an editbox (txtFind) in the spreadsheet. The application gives an error in my loop, when I create a cell-object.

This is the code:

Excel.Application exc = new Excel.Application();
    if (exc == null)
    {
        Console.WriteLine("ERROR: EXCEL couldn't be started!");
    }

    Workbooks workbooks = exc.Workbooks;
    Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
    Sheets sheets = workbook.Worksheets;
    Worksheet worksheet = (Worksheet)sheets.get_Item(1);
    if (worksheet == null)
    {
        Console.WriteLine("ERROR: worksheet == null");
    }
    for (int i = 1; i < 150; i++)
    {
        for (int j = 1; j < 150; j++)
        {
            var cell = (worksheet.Cells[i, j] as Excel.Range).Value;
            if (cell == txtFind.Text.ToString())
            {
                MessageBox.Show("FOUND");
            }
            else
            {
                cell.FindNext();
            }
        }
    }    
Это было полезно?

Решение

Excel.Application exc = new Excel.Application();
    if (exc == null)
    {
        Console.WriteLine("ERROR: EXCEL couldn't be started!");
    }

    Workbooks workbooks = exc.Workbooks;
    Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
    Sheets sheets = workbook.Worksheets;
    Worksheet worksheet = (Worksheet)sheets.get_Item(1);

    if (worksheet == null)
    {
        Console.WriteLine("ERROR: worksheet == null");
    }      
    Excel.Range range = worksheet.UsedRange;
    Excel.Range currentFind; 

    currentFind = range.Cells.Find(txtFind.Text.ToString(), Type.Missing, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole,
                    Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, false, false);
    if (currentFind != null)
    {
        MessageBox.Show("found");
    }
    else
    {
        MessageBox.Show("not found");
    }

This is my new piece of code ... still the currentFind has a null value. When I run the application through Visual Studio, I fill some cells with values I want to find, so I am not checking on an existing file.

Другие советы

  1. You are only searching the first 150 rows and 150 columns. What if there is data in row 151?

  2. Why are you doing a .FindNext? This is only to be called if you are doing a Find() operation. The Find() operation is a better choice for you then manually searching for all rows and columns.

  3. Most likely the reason for your nullrefexception is that the range doesn't exist. If you insist on hard coding the ranges (150x150) then check that the value is not null with an IF statement. You're better off however using the sheet.UsedRange range which will tell you how much of the sheet you need to search through.

  4. The code above will never find the row, as you are adding a new blank sheet and then searching it. A new blank sheet will never contain your value.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top