我想以編程方式使用Microsoft.interop.Excel庫和C#獲取Excel表的最後一行。我想這樣做,因爲我負責循環查看Excel電子表格的所有記錄並對它們執行某種操作。具體來說,我需要最後一行的實際編號,因爲我會將此編號放入函數中。任何人有任何想法如何做到這一點?以編程方式獲取最後一個使用C填充excel行#
回答
夫婦方式,
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);
OR
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;
這是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;
here發現
,或者使用SpecialCells
Excel.Range last = sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
Excel.Range range = sheet.get_Range("A1", last);
[編輯]類似的線程:
這是工作,我一直在努力獲得正確的行。 – NWessel
對於涉及Excel對象模型的問題,它往往更容易嘗試一下在VBA,然後再翻譯成C#是相當平凡的。
在這種情況下,一個做在VBA的方法是:
Worksheet.UsedRange.Row + Worksheet.UsedRange.Rows.Count - 1
Pryank's answer是什麼工作對我來說最接近的一次。我在結尾添加了一點點(.Row
),所以我不只是返回range
,而是integer
。
int lastRow = wkSheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row;
唯一的辦法我能得到它在所有情況下工作(除受保護的張):
它支持:
掃描隱藏的行/列
忽略無數據/公式的格式化單元格
代碼:
// 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;
明智的答案(以及唯一爲我工作的)。 –
的ActiveSheet.UsedRange.Value返回[行,列]的2維對象陣列。檢查兩個維度的長度將提供LastRow索引和LastColumn索引。下面的例子使用C#。
Excel.Worksheet activeSheet;
\t \t Excel.Range activeRange;
public virtual object[,] RangeArray {
\t \t \t get { return ActiveRange.Value; }
\t \t }
\t \t public virtual int ColumnCount {
\t \t \t get { return RangeArray.GetLength(1); }
\t \t }
\t \t public virtual int RowCount {
\t \t \t get { return RangeArray.GetLength(0); }
\t \t }
\t \t public virtual int LastRow {
\t \t \t get { return RowCount; }
\t \t }
這個問題時,有可能是空的細胞更是雪上加霜。但即使只填充了一個值,也必須讀取一行。當有很多未填充的單元格時,可能需要一段時間,但如果輸入接近正確,則速度非常快。
我的解決辦法忽略完全空行並返回最長列的行數:
private static int GetLastRow(Worksheet worksheet)
{
int lastUsedRow = 1;
Range range = worksheet.UsedRange;
for (int i = 1; i < range.Columns.Count; i++)
{
int lastRow = range.Rows.Count;
for (int j = range.Rows.Count; j > 0; j--)
{
if (lastUsedRow < lastRow)
{
lastRow = j;
if (!String.IsNullOrWhiteSpace(Convert.ToString((worksheet.Cells[j, i] as Range).Value)))
{
if (lastUsedRow < lastRow)
lastUsedRow = lastRow;
if (lastUsedRow == range.Rows.Count)
return lastUsedRow - 1;
break;
}
}
else
break;
}
}
return lastUsedRow;
}
對於那些誰使用SpecialCells方法,(我不知道其他人),請注意,如果您的最後一個單元格是合併,您將無法使用Range.Row和Range.Column獲取最後一行和列號作爲數字獲取最後一行和一列。 你需要首先取消合併你的範圍,然後再次獲得最後一個單元格。 這花了我很多。
private int[] GetLastRowCol(Ex.Worksheet ws)
{
Ex.Range last = ws.Cells.SpecialCells(Ex.XlCellType.xlCellTypeLastCell, Type.Missing);
bool isMerged = (bool)last.MergeCells;
if (isMerged)
{
last.UnMerge();
last = ws.Cells.SpecialCells(Ex.XlCellType.xlCellTypeLastCell, Type.Missing);
}
return new int[2] { last.Row, last.Column };
}
- 1. 以編程方式在OpenXML中的Excel中填充行
- 2. 如何使用c#獲取Excel單元格填充模式?
- 3. 以編程方式填充的DataGridView
- 4. 如何以編程方式填充DataGridView
- 5. 以編程方式填充水平UIScrollView
- 6. swift - 以編程方式填充STPPaymentCardTextField
- 7. 以編程方式填充DataGridView ComboBoxCell
- 8. 以編程方式取消gridview行編輯模式使用C#
- 9. Android:以編程方式設置textview的一個填充
- 10. 以編程方式填充的UITableView無法填充屏幕
- 11. 以編程方式填充WPF自動填充框
- 12. Excel - 以編程方式獲取圖表中使用的數據
- 13. 如何以編程方式用CoreData行填充NSArrayController?
- 14. 使單個按鈕寬度以編程方式填充父項
- 15. 以編程方式獲取C:\ Users
- 16. 以編程方式填充圖像以進行動畫
- 17. 最快的方式填充一個Db
- 18. 以編程方式獲取字段名以填充表/交叉表查詢
- 19. 編程填充一個StackPanel使用MVVM
- 20. 如何以編程方式使用ImageButton填充網格?
- 21. 以編程方式創建並使用JXA填充NSWindow
- 22. 以編程方式填充使用swift的UISegmentedControl
- 23. 以編程方式使用VB6填充Infopath 2003窗體
- 24. 以編程方式使用android:layout_weight填充TableLayout
- 25. 以編程方式使用選中的選項填充CheckBoxList
- 26. C# - 如何以編程方式填充.net DataGridView?
- 27. 以編程方式使用c填充SSAS數據挖掘結構#
- 28. 如何使用MonoTouch c#以編程方式獲取聯繫人?
- 29. 獲取最後唯一的行Excel中
- 30. 以編程方式禁用/取消註冊C#中的Excel UDF
不知道第一個選項如何工作......但第二個選項看起來很有趣。我怎樣才能把它轉換成int值? – SoftwareSavant
我編輯了我的代碼,我還沒有測試過這個代碼,但希望能給你一個你想要實現的想法。 – Priyank
此答案爲您提供工作表UsedRange中的最後一個單元格。但問題是,UsedRange從不合同。如果你在A1和AA100中有一個值,那麼刪除AA100的內容,UsedRange仍然是A1:AA100。 AA100仍將被視爲最後一個單元格。 – CtrlDot