如何获取Excel列标题名称 [英] How to get Excel column Header names

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

问题描述

如果我们选择一个Excel工作表,那么我们如何获得Excel列标题名称

if we select an Excel sheet then how do we get the Excel Column Headers name

推荐答案

如果你想获得像A,B,C这样的列,

http://stackoverflow.com/questions/181596/how-to-convert-a-column-number-eg-127-into-an-excel-column-eg-aa [ ^ ]



但是在不同的标准中,首先是也将代表列标题。试试这个 http://stackoverflow.com/questions/3855101/read- column-names-from-excel-file-in-c [ ^ ]和 http://codehill.com/2009/01/reading-excel-2003-and-2007-files-using-oledb/ [ ^ ]
If you are meant to get Columns like A,B,C,etc.
http://stackoverflow.com/questions/181596/how-to-convert-a-column-number-eg-127-into-an-excel-column-eg-aa[^]

But in a different standard, first Row will represent Column Header also. Try this http://stackoverflow.com/questions/3855101/read-column-names-from-excel-file-in-c[^] and http://codehill.com/2009/01/reading-excel-2003-and-2007-files-using-oledb/[^]


public static Dictionary<string,int> GetColumnMapping(Excel.Worksheet ExcelSheet, int HeaderRowIndex , List<string> Headers, Dictionary<string,int> HeaderMapping)
        {
            if (HeaderMapping != null)
                HeaderMapping.Clear();

            object misValue = System.Reflection.Missing.Value;

            Excel.Range HeaderRange = ExcelSheet.Rows[HeaderRowIndex , misValue];
            Excel.Range currentFind = null;

            foreach (string Header in Headers)
            {
                currentFind = HeaderRange.Find(Header, misValue, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlNext, false, misValue, misValue);
                if(currentFind != null)
                    HeaderMapping.Add(Header, currentFind.Column);
            }

            return HeaderMapping;
        }


这篇关于如何获取Excel列标题名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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