我希望能夠找到包含文本的最後一個單元格。目前我正在使用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();
http://www.cpearson.com/excel/LastCell.aspx – Vityata