问题描述
我的项目要求我使用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;
}
推荐答案
这篇关于如何删除F1,F2 ...在Excel数据的数据视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!