ASP.Net
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" ID="btnExcel" Text="Generate Excel" OnClick="btnExcel_Click" />
</div>
</form>
</body>
Backend Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
namespace Excelgeneration
{
public partial class ExcelGeneration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnExcel_Click(object sender, EventArgs e)
{
GenerateExcel();
}
#region GenerateExcel
public void GenerateExcel()
{
Application oXL = new Application();
Workbook WB = null;
Worksheet WS = null;
Range oRng;
oXL.Visible = true;
// creating workbook
//--------------------
WB = (Workbook)(oXL.Workbooks.Add(Missing.Value));
//Create work sheet
WS = (Worksheet)(WB.ActiveSheet);
//save the Excel File
// -----------------------
WB.SaveAs(@"D:\Excelfiles\Srinivas5", XlFileFormat.xlWorkbookDefault);
// F:\Desktop is the file path
// srinivas is the file name
//oXL.UserControl = true;
}
#endregion
}
}
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" ID="btnExcel" Text="Generate Excel" OnClick="btnExcel_Click" />
</div>
</form>
</body>
Backend Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
namespace Excelgeneration
{
public partial class ExcelGeneration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnExcel_Click(object sender, EventArgs e)
{
GenerateExcel();
}
#region GenerateExcel
public void GenerateExcel()
{
Application oXL = new Application();
Workbook WB = null;
Worksheet WS = null;
Range oRng;
oXL.Visible = true;
// creating workbook
//--------------------
WB = (Workbook)(oXL.Workbooks.Add(Missing.Value));
//Create work sheet
WS = (Worksheet)(WB.ActiveSheet);
//save the Excel File
// -----------------------
WB.SaveAs(@"D:\Excelfiles\Srinivas5", XlFileFormat.xlWorkbookDefault);
// F:\Desktop is the file path
// srinivas is the file name
//oXL.UserControl = true;
}
#endregion
}
}