簡體   English   中英

C#擅長如何在使用范圍內查找第一個單元格。

[英]c# excel how to Find First Cell in a used range.

我可以使用XlCellType.xlCellTypeLastCell來找到使用范圍內的最后一個單元格。 如何在一行中獲得第一個單元格?

代碼以獲取最后一個單元格位置。

    Excel.Range mergeCells = (Excel.Range)mergeSheet.Cells[6,1].EntireRow;
    var rowRng = mergeCells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
    var colPosition = rowRng.Column;

一種方法是獲取mergeCells.value並循環循環以增加計數器,直到看到空值/空值。 但是我希望能做到這一點。 有任何想法嗎 ?

測試用例:

(1)

在此處輸入圖片說明

預期結果colPosition = 1

(2) 在此處輸入圖片說明

預期結果colPosition = 5

這是使用Excel Interop庫的解決方案(如問題中所標記)。 下面的方法將返回給定行中第一個單元格的從1開始的列索引。 它適用於您提供的測試用例以及我自己的一些用例。 請注意,如果您只想使用已使用范圍中的第一行-而不是提供的行,則可以使用ActiveSheet.UsedRange.Rows[1].Row查找第一個已使用的行號。

    public static int FindFirstCellInExcelRow(string filePath, int rowNum)
    {
        Excel.Application xlApp = null;
        Excel.Workbook wkBook = null;
        Excel.Worksheet wkSheet = null;
        Excel.Range range = null;
        try
        {
            xlApp = new Excel.Application();
            wkBook = xlApp.Workbooks.Open(filePath);
            wkSheet = wkBook.ActiveSheet;
            range = wkSheet.Cells[rowNum, 1].EntireRow;
            if (range.Cells[1, 1].Value != null)
            {
                return range.Cells[1, 1].Column;
            }
            var result = range.Find(What: "*", After: range.Cells[1, 1], LookAt: Excel.XlLookAt.xlPart, LookIn: Excel.XlFindLookIn.xlValues, SearchOrder: Excel.XlSearchOrder.xlByColumns, SearchDirection: Excel.XlSearchDirection.xlNext, MatchByte: false, MatchCase: false);
            int colIdx = result?.Column ?? 0; // return 0 if no cell in row contains value
            return colIdx;
        }
        finally
        {
            wkBook.Close();
            Marshal.ReleaseComObject(xlApp);
            Marshal.ReleaseComObject(wkBook);
            Marshal.ReleaseComObject(wkSheet);
            Marshal.ReleaseComObject(range);
            xlApp = null;
            wkBook = null;
            wkSheet = null;
            range = null;
        }
    }

我強烈(x10)建議在Microsoft的Excel庫上使用ClosedXML(除非您使用的是舊的xls文件)。 使用ClosedXML,您將執行以下操作(此操作直接從他們的網頁上進行):

立即從NuGet軟件包中獲取它。 Install-Package ClosedXML -Version 0.93.1

https://github.com/ClosedXML/ClosedXML/wiki/Finding-and-extracting-the-data

  var wb = new XLWorkbook(northwinddataXlsx);
  var ws = wb.Worksheet("Data");

  // Look for the first row used
  var firstRowUsed = ws.FirstRowUsed();

  // Narrow down the row so that it only includes the used part
  var categoryRow = firstRowUsed.RowUsed();

  // Move to the next row (it now has the titles)
  categoryRow = categoryRow.RowBelow();

  // Get all categories
  while (!categoryRow.Cell(coCategoryId).IsEmpty())
  {
    String categoryName = categoryRow.Cell(coCategoryName).GetString();
    categories.Add(categoryName);

    categoryRow = categoryRow.RowBelow();
  }

嘗試下面的代碼片段,這將給出excel使用范圍的第一行

Excel.Workbook xlWB = Globals.ThisAddIn.Application.ActiveWorkbook;
Excel.Worksheet xlWS = xlWB.ActiveSheet;
int firstRow = xlWS.UsedRange.Row;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM