使用C#以编程方式获取最后填充的excel行

Sof*_*ant 25 c# excel

我试图使用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)

  • 这个答案为您提供工作表的UsedRange中的最后一个单元格.但问题是,UsedRange永远不会收缩.如果你有一个A1和AA100的值,然后删除AA100的内容,UsedRange仍然是A1:AA100.AA100仍将被视为最后一个细胞. (7认同)
  • @CtrlDot 是一个非常有效的观点, UsedRange 无法按预期工作,并且 xlCellTypeLastCell 方法也不可信。 (3认同)
  • 不确定第一个选项如何工作......但第二个选项看起来很有趣.如何将其转换为int值? (2认同)

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)

[编辑]类似的主题:

  • 方法1有效。方法 2 - 不可靠的已删除的已使用单元格被 UsedRange 和“xlCellTypeLastCell”“标记”为“脏”,并且仍然认为这些单元格仍在使用中并将空单元格报告为“已使用” (2认同)

Sof*_*ant 13

Pryank的回答是最接近我的.我在末尾添加了一点(.Row)所以我不仅仅是返回一个range,而是一个integer.

int lastRow = wkSheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row;
Run Code Online (Sandbox Code Playgroud)

  • 不可靠的已删除的已使用单元格被 UsedRange 和“xlCellTypeLastCell”“标记”为“脏”,并且仍然认为这些单元格仍在使用中并将空单元格报告为“已使用” (3认同)

Joe*_*Joe 9

对于涉及Excel对象模型的问题,首先在VBA中尝试它更容易,然后转换为C#是相当简单的.

在这种情况下,在VBA中执行此操作的一种方法是:

Worksheet.UsedRange.Row + Worksheet.UsedRange.Rows.Count - 1
Run Code Online (Sandbox Code Playgroud)


Ger*_*ell 8

我可以让它在所有场景中工作的唯一方法(受保护的工作表除外):

它支持:

  • 扫描隐藏的行/列

  • 忽略没有数据/公式的格式化单元格

码:

// 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)

  • 出色的答案(也是唯一对我有用的答案)。 (2认同)