[英]How to get the range of occupied cells in excel sheet
我使用C#來自動化Excel文件。 我能夠得到工作簿及其包含的工作表。 例如,如果我在sheet1中有兩個列和5行。 我想要o將占用的單元格范圍設為A1:B5。 我嘗試了以下代碼,但未給出正確的結果。 #列和#列要大得多,單元格也為空。
Excel.Range xlRange = excelWorksheet.UsedRange;
int col = xlRange.Columns.Count;
int row = xlRange.Rows.Count;
我還有其他方法可以使用該范圍嗎?
我遇到了一個與您非常相似的問題。 實際起作用的是:
iTotalColumns = xlWorkSheet.UsedRange.Columns.Count;
iTotalRows = xlWorkSheet.UsedRange.Rows.Count;
//These two lines do the magic.
xlWorkSheet.Columns.ClearFormats();
xlWorkSheet.Rows.ClearFormats();
iTotalColumns = xlWorkSheet.UsedRange.Columns.Count;
iTotalRows = xlWorkSheet.UsedRange.Rows.Count;
恕我直言,發生的事情是,當您從Excel中刪除數據時,它一直認為這些單元格中有數據,盡管它們是空白的。 當我清除格式后,它將刪除空白單元格,因此返回實際計數。
Excel.Range last = sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
Excel.Range range = sheet.get_Range("A1", last);
現在,“范圍”將是占用的單元格范圍
請參見Range.SpecialCells方法。 例如,要獲取具有恆定值或公式的單元格,請使用:
_xlWorksheet.UsedRange.SpecialCells(
Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeConstants |
Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeFormulas)
我可以使它在所有情況下(受保護的工作表除外)工作的唯一方法(基於Farham的回答):
它支持:
掃描隱藏的行/列
忽略沒有數據/公式的格式化單元格
碼:
// 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;
dim lastRow as long 'in VBA it's a long
lastrow = wks.range("A65000").end(xlup).row
這兩行本身對我不起作用:
xlWorkSheet.Columns.ClearFormats();
xlWorkSheet.Rows.ClearFormats();
您可以通過單擊工作表中的ctrl + end並查看選擇了哪個單元格來進行測試。
我發現在前兩個之后添加此行可以解決我遇到的所有實例的問題:
Excel.Range xlActiveRange = WorkSheet.UsedRange;
現在有點老問題了,但是如果有人正在尋找解決方案,那么這對我有用。
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);
如果您知道從何處查找范圍,則應嘗試使用currentRegion屬性。 這將為您提供使用范圍的邊界。
這是專門為查找公式而設計的,但是您應該能夠通過更改測試起始單元格的方式將其擴展為常規內容。 您必須處理超出此范圍的單個單元格范圍。
public static Range GetUsedPartOfRange(this Range range)
{
Excel.Range beginCell = range.Cells[1, 1];
Excel.Range endCell = range.Cells[range.Rows.Count, range.Columns.Count];
if (!beginCell.HasFormula)
{
var beginCellRow = range.Find(
"*",
beginCell,
XlFindLookIn.xlFormulas,
XlLookAt.xlPart,
XlSearchOrder.xlByRows,
XlSearchDirection.xlNext,
false);
var beginCellCol = range.Find(
"*",
beginCell,
XlFindLookIn.xlFormulas,
XlLookAt.xlPart,
XlSearchOrder.xlByColumns,
XlSearchDirection.xlNext,
false);
if (null == beginCellRow || null == beginCellCol)
return null;
beginCell = range.Worksheet.Cells[beginCellRow.Row, beginCellCol.Column];
}
if (!endCell.HasFormula)
{
var endCellRow = range.Find(
"*",
endCell,
XlFindLookIn.xlFormulas,
XlLookAt.xlPart,
XlSearchOrder.xlByRows,
XlSearchDirection.xlPrevious,
false);
var endCellCol = range.Find(
"*",
endCell,
XlFindLookIn.xlFormulas,
XlLookAt.xlPart,
XlSearchOrder.xlByColumns,
XlSearchDirection.xlPrevious,
false);
if (null == endCellRow || null == endCellCol)
return null;
endCell = range.Worksheet.Cells[endCellRow.Row, endCellCol.Column];
}
if (null == endCell || null == beginCell)
return null;
Excel.Range finalRng = range.Worksheet.Range[beginCell, endCell];
return finalRng;
}
}
您不應該通過按“刪除”來刪除框中的數據,我認為這就是問題所在,因為excel仍會檢測到該框為“” <-仍然有值,您應該通過右鍵單擊該框並單擊“刪除”來刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.