读取 Excel 表格时出现问题
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 工作表进行任何更改,然后再次运行程序,则仅提取之前存在的行,而不提取后来添加的行。请帮助我......提前致谢......
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");
Here the program works only if the excel sheet is present in the project folder. And if any changes are made to the excel sheet and then the program is run again, then only the rows that were present before are fetched, and not the ones added later. Please help me.....thanks in advance.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您好,请发布完整的代码,或者您可以使用以下方法将完整的工作表导入到 Datatable 对象中,并检索 DataTable 所需的列和行
Hi Please post the complete code, alternatively you can import the complete worksheet to a Datatable object with the below method and retrieve required columns and rows of DataTable