Here I have created sample that will help you out.
In this I have created SampleLogFile.xlsx (Note : make this file protected) that I have used to get structure of excelfile to create daily basis excel file.
Page1
HTML
<div>
<table cellpadding="0" cellspacing="0" class="auto-style1">
<tr>
<td class="auto-style2">
Name:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">
Email:
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">
Mobile No.:
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">
Location:
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">
Qualification:
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="CreateExcel" />
</td>
</tr>
</table>
</div>
Code
protected void CreateExcel(object sender, EventArgs e)
{
string ConStr = "";
string sourceFile = Server.MapPath("~/SampleFiles/SampleLogFile.xlsx");
string destinationFile = Server.MapPath("~/LogFiles/InsertDataExcel" + DateTime.Now.ToShortDateString().Replace("/", "_") + ".xlsx");
if (!File.Exists(destinationFile))
{
File.Copy(sourceFile, destinationFile);
}
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + destinationFile + ";Extended Properties='Excel 12.0;ReadOnly=False;HDR=Yes;'";
string query = "INSERT INTO [Sheet1$] ([Name], [Email], [MobileNo], [Location], [Qualification]) VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
OleDbConnection conn = new OleDbConnection(ConStr);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
OleDbCommand cmd = new OleDbCommand(query, conn);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
Response.Write("<script>alert('Sucessfully Data Inserted Into Excel')</script>");
}
else
{
Response.Write("<script>alert('Sorry!\n Insertion Failed')</script>");
}
conn.Close();
Response.Redirect("Default2.aspx");
}
Page2
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<h3>
<b>User Log</b></h3>
</div>
<div class="container-fluid" style="background: #fff; padding: 10px; margin: 2px;
border: 1px solid #ccc;">
<div class="container">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No files uploaded"
Width="80%" GridLines="Vertical" BorderStyle="Solid" BorderColor="Transparent"
BorderWidth="0" AlternatingRowStyle-BackColor="#e6e6e6" CellPadding="-1" CellSpacing="0"
RowStyle-Height="30">
<Columns>
<asp:BoundField DataField="Text" HeaderText="File Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("Value") %>'
runat="server" OnClick="DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</div>
</div>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
try
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/LogFiles/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
Screenshot
