在C#中将Excel文件的数据填充到DataSet中的问题。

将已经存在的Excel文档路径输入到txtFileName文本框中,建立数据连接。
connection连接无问题,能否使用查询语句将文档当作一个表来处理,然后
直接填充到DataSet中?下例尝试用DataGrid列出DataSet中的Excel文档内容。

string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
+"Data Source=" + this.txtFileName.Text + ";"
+"Extended Properties="Excel 8.0";";
try
{
OleDbConnection cn = new OleDbConnection(strConn);
cn.Open();
}
catch
{
MessageBox.Show("Connection to DataBase Error!");
return;
}
try
{
string sql = "select * from sheet1";//此处出现问题。
OleDbDataAdapter da = new OleDbDataAdapter(sql,cn);
DataSet ds = new DataSet();
da.Fill(ds,"testTable");
this.DataGrid1.DataSource = ds.Tables["testTable"];
}
catch(System.Exception err)
{
MessageBox.Show(err.ToString());
return;
}
cn.Close();

我不清楚查询语句中"select * from ???"
---------------------------------------------------------------

select * from Sheet1$

Published At
Categories with Web编程
Tagged with
comments powered by Disqus