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
<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> <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>
</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
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();
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);
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$]";
conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed) conn.Open();
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())
{
Category = odr.GetValue(0).ToString();
Brand = odr.GetValue(1).ToString();
Type = odr.GetValue(2).ToString();
Description = odr.GetValue(3).ToString();
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