【问题标题】:Find last cell with text in it查找最后一个包含文本的单元格
【发布时间】:2017-04-29 18:29:06
【问题描述】:

我希望能够找到包含文本的最后一个单元格。目前我正在使用 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();

【问题讨论】:

标签: c# asp.net vba interop


【解决方案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();

【讨论】:

  • 有什么办法可以删除最后一个单元格之后的所有内容,以删除 J32 的单元格样式?
  • 您可以使用最后一个带有文本的单元格的行/列号和最后一个使用的范围单元格来删除其间所有单元格的格式吗? (希望够清楚)
  • 你介意给我一个小例子来说明如何做到这一点吗?谢谢
  • 我编辑了上面的代码以清除从最后一个包含文本的单元格到最后一个使用的单元格的所有单元格的格式。
  • 这适用于删除包含文本的单元格下方的任何单元格样式,但不会删除它右侧的单元格样式。要这样做吗?
猜你喜欢
  • 1970-01-01
  • 2016-05-30
  • 2022-06-22
  • 1970-01-01
  • 2019-12-12
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
相关资源
最近更新 更多