2016-12-14 102 views
0

我希望能夠找到包含文本的最後一個單元格。目前我正在使用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(); 
+0

http://www.cpearson.com/excel/LastCell.aspx – Vityata

回答

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

這完美的作品。謝謝 –

+0

有沒有什麼辦法可以刪除最後一個單元格後面的文本,以刪除它的單元格樣式? –

+0

你可以使用最後一個帶有文本的單元格的行號/列號和上一次使用的範圍單元格來刪除所有單元格之間的格式? (希望已經足夠清楚) – NavkarJ

相關問題