SqlDataReader reader;
string r="";
if ((FileUpload1.PostedFile != null)&&(FileUpload1.PostedFile.ContentLength > 0))
{
r = System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);
}
OleDbConnection oconn =
new OleDbConnection
(@"Provider=Microsoft.Jet.OLEDB.4.0;"
+ @"Data Source="+r+";"
+ @"Extended Properties=""Excel 8.0;HDR=Yes;""");
oconn.Open();
OleDbCommand dbcom = new OleDbCommand("SELECT * FROM [Sheet1$]", oconn);
OleDbDataReader dbreader = dbcom.ExecuteReader();
int rni = dbreader.GetOrdinal ("RollNo");
int mki = dbreader.GetOrdinal ("marks");
此处程序仅在项目文件夹中存在Excel工作表时才有效。如果对excel表进行了任何更改,然后再次运行程序,则只提取之前存在的行,而不是稍后添加的行。请帮帮我.....提前谢谢.....
答案 0 :(得分:1)
您好请发布完整代码,或者您可以使用以下方法将完整工作表导入Datatable对象,并检索DataTable所需的列和行
static public DataTable ExecuteOleDataTable(string sql, string oledbconnectionstring)
{
using (OleDbCommand command = new OleDbCommand())
{
command.CommandType = CommandType.Text;
OleDbConnection oledbconnection = new OleDbConnection(oledbconnectionstring);
command.Connection = new OleDbConnection(oledbconnectionstring); ;
command.CommandText = sql;
if (oledbconnection.State == System.Data.ConnectionState.Open)
oledbconnection.Close();
oledbconnection.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(command);
DataTable datatable = new DataTable();
sda.Fill(datatable);
oledbconnection.Close();
return datatable;
}
}