代码之家  ›  专栏  ›  技术社区  ›  ProgSky

c#excel如何查找所用区域中的第一个单元格。

  •  0
  • ProgSky  · 技术社区  · 6 年前

    我可以用 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 循环遍历以增加计数器,直到看到空值。但我希望能把这个放在一条线上。 有什么想法吗?

    测试用例:

    (一)

    enter image description here

    预期结果colPosition=1

    (二) enter image description here

    预期结果colPosition=5

    3 回复  |  直到 6 年前
        1
  •  3
  •   user8061994user8061994    6 年前

    下面是一个使用Excel互操作库的解决方案(如问题中的标记)。下面的方法将返回给定行中第一个单元格的基于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;
            }
        }
    
        2
  •  2
  •   Juls    6 年前

    我强烈建议(x10)使用ClosedXML而不是微软的Excel库(除非你使用的是旧的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();
      }
    
        3
  •  0
  •   LuckyLikey    6 年前

    请尝试下面的代码片段,这将给出excel使用范围的第一行

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