查找其中包含文本的最后一个单元格

Find last cell with text in it

我希望能够找到最后一个包含文本的单元格。目前我正在使用 Interop 这可以很好地找到 usedRnage 但是我希望它只找到最后使用的包含文本的单元格。在此示例中,最后使用的单元格是 J32,我希望它只查找包含文本的最后一个值,因此它应该是 A26。

我的代码是休闲的

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;

            string str;


            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\Craig\Desktop\testCell.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


            range = xlWorkSheet.UsedRange;


            // Detect Last used Row / Column - Including cells that contains formulas that result in blank values
           var iTotalColumns = xlWorkSheet.UsedRange.Columns.Count;
           var iTotalRows = xlWorkSheet.UsedRange.Rows.Count;

            //Excel.Range last = xlWorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
            //Excel.Range lastRange = xlWorkSheet.get_Range("A1", last);

            Excel.Application xlCell;

Excel.Range last = xlWorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
            Excel.Range usedRange = xlWorkSheet.get_Range("A1", last);


            int lastUsedRow = last.Row;
            int lastUsedColumn = last.Column;

            xlWorkBook.Close(@"C:\Users\Craig\Desktop\testCell.xlsx", true, null);
            xlApp.Quit();

基于使用范围应包含最后一个包含文本的单元格这一事实,您可以从使用范围中的最后一个单元格循环到第一个单元格(一个循环用于列,一个循环用于行)以找到最后一个工作表上包含文本的单元格。查看以下代码:

Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
Microsoft.Office.Interop.Excel.Range range;

string str;


xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\Admin\Desktop\testCell.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


range = xlWorkSheet.UsedRange;

Microsoft.Office.Interop.Excel.Range last = xlWorkSheet.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing);


int lastUsedRow = last.Row;
int lastUsedColumn = last.Column;

int lastColumn = -1;

// Loop to find the last column containing text
for(int i = lastUsedColumn; i > 0; i--)
{
    if(xlWorkSheet.Application.WorksheetFunction.CountA(xlWorkSheet.Columns[i]) > 0)
    {
        lastColumn = i;
        break;
    }
}

int lastRow = -1;

// Loop to find the last row containing text
for (int i = lastUsedRow; i > 0; i--)
{
    if (xlWorkSheet.Application.WorksheetFunction.CountA(xlWorkSheet.Rows[i]) > 0)
    {
        lastRow = i;
        break;
    }
}

Console.WriteLine("Last row containing text: " + lastRow);
Console.WriteLine("Last column containing text: " + lastColumn);

// Clear formatting of all columns from last cell with text to last cell in used range
// For columns, you will 
xlWorkSheet.Range[xlWorkSheet.Cells[lastRow, lastColumn+1], xlWorkSheet.Cells[lastUsedRow, lastUsedColumn]].ClearFormats();

// Clear formatting of all rows from last cell with text to last cell in used range
xlWorkSheet.Rows[(lastRow+1) + ":" + lastUsedRow].ClearFormats();

Console.ReadLine();

xlWorkBook.Close(true);
xlApp.Quit();