将CSV文件导入到Gridview中 [英] Import csv file into a Gridview
问题描述
我正在尝试读取excel.csv文件并填充网格,但是我正在获取
尝试填充时指定未指定错误"的异常
数据适配器.关于什么地方有什么建议吗?
代码段....
字符串strPath = @"\\ clw-Grouper \ web_data \ RFI";
String connString = string.Format("Provider = Microsoft.Jet.OLEDB.4.0; Data Source =''{0}''; Extended Properties ="''Text'';,strPath);
试试
{
字符串cmdString = string.Format("SELECT * FROM {0}","excel.csv");
OleDbDataAdapter dataAdapter =新的OleDbDataAdapter(cmdString,connString);
DataSet dataSet =新的DataSet();
dataAdapter.Fill(dataSet); ExcelImportGrid.DataSource = dataSet;
ExcelImportGrid.DataBind();
ExcelImportGrid.Caption = Path.GetFileName(strFileName);
Application ["GridTable"] = dataSet;
}
catch(ex ex例外)
{
MessageLabel.Text = ex.Message;
}
I''m trying to read in a excel.csv file and populate a grid but I''m getting
an exception which specifies "Unspecifed Error" when trying to fill
the data adapter. Any suggestion as to what''s wrong?
code snippet....
String strPath = @"\\clw-Grouper\web_data\RFI";
String connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=''{0}'';Extended Properties=''Text'';", strPath);
try
{
string cmdString = string.Format("SELECT * FROM {0}","excel.csv");
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdString, connString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet); ExcelImportGrid.DataSource = dataSet;
ExcelImportGrid.DataBind();
ExcelImportGrid.Caption = Path.GetFileName(strFileName);
Application["GridTable"] = dataSet;
}
catch (Exception ex)
{
MessageLabel.Text = ex.Message;
}
推荐答案
您使用的是Excel连接器,而不是csv连接器.
更好的尝试:
You are using an excel connector not a csv connector.
Better try:
select TerritoryID
,TotalSales
,TotalCost
from openrowset('MSDASQL'
,'Driver={Microsoft Access Text Driver (*.txt, *.csv)}'
,'select * from C:\myfileName.CSV')
more info can be found here[^].
这篇关于将CSV文件导入到Gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!