如何删除F1,F2 ...在Excel数据的数据视图中? [英] How I can remove the F1, F2 ... In datagridview of excel data?

查看:255
本文介绍了如何删除F1,F2 ...在Excel数据的数据视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目要求我使用c#读取excel数据。当我使用gridview显示工作表时,我发现显示屏还包括F1,F2,...作为每列的标题。我如何删除F1,F2,...?我希望每列的标题与excel文件中的标题相同。例如,文件大小,音频和...



我尝试过:



这是我的连接代码:

public DataTable ReadExcel(string fileName,string fileExt)

{

string conn = string.Empty;

DataTable dtexcel = new DataTable();

if(fileExt.CompareTo(。xls)== 0)

conn = @provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ fileName +; Extended Properties ='Excel 8.0; HRD = Yes; IMEX = 1';; //对于下面的excel 2007

else

conn = @Provider = Microsoft.ACE.OLEDB.12.0; Data Source =+ fileName +; Extended Properties =' Excel 12.0; HDR = NO';; // for above excel 2007

使用(OleDbConnection con = new OleDbConnection(conn))

{

try

{

OleDbDataAdapter oleAdpt = new OleDbDataAdapter(select * from [Files Audio $],con); //这里我们从sheet1读取数据

oleAdpt.Fill(dtexcel); //将excel数据填入dataTable

}

catch {}

}

返回dtexcel;

}

My project requires me to read excel data using c#. When i display the worksheet using the gridview, i found that the display also include F1, F2, ... as the header for every column. How i can remove the F1, F2, ...? I want the header of every column is the same as in excel file. Such as, File size, Audio and ...

What I have tried:

Here is my code for connection:
public DataTable ReadExcel(string fileName, string fileExt)
{
string conn = string.Empty;
DataTable dtexcel = new DataTable();
if (fileExt.CompareTo(".xls") == 0)
conn = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';"; //for below excel 2007
else
conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=NO';"; //for above excel 2007
using (OleDbConnection con = new OleDbConnection(conn))
{
try
{
OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Files Audio$]", con); //here we read data from sheet1
oleAdpt.Fill(dtexcel); //fill excel data into dataTable
}
catch { }
}
return dtexcel;
}

推荐答案

,con); //这里我们从sheet1读取数据

oleAdpt.Fill(dtexcel) ; //将excel数据填入dataTable

}

catch {}

}

返回dtexcel;

}
", con); //here we read data from sheet1
oleAdpt.Fill(dtexcel); //fill excel data into dataTable
}
catch { }
}
return dtexcel;
}


在连接字符串的扩展属性中指定 HDR = YES ,如使用MS Excel(xls / xlsx)使用MDAC和Oledb [ ^ ]。
Specify HDR=YES in the extended properties of the connection string, as explained in Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^].


这篇关于如何删除F1,F2 ...在Excel数据的数据视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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