Use the following code
-----------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Pabitra_TestWork_Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnExport" runat="server" CssClass="green_button" Text="Export To Excel" Width="180px" OnClientClick="ExportToExcel();" />
<br/>
<asp:HiddenField runat="server" ID="hfExcelName" Value="MyCustomExcel" />
<br/>
<div id="divprint">
<table border='1'>
<tr>
<td>Id</td>
<td>Name</td>
<td>City</td>
</tr>
<tr>
<td>1</td>
<td>Pabitra</td>
<td>Bhubaneswar</td>
</tr>
</table>
<%--or use gridview here --%>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
function ExportToExcel() {
var ExcelName = document.getElementById("<%=hfExcelName.ClientID %>");
//creating a temporary HTML link element (they support setting file names)
var a = document.createElement('a');
//getting data from our div that contains the HTML table
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementById('divprint');
var table_html = table_div.outerHTML.replace(/ /g, '%20');
a.href = data_type + ', ' + table_html;
//setting the file name
a.download = ExcelName.value + '.xls';
//triggering the function
a.click();
//just in case, prevent default behaviour
e.preventDefault();
}
</script>
</form>
</body>
</html>
-----------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Pabitra_TestWork_Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnExport" runat="server" CssClass="green_button" Text="Export To Excel" Width="180px" OnClientClick="ExportToExcel();" />
<br/>
<asp:HiddenField runat="server" ID="hfExcelName" Value="MyCustomExcel" />
<br/>
<div id="divprint">
<table border='1'>
<tr>
<td>Id</td>
<td>Name</td>
<td>City</td>
</tr>
<tr>
<td>1</td>
<td>Pabitra</td>
<td>Bhubaneswar</td>
</tr>
</table>
<%--or use gridview here --%>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
function ExportToExcel() {
var ExcelName = document.getElementById("<%=hfExcelName.ClientID %>");
//creating a temporary HTML link element (they support setting file names)
var a = document.createElement('a');
//getting data from our div that contains the HTML table
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementById('divprint');
var table_html = table_div.outerHTML.replace(/ /g, '%20');
a.href = data_type + ', ' + table_html;
//setting the file name
a.download = ExcelName.value + '.xls';
//triggering the function
a.click();
//just in case, prevent default behaviour
e.preventDefault();
}
</script>
</form>
</body>
</html>
No comments:
Post a Comment