Click here to Skip to main content
15,175,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I have created a website application to display excel data in gridview using file upload control, but now I want to insert a notepad file in excel sheet and show it in gridview.
I am not getting idea, can u please help me.
this is my code:
public partial class NotepadToExcel : System.Web.UI.Page
{
    static string Path1;
    static string Extension;
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
           Path1 = Path.GetFullPath(FileUpload1.PostedFile.FileName);
             Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);   
   FileUpload1.SaveAs(Path1);
            Import_To_Grid(Path1, Extension, rbHDR.SelectedItem.Text);
        }

    }
    private void Import_To_Grid(string Path1, string Extension, string isHDR)
    {
        string conStr = "";
        switch (Extension)
        {
            case ".xls": //Excel 97-03
                conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                break;
            case ".xlsx": //Excel 07
                conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"] .ConnectionString;
                                break;
                        }
        conStr = String.Format(conStr, Path1, isHDR);
        OleDbConnection connExcel = new OleDbConnection(conStr);
        OleDbCommand cmdExcel = new OleDbCommand();
        OleDbDataAdapter oda = new OleDbDataAdapter();
        DataTable dt = new DataTable();
        cmdExcel.Connection = connExcel;

        //Get the name of First Sheet
        connExcel.Open();
        DataTable dtExcelSchema;
        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
        connExcel.Close();

        //Read Data from First Sheet
        connExcel.Open();
        cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
        oda.SelectCommand = cmdExcel;
        oda.Fill(dt);
        connExcel.Close();

        //Bind Data to GridView
        GridView1.Caption = Path.GetFileName(Path1);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
         Import_To_Grid(Path1, Extension, rbHDR.SelectedItem.Text);
        GridView1.PageIndex = e.NewPageIndex;
        
        GridView1.DataBind();
    }
Posted
Updated 14-Sep-10 21:35pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900