我试图使用Microsoft.interop.Excel库和C#以编程方式获取excel表的最后一行.我想这样做,因为我负责循环遍历excel电子表格的所有记录并对它们执行某种操作.具体来说,我需要最后一行的实际数字,因为我将这个数字放入一个函数中.任何人都知道如何做到这一点?
Pri*_*ank 52
情侣方式,
using Excel = Microsoft.Office.Interop.Excel;
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Application app = excel.Application;
Excel.Range all = app.get_Range("A1:H10", Type.Missing);
Run Code Online (Sandbox Code Playgroud)
要么
Excel.Range last = sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
Excel.Range range = sheet.get_Range("A1", last);
int lastUsedRow = last.Row;
int lastUsedColumn = last.Column;
Run Code Online (Sandbox Code Playgroud)
JMa*_*Max 16
这是Excel中的常见问题.
这是一些C#代码:
// Find the last real row
nInLastRow = oSheet.Cells.Find("*",System.Reflection.Missing.Value,
System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSearchOrder.xlByRows,Excel.XlSearchDirection.xlPrevious, false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Row;
// Find the last real column
nInLastCol = oSheet.Cells.Find("*", System.Reflection.Missing.Value, System.Reflection.Missing.Value,System.Reflection.Missing.Value, Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlPrevious, false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Column;
Run Code Online (Sandbox Code Playgroud)
在这里找到
或使用 SpecialCells
Excel.Range last = sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
Excel.Range range = sheet.get_Range("A1", last);
Run Code Online (Sandbox Code Playgroud)
[编辑]类似的主题:
Sof*_*ant 13
Pryank的回答是最接近我的.我在末尾添加了一点(.Row
)所以我不仅仅是返回一个range
,而是一个integer
.
int lastRow = wkSheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row;
Run Code Online (Sandbox Code Playgroud)
对于涉及Excel对象模型的问题,首先在VBA中尝试它更容易,然后转换为C#是相当简单的.
在这种情况下,在VBA中执行此操作的一种方法是:
Worksheet.UsedRange.Row + Worksheet.UsedRange.Rows.Count - 1
Run Code Online (Sandbox Code Playgroud)
我可以让它在所有场景中工作的唯一方法(受保护的工作表除外):
它支持:
扫描隐藏的行/列
忽略没有数据/公式的格式化单元格
码:
// Unhide All Cells and clear formats
sheet.Columns.ClearFormats();
sheet.Rows.ClearFormats();
// Detect Last used Row - Ignore cells that contains formulas that result in blank values
int lastRowIgnoreFormulas = sheet.Cells.Find(
"*",
System.Reflection.Missing.Value,
InteropExcel.XlFindLookIn.xlValues,
InteropExcel.XlLookAt.xlWhole,
InteropExcel.XlSearchOrder.xlByRows,
InteropExcel.XlSearchDirection.xlPrevious,
false,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value).Row;
// Detect Last Used Column - Ignore cells that contains formulas that result in blank values
int lastColIgnoreFormulas = sheet.Cells.Find(
"*",
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
InteropExcel.XlSearchOrder.xlByColumns,
InteropExcel.XlSearchDirection.xlPrevious,
false,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value).Column;
// Detect Last used Row / Column - Including cells that contains formulas that result in blank values
int lastColIncludeFormulas = sheet.UsedRange.Columns.Count;
int lastColIncludeFormulas = sheet.UsedRange.Rows.Count;
Run Code Online (Sandbox Code Playgroud)