Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET ADO.NET C#4.0 Excel , +
I would like to extract / retrieve only the COLUMN NAMES OF AN EXCEL sheet using c sharp. For example an excel sheet has data in following structure:-
TITLE   | FIRST NAME | LAST NAME
-----------------------------------
Ms.     | Jeannette  | Higgi
Mr.     | Ian        | Botham
Mr.     | Lawrence   | Bocha
 
Sheet1 (sheet name)

The output should be:-
TITLE
FIRST NAME
LAST NAME
 
Your valuable suggestion is highly appreciated.
 
Thanks in advance.
 
Mohandas.
Posted 22-May-11 22:18pm
Edited 22-May-11 22:32pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Firstly Take ur excel data in dataset or datatable.
Then u can use ColumnName property to extract Columns name like
 
dt.Columns[0].ColumnName
 
Be sure Column name Should be first row of excel.
  Permalink  
v2
Comments
Mohandas_Kulasekaran at 23-May-11 3:30am
   
In real time excel sheet, there are over 60000 rows with 40 columns. I want only the column names without having to waste my system resource taking the excel data to a dataset or datatable.
Ashishmau at 23-May-11 4:07am
   
Its not possible without taking it in dataset or Other way u can use dll like LinqToExcel.dll to retrieve columns name.see the links i mentioned below and download dlls for your use.
Dalek Dave at 23-May-11 4:48am
   
Good answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Dalek Dave at 23-May-11 4:48am
   
Good Links
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try below code snippet:
You need to add "Microsoft Excel 12.0 Object Library" to your project
 
using Microsoft.Office.Interop.Excel;
 
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Microsoft.Office.Interop.Excel.Application app = new ApplicationClass();
Workbook workBook = app.Workbooks.Open(@"your_file_path",
0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Worksheet workSheet = (Worksheet)workBook.Worksheets.get_Item(1);
Range range = workSheet.UsedRange;
for (int i = 1; i <= range.Columns.Count; i++)
{
Console.WriteLine(((Range)range.Cells[2, i]).Value2);
}
Console.ReadLine();
}
}
}
 
Reference: http://csharp.net-informations.com/excel/csharp-read-excel.htm[^]
  Permalink  
Comments
Dalek Dave at 23-May-11 4:48am
   
Excellent suggestion, so long as you know the row the column names are in, but it is a method I would employ.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 275
1 Maciej Los 230
2 Sergey Alexandrovich Kryukov 185
3 Afzaal Ahmad Zeeshan 152
4 ProgramFOX 130
0 OriginalGriff 6,524
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,218
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 23 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100