读取 Excel 表格时出现问题

发布于 2024-09-28 10:19:07 字数 729 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身情人 2024-10-05 10:19:07

您好,请发布完整的代码,或者您可以使用以下方法将完整的工作表导入到 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;
       }
   }

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

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;
       }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文