zoukankan      html  css  js  c++  java
  • 读远程CSV和显示它在GridView

    标题:读远程CSV和显示它在GridView(Reading a remote CSV and displaying it in a GridView)

    ading or storing it temporaraily. Also, I have included how to read a local CSV and bind it to a GridView.
    本文介绍了如何显示不用下载或储存它temporaraily远程CSV文件,并将其绑定到一个GridView。此外,我亦包括如何阅读本地的CSV并将其绑定到一个GridView。
    Using the code
    使用代码
    We have to follow just a few steps to achieve it. First, make a web request to the URL of the CSV file. After retrieving the response into a StreamReader, retrieve the first row from the StreamReader and make a table with the column specified in the first row of the CSV file. Retrieve the data row by row and adds the rows to the table. Bind the GridView after adding all the rows to the DataTable. That is it. Here is the code:
    我们必须遵循只需要几个步骤来实现这一目标。首先,网络请求的CSV文件的URL。在检索到一个StreamReader响应,检索从StreamReader第一行,并与在CSV文件的第一行中指定的列的表。行检索数据行并添加到表行。绑定到GridView后加入所有行到DataTable。这是它。以下是代码:

    //to display remote csv file
    protected void btnDispRemote_Click(object sender, EventArgs e)
    {
        try
        {
            System.Net.HttpWebRequest WebRequest;
            string URL = txtRemote.Text.Trim(); //URL of the remote csv
            Uri CSVUri = new Uri(URL);
            WebRequest = (System.Net.HttpWebRequest)
                          System.Net.HttpWebRequest.Create(CSVUri);
            if ((WebRequest.GetResponse().ContentLength > 0))
            {
                System.IO.StreamReader strReader = 
                  new System.IO.StreamReader(
                  WebRequest.GetResponse().GetResponseStream());
                //create datatable with column names
                CreateCSVTable(strReader.ReadLine());
                String SingleLine;
                while ((SingleLine = strReader.ReadLine()) != null)
                {
                    AddRowCSVTable(SingleLine);
                    //adding rows to datatable
                }
                grdList.DataSource = CSVTable;
                grdList.DataBind();
                if (strReader != null) strReader.Close();
            }
        }
        catch (System.Net.WebException ex)
        {
            lblMsg.Text = "File does not exist.";
        }
    }
    

    We have two separate methods for creating the DataTable from the CSV data and to add row wise data to the DataTable. These two methods are as follows:
    我们有两种用于创建从CSV数据DataTable和明智行添加到DataTable的数据分离的方法。这两种方法如下:

    //to create the data table with columns
    public void CreateCSVTable(string TableColumnList)
    {
        CSVTable = new DataTable("CSVTable");
        DataColumn myDataColumn;
        string[] ColumnName = TableColumnList.Split(',');
        for (int i = 0; i < ColumnName.Length - 1; i++)
        {
            myDataColumn = new DataColumn();
            myDataColumn.DataType = 
               Type.GetType("System.String");
            myDataColumn.ColumnName = ColumnName[i];
            CSVTable.Columns.Add(myDataColumn);
        }
    }
    //to add the rows to datatable
    public void AddRowCSVTable(string RowValueList)
    
    {
        string[] RowValue = RowValueList.Split(',');
        DataRow myDataRow;
        myDataRow = CSVTable.NewRow();
      for (int i = 0; i < RowValue.Length - 1; i++)
        {
        myDataRow[i] = RowValue[i];
        }
        CSVTable.Rows.Add(myDataRow);
    }
    
    

    To display a CSV file residing on the system or an application folder, we can easily use simple steps to create an OLEDB connection, command, and adaptor. Sample code is shown below:
    要显示CSV文件驻留在系统或应用程序文件夹,我们可以很容易地用简单的步骤来创建一个OLEDB连接,命令和适配器。示例代码如下所示:

    //to display local csv file
    protected void btnDispLoc_Click(object sender, EventArgs e)
    {
        string filename=fl.PostedFile.FileName.ToString();
        FileInfo file = new FileInfo(filename);
        string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + 
               "Data Source=\"" + file.DirectoryName + 
    "\";Extended Properties='text;HDR=Yes;FMT=Delimited(,)';";
        OleDbConnection con = new OleDbConnection(ConnectionString);
        OleDbCommand cmd = new OleDbCommand(string.Format(
          "SELECT * FROM [" + file.Name + "]", file.Name), con);
        try
        {
            con.Open();
            OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
            DataTable dt = new DataTable("CSVTable");
            oda.Fill(dt);
            grdList.DataSource = dt;
            grdList.DataBind();
        }
        catch (Exception ee)
        {
            lblMsg.Text = ee.Message;
        }
    }

    使用XMLHTTP获取页面数据的方法:

            MSXML2.XMLHTTPClass xmlhttp = new MSXML2.XMLHTTPClass();
            string url = "http://table.finance.yahoo.com/table.csv?s=002468.sz&d=11&e=31&f=2010&g=d&a=11&b=1&c=2010&ignore=.csv";
            xmlhttp.open("GET", url, false, "", "");
            xmlhttp.send("");
            Response.Write(xmlhttp.responseText);
    
  • 相关阅读:
    数据结构总结(UPDATING......)
    课件例题4.11,4.12
    Luogu P1525 关押罪犯
    Luogu P1540 机器翻译
    Luogu P1313 计算系数
    Luogu P1311 选择客栈
    Luogu P1519 穿越栅栏 Overfencing
    Luogu P2863 [USACO06JAN]牛的舞会The Cow Prom
    Tarjan学习笔记
    Luogu P3393 逃离僵尸岛
  • 原文地址:https://www.cnblogs.com/superfeeling/p/1902342.html
Copyright © 2011-2022 走看看