Click here to Skip to main content
16,021,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to download (open)excel sheet Template (by not incuding in our project) with fields included dynamically eg sno , name , age, then user populate the same excel sheet and upload into database in atable of similar fields.???
Posted

1 solution

Try This

Download Excel File From Server Initially Make one excel with what ever field you want add as a header field then save it into your project folder then download time use below code

context.Response.Redirect("server path for excel folder/generated.xlsx");


Then upload excel file i give below example make it as your way

in aspx page add below code


XML
<table style="padding: 5px; font-size: 11px;">
                                                                            <tbody>
                                                                                <tr>
                                                                                    <td>
                                                                                        <strong>Please Select Excel file ...</strong>
                                                                                    </td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td>
                                                                                        <div style="padding: 8px 5px; border-bottom: 1px solid #ccc;">
                                                                                            <asp:FileUpload ID="txtFilePath" runat="server"></asp:FileUpload>&nbsp;&nbsp;<asp:Button
                                                                                                ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
                                                                                            <br />
                                                                                            <asp:Label ID="lblMessage" runat="server" Visible="False" Font-Bold="True" ForeColor="#009933"></asp:Label>
                                                                                        </div>
                                                                                    </td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td>
                                                                                        &nbsp;</td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td>
                                                                                        <asp:GridView ID="grvExcelData" runat="server">
                                                                                            
                                                                                        </asp:GridView>
                                                                                    </td>
                                                                                </tr>
                                                                            </tbody>
                                                                        </table>



Then aspx.cs page button upload event include this below code

add header file in top of page

C#
using System.IO;
using System.Data.OleDb;

protected void btnUpload_Click(object sender, EventArgs e)
    {
        if ((txtFilePath.HasFile))
        {
            OleDbConnection conn = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();

            OleDbDataAdapter da = new OleDbDataAdapter();
            DataSet ds = new DataSet();
            string query = null;
            string connString = "";
            string strFileName = DateTime.Now.ToString("MM-dd-yyyy_HHmmss");
            string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();

            //Check file type
            if (strFileType == ".xls" || strFileType == ".xlsx")
            {
                txtFilePath.SaveAs(Server.MapPath("Documents folder \\path for your folder\\" + strFileName + strFileType));
            }
            else
            {
                lblMessage.Text = "Only excel files allowed";
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Visible = true;
                return;
            }

            string strNewPath = Server.MapPath("Documents folder \\folder path\\" + strFileName + strFileType);

            //Connection String to Excel Workbook
            if (strFileType.Trim() == ".xls")
            {
                connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
            }
            else if (strFileType.Trim() == ".xlsx")
            {

                connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\"";
            }


            query = "SELECT * FROM [sheet1$]";


            //Create the connection object
            conn = new OleDbConnection(connString);
            //Open connection
            if (conn.State == ConnectionState.Closed) conn.Open();
            //Create the command object
            cmd = new OleDbCommand(query, conn);
            da = new OleDbDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds);

            grvExcelData.DataSource = ds.Tables[0];
            grvExcelData.DataBind();
            con.Open();
            OleDbDataReader odr = cmd.ExecuteReader();

            while (odr.Read())
            {

                //Here we are retrive from xsl sheet data


                Category = odr.GetValue(0).ToString();
                Brand = odr.GetValue(1).ToString();
                Type = odr.GetValue(2).ToString();
                Description = odr.GetValue(3).ToString();

                
                //Here using this method we are inserting the data into the database
                if (Categoryid != "" && Brandid != "")
                {
                    SqlCommand cmdinsert = new SqlCommand("insert into TypeMaster(BrandId,CategoryId,TypeName,TypeDescription,CreatorName,CreateDate)values('" + Categoryid + "','" + Brandid + "','" + Type + "','" + Description + "','VMichael','" + DateTime.Now + "')", con);
                    cmdinsert.ExecuteNonQuery();
                }
            }

        }
        else
        {
            lblMessage.Text = "Please select an excel file first";
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Visible = true;
        }
    }

Hope This will hepl!!! any query Please send your Doubts Let me known
 
Share this answer
 
v3
Comments
Nelek 18-Apr-12 16:35pm    
Added tags to header code

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