gpt4 book ai didi

c# - 读取excel表的问题

转载 作者:行者123 更新时间:2023-11-30 18:07:26 24 4
gpt4 key购买 nike

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 工作表进行了任何更改,然后程序再次运行,则只会提取之前存在的行,而不是以后添加的行。请帮助我.....提前致谢.....

最佳答案

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

关于c# - 读取excel表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3976896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com