Wednesday, 19 December 2012

Showing CSV/Excel Data in Gridview

Showing CSV/Excel Data in Gridview


Design Part:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>Auto Complete Example 1</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="GridView1" runat="server">

        </asp:GridView>

    </div>

    </form>

</body>

</html>

Code-Behind:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Configuration;

using System.Web.Services;

using System.Collections;

public partial class Default3 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        System.Data.Odbc.OdbcConnection Conn = null;

        System.Data.DataTable dt = new System.Data.DataTable();

        System.Data.Odbc.OdbcDataAdapter da = null;

        string strConnstr = null;

        string strImportfolder = null;

        string strFilename = null;

        strImportfolder = "E:\\rakesh\\temp\\";

        //strFilename = "test.csv";

        strFilename = "file.csv";

        //this is the csv file to be imported

        strConnstr = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + strImportfolder + ";";

        Conn = new System.Data.Odbc.OdbcConnection(strConnstr);

        da = new System.Data.Odbc.OdbcDataAdapter("select * from [" + strFilename + "]", Conn);

        da.Fill(dt);

        GridView1.DataSource = dt;

        GridView1.DataBind();

    }

}
 

1 comment:

  1. Really a gud website....!!!
    very useful...

    ReplyDelete

Thank You for Your Comments. We will get back to you soon.

back to top