仅查找Excel工作表列名称 [英] Find only the excel sheet column names

查看:119
本文介绍了仅查找Excel工作表列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想使用c Sharp提取/检索EXCEL工作表的列名.例如,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



非常感谢您的宝贵建议.

提前谢谢.

Mohandas.



Your valuable suggestion is highly appreciated.

Thanks in advance.

Mohandas.

推荐答案

首先从数据集或数据表中获取excel数据.
然后,您可以使用ColumnName属性提取列名称,例如

Firstly Take ur excel data in dataset or datatable.
Then u can use ColumnName property to extract Columns name like

dt.Columns[0].ColumnName



确保列名应该是excel的第一行.



Be sure Column name Should be first row of excel.


请参阅此链接

http://www.dotnetexpertsforum.com/getting-column-names-from- excel-sheet-t1973.html [ ^ ]

http://code.google.com/p/linqtoexcel/wiki/UsingLinqToExcel [ ^ ]
see this links

http://www.dotnetexpertsforum.com/getting-column-names-from-excel-sheet-t1973.html[^]

http://code.google.com/p/linqtoexcel/wiki/UsingLinqToExcel[^]


尝试以下代码段:
您需要将"Microsoft Excel 12.0对象库"添加到您的项目中

使用Microsoft.Office.Interop.Excel;

命名空间ConsoleApplication1
{
课程计划
{
静态void Main(string [] args)
{
Microsoft.Office.Interop.Excel.Application app = new ApplicationClass();
工作簿workBook = app.Workbooks.Open(@"your_file_path",
0,true,5,",",true,XlPlatform.xlWindows,"\ t",false,false,0,true,1,0);
工作表workSheet =(工作表)workBook.Worksheets.get_Item(1);
范围范围= workSheet.UsedRange;
for(int i = 1; i< = range.Columns.Count; i ++)
{
Console.WriteLine((((Range)range.Cells [2,i]).Value2);
}
Console.ReadLine();
}
}
}

参考: http://csharp.net-informations.com/excel/csharp-read-excel. htm [^ ]
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[^]


这篇关于仅查找Excel工作表列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆