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

时间:2016-12-14 12:13:58

标签: c# asp.net vba interop

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

enter image description here

enter image description here

我的代码是休闲

            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();

1 个答案:

答案 0 :(得分:1)

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

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();