繁体   English   中英

使用Excel Interop对行中使用的单元格进行计数

[英]Count used cells in row using Excel Interop

我想在工作表的每一行中获取最后使用的单元格的索引。 我可以得到整个工作表中的最后一个使用的列,但是我有一个表,其中每行都有不同的使用单元格,如下所示: excell表样本

我尝试了这个:

var excelApp = new Excel.Application();
var workBook = excelApp.Workbooks.Open(excelFilePath);
Excel._Worksheet worksheet = workBook.Worksheets[worksheetName];

int rowsCount = worksheet.UsedRange.Rows.Count;
for (int i = 1; i <= rowsCount; i++)
{               
    Excel.Range range = worksheet.Rows[i];
    int lastColumn = range.Columns.Count;
    Console.WriteLine(lastColumn);         
}

我期望输出:

3
5
2
7

但实际输出是:

16384
16384
16384
16384

我使用Excel Interop库。 任何建议,将不胜感激。 谢谢!

您可以使用如下形式:

var usedRange = worksheet.UsedRange;
int startRow = usedRange.Row;
int endRow = startRow + usedRange.Rows.Count - 1;
int startColumn = usedRange.Column;
int endColumn = startColumn + usedRange.Columns.Count - 1;
for (int row = startRow; row <= endRow; row++)
{
    Excel.Range lastCell = worksheet.Cells[row, endColumn];
    if (lastCell.Value2 == null)
        lastCell = lastCell.End[Excel.XlDirection.xlToLeft];
    var lastColumn = lastCell.Column;
    Console.WriteLine($"{row}: {lastColumn}");
}

基本上,技巧是获取行中的最后一个单元格,如果它为空, Range.End属性(或方法?)与XlDirection.xlToLeft一起XlDirection.xlToLeft (需要空检查,因为似乎起始单元格已从End调用中排除) 。

下面显示了获取一行最后使用的单元格的逻辑。 它不会在所有行中循环,但是您应该可以将其用于for迭代器。

using System;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.IO;

namespace Example 
{
    public class ExcelUsed
    {
        /// <summary>
        /// Get last used column for a row
        /// </summary>
        /// <param name="fileName">Excel file to read</param>
        /// <param name="sheetName">Sheet to work on</param>
        /// <param name="row">Row in sheet to get last used column</param>
        /// <returns></returns>
        public int LastColumnForRow(string fileName, string sheetName, int row)
        {
            int lastColumn = -1;

            if (File.Exists(fileName))
            {
                Excel.Application xlApp = null;
                Excel.Workbooks xlWorkBooks = null;
                Excel.Workbook xlWorkBook = null;
                Excel.Worksheet xlWorkSheet = null;
                Excel.Sheets xlWorkSheets = null;

                xlApp = new Excel.Application();
                xlApp.DisplayAlerts = false;

                xlWorkBooks = xlApp.Workbooks;
                xlWorkBook = xlWorkBooks.Open(fileName);

                xlApp.Visible = false;

                xlWorkSheets = xlWorkBook.Sheets;

                for (int x = 1; x <= xlWorkSheets.Count; x++)
                {
                    xlWorkSheet = (Excel.Worksheet)xlWorkSheets[x];


                    if (xlWorkSheet.Name == sheetName)
                    {
                        Excel.Range xlCells = null;
                        xlCells = xlWorkSheet.Cells;

                        Excel.Range workRange = xlCells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell);
                        Excel.Range xlColumns = xlWorkSheet.Columns;

                        int count = xlColumns.Count;

                        Marshal.FinalReleaseComObject(xlColumns);
                        xlColumns = null;

                        Excel.Range xlLastRange = (Excel.Range)xlWorkSheet.Cells[row, count];
                        Excel.Range xlDirRange = xlLastRange.End[Excel.XlDirection.xlToLeft];

                        Marshal.FinalReleaseComObject(xlLastRange);
                        xlLastRange = null;

                        lastColumn = xlDirRange.Column;
                        Marshal.FinalReleaseComObject(xlDirRange);
                        xlDirRange = null;

                        Marshal.FinalReleaseComObject(workRange);
                        workRange = null;

                        Marshal.FinalReleaseComObject(xlCells);
                        xlCells = null;

                        break;
                    }

                    Marshal.FinalReleaseComObject(xlWorkSheet);
                    xlWorkSheet = null;

                }

                xlWorkBook.Close();
                xlApp.UserControl = true;
                xlApp.Quit();

                Release(xlWorkSheets);
                Release(xlWorkSheet);
                Release(xlWorkBook);
                Release(xlWorkBooks);
                Release(xlApp);

                return lastColumn;

            }
            else
            {
                throw new Exception("'" + fileName + "' not found.");
            }
        }
         /// <summary>
        /// 
        /// </summary>
        public void CallGarbageCollector()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        /// <summary>
        /// Method to release object used in Excel operations
        /// </summary>
        /// <param name="sender"></param>
        private void Release(object sender)
        {
            try
            {
                if (sender != null)
                {
                    Marshal.ReleaseComObject(sender);
                    sender = null;
                }
            }
            catch (Exception)
            {
                sender = null;
            }
        }
    }
}

int row = 1;
int results = eu.LastColumnForRow(fileName, sheetName,row);
MessageBox.Show($"Row {row}: {results}");

在这里尝试一下https://code.msdn.microsoft.com/Excel-get-last-row-and-fe764cfc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM