简体   繁体   中英

How to count number of columns Excel file in ClosedXML?

I tried to do that using code:

worksheet.Columns.count();

But there is not method count() for Columns

All code is:

 class ExcelReader
    {

        private XLWorkbook workbook;
        private string file;
        private IXLWorksheet worksheet;


        public ExcelReader(string file) {

            this.workbook = new XLWorkbook(file);
        }

        private void ChooseWorksheet(int sheet) {

            this.worksheet = workbook.Worksheet(sheet);
        } 

        public int NumberColumns() {

            return this.worksheet.Columns.Count();
        }
}

Looking at the code from https://github.com/ClosedXML/ClosedXML Count should work using LINQ, since IXLColumns implemments IEnumerable<IXLColumn> IXLColumns: IEnumerable

If you are getting a method missing on Count, means you are missing

using System.Linq;

I was trying to do the same thing. The following worked for me:

worksheet.Columns().Count()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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