读取Excel数据行,用C#.net线数据、Excel、net

2023-09-04 00:33:24 作者:告诉自己忘了他

有谁知道我怎么能读通过C#.NET行Excel数据线。

我发现这个code将返回在C#.NET的grindview从Excel和显示数据。这将如何每次,我只是徘徊可能读取一行数据线在服务器端呢?

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;

使用System.Data这;
使用System.Data.OleDb;
使用System.IO;


命名空间的网站
{
    公共部分类pgTest:System.Web.UI.Page
    {
        保护无效的Page_Load(对象发件人,EventArgs的)
        {

        }


        保护无效btnImport_Click(对象发件人,EventArgs的)
        {
            字符串CONNSTRING =;
            字符串strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
            字符串路径= fileuploadExcel.PostedFile.FileName;
            //连接字符串到Excel工作簿
            如果(strFileType.Trim()==的.xl​​s)
            {
                CONNSTRING =供应商= Microsoft.Jet.OLEDB.4.0;数据源=+路径+;扩展属性= \的Excel 8.0; HDR =是; IMEX = 2 \;
            }
            否则,如果(strFileType.Trim()==的.xl​​sx)
            {
                CONNSTRING =供应商= Microsoft.ACE.OLEDB.12.0;数据源=+路径+;扩展属性= \的Excel 12.0; HDR =是; IMEX = 2 \;
            }


            查询字符串=SELECT [用户名],[年龄],[电话] FROM [工作表Sheet1 $];
            OleDbConnection的康恩=新的OleDbConnection(CONNSTRING);
            如果(conn.State == ConnectionState.Closed)
                conn.Open();
            OleDbCommand的CMD =新的OleDbCommand(查询,康涅狄格州);
            OleDbDataAdapter的DA =新OleDbDataAdapter的(CMD);
            的DataSet ds为新的DataSet();
            da.Fill(DS);
            grvExcelData.DataSource = ds.Tables [0];
            grvExcelData.DataBind();
            da.Dispose();
            conn.Close();
            conn.Dispose();
        }


    }
}
 

解决方案

您可以使用 OleDbDataReader 如下

 使用(OleDbConnection的连接=新的OleDbConnection(的connectionString))
{
    OleDbCommand的命令=新的OleDbCommand(查询字符串,连接);

    connection.Open();
    OleDbDataReader读者= Command.ExecuteReader却();

    而(reader.Read())
    {
        变种val1中=读者[0]的ToString();
    }
    reader.Close();
}
 
如何应用C 读取excel中的数据

Does anyone know how can I read excel data line by line with c# .net.

I found this code which will return the data from excel and display on the grindview in c# .net. How every, I just wandering will this be possible to read the data line by line on the server side instead?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.OleDb;
using System.IO;


namespace site
{
    public partial class pgTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void btnImport_Click(object sender, EventArgs e)
        {
            string connString = "";
            string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
            string path = fileuploadExcel.PostedFile.FileName;
            //Connection String to Excel Workbook
            if (strFileType.Trim() == ".xls")
            {
                connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (strFileType.Trim() == ".xlsx")
            {
                connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }


            string query = "SELECT [username],[age],[phone] FROM [Sheet1$]";
            OleDbConnection conn = new OleDbConnection(connString);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            OleDbCommand cmd = new OleDbCommand(query, conn);
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            grvExcelData.DataSource = ds.Tables[0];
            grvExcelData.DataBind();
            da.Dispose();
            conn.Close();
            conn.Dispose();
        }


    }
}

解决方案

you can use OleDbDataReader as below

using (OleDbConnection connection = new OleDbConnection(connectionString))
{
    OleDbCommand command = new OleDbCommand(queryString, connection);

    connection.Open();
    OleDbDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        var val1= reader[0].ToString();
    }
    reader.Close();
}