Default.aspx(Design)-:
Note: Add iTextsharp Dll in Your Project.It can be downloded from here
http://sourceforge.net/projects/itextsharp/files/latest/download
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function Print() {
document.body.offsetHeight; window.print();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%" align="center">
<tr>
<td width="25%"></td><td width="25%"></td><td width="25%"></td>
<td width="25%">
<asp:ImageButton ID="ImgExcel" runat="server" ImageUrl="~/Image/excel.jpg"
Width="50px" Height="50px" onclick="ImgExcel_Click" />
<asp:ImageButton ID="ImgWord" runat="server" ImageUrl= "~/Image/word.jpg"
Width="50px" Height="50px" onclick="ImgWord_Click" />
<asp:ImageButton ID="ImgPdf" runat="server" ImageUrl= "~/Image/pdf.jpg"
Width="50px" Height="50px" onclick="ImgPdf_Click" />
<asp:ImageButton ID="ImgPrint" runat="server" ImageUrl="~/Image/print.gif" OnClientClick="Print()"
Width="50px" Height="50px"/></td>
</tr>
</table>
<asp:Label ID="LblView" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
Note: Add iTextsharp Dll in Your Project.It can be downloded from here
http://sourceforge.net/projects/itextsharp/files/latest/download
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function Print() {
document.body.offsetHeight; window.print();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%" align="center">
<tr>
<td width="25%"></td><td width="25%"></td><td width="25%"></td>
<td width="25%">
<asp:ImageButton ID="ImgExcel" runat="server" ImageUrl="~/Image/excel.jpg"
Width="50px" Height="50px" onclick="ImgExcel_Click" />
<asp:ImageButton ID="ImgWord" runat="server" ImageUrl= "~/Image/word.jpg"
Width="50px" Height="50px" onclick="ImgWord_Click" />
<asp:ImageButton ID="ImgPdf" runat="server" ImageUrl= "~/Image/pdf.jpg"
Width="50px" Height="50px" onclick="ImgPdf_Click" />
<asp:ImageButton ID="ImgPrint" runat="server" ImageUrl="~/Image/print.gif" OnClientClick="Print()"
Width="50px" Height="50px"/></td>
</tr>
</table>
<asp:Label ID="LblView" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs(Code):
Add Following Namespace in your C# Page.
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
Add Following Code In Page Load for Html Report Generation.
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/App_Data/XMLFile.xml"));
StringBuilder strHtmlContent = new StringBuilder();
strHtmlContent.Append("<h2 align='center'><b><font family='verdana'><u>Student Information</u></font></b></h1>".ToString());
strHtmlContent.Append("<br/>".ToString());
strHtmlContent.Append("<table align='center' width='50%' border='0' style='font-family:verdana'>".ToString());
strHtmlContent.Append("<tr><td width='20%'>College Name:</td><td width='20%'>A.S COLLEGE,TIRTOL</td><td width='20%'>Date:</td><td width='20%'>30/12/2011</td><td width='20%' rowspan='3'></td></tr>".ToString());
strHtmlContent.Append("<tr><td width='20%'>College Address:</td><td width='20%'>Tirtol,Jagatsinfhpur</td><td width='20%'>Place:</td><td width='20%'>Tirtol</td></tr>".ToString());
strHtmlContent.Append("<tr><td width='20%'>Year:</td><td width='20%'>2011</td><td width='20%'></td><td width='20%'></td></tr>".ToString());
strHtmlContent.Append("</table>".ToString());
strHtmlContent.Append("<table align='center' width='50%' border='1' style='font-family:verdana'>".ToString());
strHtmlContent.Append("<tr style='font-weight:Bold'><b><td width='40%'>Name</td><td width='35%'>Country</td><td width='35%'>City</td></b></tr>".ToString());
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
strHtmlContent.Append("<tr><td width='40%'>" + ds.Tables[0].Rows[i][0].ToString() + "</td><td width='35%'>" + ds.Tables[0].Rows[i][1].ToString() + "</td><td width='35%'>" + ds.Tables[0].Rows[i][2].ToString() + "</td></tr>".ToString());
}
strHtmlContent.Append("</table>".ToString());
strHtmlContent.Append("<table align='center' width='50%' style='font-family:verdana;font-weight:Bold'>".ToString());
strHtmlContent.Append("<tr><td colspan='3' align='right'>Signture:</td></tr>".ToString());
strHtmlContent.Append("<tr><td colspan='3' align='right'>Date:</td></tr>".ToString());
//strHtmlContent.Append("<tr><td colspan='3' align='center'><input type='button' value='PRINT' runat='server' onClick='Print()' ID='btnprint'> <input type='button' value='WORD' runat='server' ID='btnword' onClick='btnword_Click'></td></tr>".ToString());
strHtmlContent.Append("</table>".ToString());
LblView.Text = strHtmlContent.ToString();
}
protected void ImgWord_Click(object sender, ImageClickEventArgs e)
{
string attachment = "attachment; filename=Contacts.doc";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/msword";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
LblView.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
protected void ImgExcel_Click(object sender, ImageClickEventArgs e)
{
string attachment = "attachment; filename=GridviewExport.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
LblView.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
protected void ImgPdf_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//GridView1.AllowPaging = false;
//GridView1.DataBind();
//GridView1.RenderControl(hw);
LblView.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Add Following code in JavaScript :
function Print() {
document.body.offsetHeight; window.print();
}
Page View:
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
Add Following Code In Page Load for Html Report Generation.
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/App_Data/XMLFile.xml"));
StringBuilder strHtmlContent = new StringBuilder();
strHtmlContent.Append("<h2 align='center'><b><font family='verdana'><u>Student Information</u></font></b></h1>".ToString());
strHtmlContent.Append("<br/>".ToString());
strHtmlContent.Append("<table align='center' width='50%' border='0' style='font-family:verdana'>".ToString());
strHtmlContent.Append("<tr><td width='20%'>College Name:</td><td width='20%'>A.S COLLEGE,TIRTOL</td><td width='20%'>Date:</td><td width='20%'>30/12/2011</td><td width='20%' rowspan='3'></td></tr>".ToString());
strHtmlContent.Append("<tr><td width='20%'>College Address:</td><td width='20%'>Tirtol,Jagatsinfhpur</td><td width='20%'>Place:</td><td width='20%'>Tirtol</td></tr>".ToString());
strHtmlContent.Append("<tr><td width='20%'>Year:</td><td width='20%'>2011</td><td width='20%'></td><td width='20%'></td></tr>".ToString());
strHtmlContent.Append("</table>".ToString());
strHtmlContent.Append("<table align='center' width='50%' border='1' style='font-family:verdana'>".ToString());
strHtmlContent.Append("<tr style='font-weight:Bold'><b><td width='40%'>Name</td><td width='35%'>Country</td><td width='35%'>City</td></b></tr>".ToString());
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
strHtmlContent.Append("<tr><td width='40%'>" + ds.Tables[0].Rows[i][0].ToString() + "</td><td width='35%'>" + ds.Tables[0].Rows[i][1].ToString() + "</td><td width='35%'>" + ds.Tables[0].Rows[i][2].ToString() + "</td></tr>".ToString());
}
strHtmlContent.Append("</table>".ToString());
strHtmlContent.Append("<table align='center' width='50%' style='font-family:verdana;font-weight:Bold'>".ToString());
strHtmlContent.Append("<tr><td colspan='3' align='right'>Signture:</td></tr>".ToString());
strHtmlContent.Append("<tr><td colspan='3' align='right'>Date:</td></tr>".ToString());
//strHtmlContent.Append("<tr><td colspan='3' align='center'><input type='button' value='PRINT' runat='server' onClick='Print()' ID='btnprint'> <input type='button' value='WORD' runat='server' ID='btnword' onClick='btnword_Click'></td></tr>".ToString());
strHtmlContent.Append("</table>".ToString());
LblView.Text = strHtmlContent.ToString();
}
protected void ImgWord_Click(object sender, ImageClickEventArgs e)
{
string attachment = "attachment; filename=Contacts.doc";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/msword";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
LblView.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
protected void ImgExcel_Click(object sender, ImageClickEventArgs e)
{
string attachment = "attachment; filename=GridviewExport.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
LblView.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
protected void ImgPdf_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//GridView1.AllowPaging = false;
//GridView1.DataBind();
//GridView1.RenderControl(hw);
LblView.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Add Following code in JavaScript :
function Print() {
document.body.offsetHeight; window.print();
}
Page View: