Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Fri Feb 28, 2020 4:55 pm

Hello Team,

Am new to spire.xls, is it possible to read all the column names (c#) of excel (i.e i need to read first row which contains column names alone) ?

sr243605
 
Posts: 3
Joined: Thu Feb 27, 2020 9:42 am

Mon Mar 02, 2020 2:40 am

Hi,

Thanks for your inquiry.
There is no direct method in Spire to get column names. But you could refer to following code to achieve it:
Code: Select all
        public static void Test()
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"sample.xlsx");
            Worksheet sheet = workbook.Worksheets[0];
            int columnCount = sheet.Columns.Length;

            List<string> ColumnNames = new List<string>();
            for (int i = 1; i <= columnCount; i++)
            {
                string name = GetExcelLetter(i);
                ColumnNames.Add(name);
            }
        }
        private static string GetExcelLetter(int columnNum)
        {
            int num = columnNum;
            int mod = 0;
            string result = String.Empty;
            while (num > 0)
            {
                mod = (num - 1) % 26;
                result = (char)(65 + mod) + result;
                num = (int)((num - mod) / 26);
            }
            return result;
        }


If I misunderstand your meaning, please provide detailed information for further investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Mar 02, 2020 9:02 am

Thanks for response

Below is he explanation

For example, if my excel looks like below, I need to read only first row names to a list like OrderDate, Region etc(all the first row heading names)

OrderDate Region Rep Item Units UnitCost Total
01-06-2018 East Jones Pencil 95 1.99 189.05
1/23/2018 Central Kivell Binder 50 19.99 999.5
02-09-2018 Central Jardine Pencil 36 4.99 179.64

sr243605
 
Posts: 3
Joined: Thu Feb 27, 2020 9:42 am

Mon Mar 02, 2020 9:14 am

Hi,

Thanks for your information.
Code: Select all
Please refer to following code:
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"Sample.xlsx");
            Worksheet sheet = workbook.Worksheets[0];
            CellRange[] crs = sheet.Rows[0].Cells;
            List<string> cells = new List<string>();
            foreach (CellRange cr in crs)
            {
                cells.Add(cr.Value);
            }


If you still have the issue, please provide your Excel file for further investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Mar 06, 2020 8:27 am

Hi,

Hope you are doing well.
Has your issue been resolved? Could you please give us some feedback at your convenience?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.XLS