SoFunction
Updated on 2025-03-08

How to get the content of an Excel table in the page

How to get the content of the Excel table on the page, the specific content is as follows:

First reference the component and namespace

using ;
 using ;

Then upload excel to the specified path

Upload file method omitted

Finally, turn the uploaded excel into Dataset  (copy the following method to use)

public DataSet seachExcel(string str) //Path with parameter excel  {
    OleDbDataAdapter da = new OleDbDataAdapter();
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    string NameTable = "";
    string ConText = "";
    try
    {
      //Get Excel path      FileInfo info = new FileInfo(str);
      //Get the file extension      string fileExt = ;
      //Judge which connection method to use      if (fileExt .ToLower() ==".xls")
      {
        ConText = "Provider=.4.0;Data Source=" + str + ";Extended Properties='excel 8.0;hdr=no;IMEX=1';Persist Security Info=false";
      }
      else if (() == ".xlsx")
      {
        ConText = "Provider=.12.0; Data Source=" + str + ";Extended Properties='excel 12.0 Xml;hdr=no;IMEX=1';Persist Security Info=False";
      }
      //Connect Excel      OleDbConnection conn = new OleDbConnection(ConText);
     //Open excel      ();
      dt=(,null );
        if(dt!=null &&  .Count >0)
        {
          //Get the table name of the sheet1 form          NameTable = [0]["TABLE_NAME"].ToString();
          //Get the table name of the sheet2 form          //NameTable = [1]["TABLE_NAME"].ToString();
        }
        string sql = "select * from [" + NameTable + "]";
        da = new OleDbDataAdapter(sql, conn);
        try
        {
          (ds,NameTable); //Fill the data into Dataset        }  
        catch
        { }
        ();
    }
    catch
    {
    }
    return ds; //Reverse Dataset}

How to read excel table data

In fact, reading data in Excel tables is very similar to reading data in databases, because to a certain extent, Excel tables can be regarded as data tables one by one. The main difference between the two is that the data engines used are different.

In the program in this article, the following code is used to read Excel table data, as follows:

string strDataPathPhy = "c://";
string strCon = " Provider = .4.0 ; Data Source = " + strDataPathPhy + ";Extended Properties=Excel 8.0";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [Sheet1$]";
();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
DataTable dtData = new DataTable();
(dtData);
();

Since you can look at Excel on the left database and the worksheet inside can look at each database table on the left, you can also filter the search results, for example:

Copy the codeThe code is as follows:

strCom = " SELECT * FROM [Sheet1$] WHERE column1 <> '' ";

In this way, the data in dtData is all the data in the [Sheet1$] table that column1 is not empty

The above is the content of how to obtain Excel tables in the introduction page of this article. I hope it will be helpful to everyone.