Note:-
1. First
take the copy of the Master Page.
2. In
the Original Master the Form tag will be like this <form
runat="server" onsubmit="if (typeof(_spFormOnSubmitWrapper) !=
'undefined') {return _spFormOnSubmitWrapper();} else {return true;}">.
3. In
the copy you have to remove this " onsubmit="if
(typeof(_spFormOnSubmitWrapper) != 'undefined') {return
_spFormOnSubmitWrapper();} else {return true;}" in Form tag.
4. Now
apply the master page to your Aspx page where you are using this webpart.
5. Now
you can generate any kind of document how many times you want you can.
6. If
you are not setting this then you can generate only one document at a time. If
you want to generate another document you have to refresh the page again.
7. For
PDF download ITextSharp DLL from http://itextpdf.com/ and add the dll in the References of your WebPart.
Ascx
|
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0,
Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint,
Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
%>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint,
Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
%>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint,
Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
%>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="dgvHTMLBinding.VisualWebPart1.VisualWebPart1UserControl"
%>
<style type="text/css">
.HeaderStyle
{
background-color:Navy;
color:white;
font-size:15px;
}
.ItemTemplateStyle
{
background-color:Orange;
color:white;
padding-left:5px;
font-size:15px;
/*text-align:Center;*/
}
.AlternativeTemplateStyle
{
background-color:green;
color:white;
padding-left:5px;
font-size:15px;
/*text-align:Center;*/
}
.BlankTr
{
background-color:Red;
}
</style>
<asp:Button ID="btnGeneratePDF" runat="server" Text="Generate
PDF"
onclick="btnGeneratePDF_Click" /> <asp:Button ID="btnGeneratePPT"
runat="server" Text="Generate PPT" onclick="btnGeneratePPT_Click" /> <asp:Button
ID="btnGenerateExcel" runat="server"
Text="Generate
Excel"
onclick="btnGenerateExcel_Click" /> <asp:Button ID="btnGenerateWord"
runat="server" Text="Generate Word" onclick="btnGenerateWord_Click" />
<asp:GridView ID="dgvBreakingNews" runat="server"
AutoGenerateColumns="false"
Width="100%"
CellPadding="0"
CellSpacing="0">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<div class="BlankTr" style="width:100%;height:2px;"> </div>
<div style="width:100%;text-align:center;" class="HeaderStyle">
Breaking News
</div>
<div class="BlankTr" style="width:100%;height:2px;"> </div>
</HeaderTemplate>
<ItemTemplate>
<div style="width:100%;" class="ItemTemplateStyle">
<span>
<%# Eval("ID") %>.
</span>
<span>
<strong><%# Eval("Title") %></strong>
</span>
</div>
<div class="BlankTr" style="width:100%;height:2px;"> </div>
</ItemTemplate>
<AlternatingItemTemplate>
<div style="width:100%;" class="AlternativeTemplateStyle">
<span>
<%# Eval("ID") %>.
</span>
<span>
<strong><%# Eval("Title") %></strong>
</span>
</div>
<div class="BlankTr" style="width:100%;height:2px;"> </div>
</AlternatingItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
|
Ascx.cs
|
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
using System.IO;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.xml;
using iTextSharp.text.pdf;
using
iTextSharp.text.html.simpleparser;
namespace dgvHTMLBinding.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridVIew();
}
}
public
void BindGridVIew()
{
SPWeb
currentWeb = SPContext.Current.Web;
SPList
lst = currentWeb.Lists["Breaking News"];
SPQuery
sQuery = new SPQuery();
sQuery.Query = "<OrderBy><FieldRef Name='ID'
Ascending='False' /></OrderBy>";
SPListItemCollection
myColl = lst.GetItems(sQuery);
dgvBreakingNews.DataSource =
myColl.GetDataTable();
dgvBreakingNews.DataBind();
}
protected
void btnGeneratePDF_Click(object sender, EventArgs
e)
{
AvoidFormRunAtServerError();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Sample.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter
sw = new StringWriter();
HtmlTextWriter
hw = new HtmlTextWriter(sw);
dgvBreakingNews.RenderControl(hw);
StringReader
sr = new StringReader(sw.ToString());
Document
pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker
htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,
Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
protected
void btnGeneratePPT_Click(object sender, EventArgs
e)
{
//Unable to find
solution for this
}
protected
void btnGenerateExcel_Click(object sender, EventArgs
e)
{
AvoidFormRunAtServerError();
string
attachment = "attachment;
filename=Sample.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter
sw = new StringWriter();
HtmlTextWriter
htw = new HtmlTextWriter(sw);
dgvBreakingNews.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public
void AvoidFormRunAtServerError()
{
MyPage
tmpPage = new MyPage();
HtmlForm
form = new HtmlForm();
form.Controls.Add(dgvBreakingNews);
tmpPage.Controls.Add(form);
}
protected
void btnGenerateWord_Click(object sender, EventArgs
e)
{
AvoidFormRunAtServerError();
string
attachment = "attachment;
filename=Sample.doc";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/msword";
StringWriter
sw = new StringWriter();
HtmlTextWriter
htw = new HtmlTextWriter(sw);
dgvBreakingNews.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
}
}
|
No comments:
Post a Comment