使用asp.net从excel表读取单元格 [英] reading cell from excel sheet using asp.net

查看:203
本文介绍了使用asp.net从excel表读取单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
$ b $我试图在我的asp.net中读取excel表格单元格这里是代码。



Hi!
i am trying to read excel sheet cell in my asp.net here is the code.

string file = Server.MapPath("Model.xls");
            string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties=Excel 8.0;";
            OleDbConnection objConn = new OleDbConnection(connStr);
            objConn.Open();
 DataSet ds;
            OleDbDataAdapter MyCommand;
            
            MyCommand = new OleDbDataAdapter("select * from [Sheet1$B6:B6]", objConn);
            ds = new System.Data.DataSet();
            MyCommand.Fill(ds);
            Label1.Text = (MyCommand.Fill(ds)).ToString();





但它没有显示价值来自那个牢房。有人可以告诉我有什么不对...

提前谢谢



but it does not show value from that cell. can somebody please tell me what wrong...
Thanks in advance

推荐答案

B6:B6],objConn) ;
ds = new System.Data.DataSet();
MyCommand.Fill(ds);
Label1.Text =( MyCommand.Fill(ds))。ToString();
B6:B6]", objConn); ds = new System.Data.DataSet(); MyCommand.Fill(ds); Label1.Text = (MyCommand.Fill(ds)).ToString();





但它没有显示该单元格的值。有人可以告诉我错误...

提前谢谢



but it does not show value from that cell. can somebody please tell me what wrong...
Thanks in advance


数据集就像一个内存数据库。这可以包含一个或多个表。获取检索到的值从你的excel文件中,你应该进入它的表并访问那里的单元格。



由于你的select查询只返回excel文件中的一个单元格,你可能以这种方式从数据集中访问值。

A dataset is like a in-memory database. This can contain one or more tables. To get the retrieved value from your excel file, you should go inside its table and access the cell there.

Since your select query there returns only one cell from the excel file, you can probably access the value from the dataset this way.
Label1.Text = ds.Tables[0].Rows[0][0].ToString(); //On the [0][0] part, the first index refers to the row index while the second refers to the column index





更新:



似乎它将值视为标题列。我尝试了你的代码并对连接字符串进行了一些修改,只添加了HDR = NO。您可以尝试下面的代码。





UPDATE:

It seems that it's treating the value as a header column. I tried your code and made some modifications on the connection string, just added HDR=NO. You may try the code below on your end.

string connStr = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=NO;""", file);


这篇关于使用asp.net从excel表读取单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆