How to create a detailed report in ASP.NET - asp.net

I want to create a report with very detailed data in ASP.NET. This report should be exportable to PDF and Excel. Can you tell me tips on how to do this kind of report?
I'm having a problem linking up my datasets in ReportViewer.
You will find below a screenshot of what I want :

Using iTextSharp download:
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=this.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
GridView1.AllowPaging = false;
GridView1.Parent.Controls.Add(frm);
frm.Controls.Add(GridView1);
frm.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document PDFdoc = new Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F);
iTextSharp.text.html.simpleparser.HTMLWorker htmlparser = new iTextSharp.text.html.simpleparser.HTMLWorker(PDFdoc);
PdfWriter.GetInstance(PDFdoc, Response.OutputStream);
PDFdoc.Open();
htmlparser.Parse(sr);
PDFdoc.Close();
Response.Write(PDFdoc);
Response.End();
To Excel:
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "YourExcelFile.xls"));
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();

Related

Adding Header Page in Itextsharp when the value is from gridview

I want to make convert gridview to pdf, the code is run well, but iam confused how to make headerpage (not header column) on it, ill try to use stringbuilder but when i tried the gridview is not view in pdf, i also tried other way but still dont know how to make it works,
maybe u can teach me how to make it works?
in this code i tried to use chunk, but the chunk isnot show :(
protected void btnConvertPDF_Click(object sender, EventArgs e)
{
gridconvertPDF.AllowPaging = false;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ClientList.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm hf = new HtmlForm();
gridconvertPDF.Parent.Controls.Add(hf);
hf.Attributes["runat"] = "server";
hf.Controls.Add(gridconvertPDF);
hf.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
pdfDoc.Open();
Chunk c = new Chunk
("PEMINJAMAN INVENTARIS \n",
FontFactory.GetFont("Verdana", 25));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk
("Rizki Asriningtyas \n",
FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);
pdfDoc.Add(p);
pdfDoc.Add(p1);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();}
my fault is make pdfwriter after add all the header content to pdf, here is the right code
protected void btnConvertPDF_Click(object sender, EventArgs e)
{
gridconvertPDF.AllowPaging = false;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ClientList.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm hf = new HtmlForm();
gridconvertPDF.Parent.Controls.Add(hf);
hf.Attributes["runat"] = "server";
hf.Controls.Add(gridconvertPDF);
hf.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
Chunk c = new Chunk
("PEMINJAMAN INVENTARIS \n",
FontFactory.GetFont("Verdana", 25));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk
("Rizki Asriningtyas \n",
FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);
pdfDoc.Add(p);
pdfDoc.Add(p1);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();}

while creating pdf, file name not coming dymaically . only static file name is coming

I want to give a file name according to project name. This line below is naming pdf statically
Response.AddHeader("content-disposition", "attachment;filename=WeeklyReport.pdf");
output file name - weeklyreport.pdf
while giving dynamic name its not taking .pdf
Response.AddHeader("content-disposition", "attachment;filename=" + Projname + ".pdf");
output-TestingProject
here .pdf is missing , so not opening in pdf by default
To make pdf file and name it dynamically u can use like this
Response.AddHeader("content-disposition", Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment;filename=\"{0}.pdf\"", Projname));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
design.RenderControl(htmlWrite);
string myText = stringWrite.ToString().Replace("&", "&");
StringReader sr = new StringReader(myText.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
Response.ContentType = "application/pdf";
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
Response.End();
pdfDoc.Dispose();

blank pdf coming while exporting to pdf in asp.net

i have one webform page . i have added everything in one div and wanted to export this div in pdf . while exporting in pdf . pdf is coming with empty.
protected void btnExport_Click(object sender, EventArgs e)
{
String DownloadFile = String.Format("{0}Targetkeywordsofdt-{1}", ddlproject.SelectedItem.Text, txtEndDate.Text.ToString() + ".pdf");
Response.AddHeader("content-disposition", "attachment;filename=" + DownloadFile);
Response.ContentType = "application/pdf";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
PdfReport.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
string strpath = Server.MapPath("~") + "/Temp/" + ddlproject.SelectedItem.Text.Trim() + ".pdf";
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strpath, FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
pdfDoc.Dispose();
Response.Write(pdfDoc);
Response.End();
}

how to convert aspx page to pdf with gridview and css class design

How to convert .aspx page with css class design to pdf format which contains asp.net controls.
I have used some code but its not covert the page with proper allignment and design.
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
a.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
I have used this code..
Can you help me.please.

How could i export gridview data to ms word

I am working on asp.net 4.0 web application development using c#. I am trying to export the Gridview data to ms word document. I have used the following code.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=MyWord.doc");
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/vnd.word";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
The application is running without problems but it returns the data in word like
+ADw-div+AD4- +ADw-table cellspacing+AD0AIg-0+ACI- cellpadding+AD0AIg-4+ACI- id+AD0AIg-GridView1+ACI- style+AD0AIg-color:+ACM-333333+ADs-border-collapse:collapse+ADsAIgA+- +ADw-tr style+AD0AIg-color:White+ADs-background-color:+ACM-507CD1+ADs-font-weight:bold+ADsAIgA+- +ADw-th scope+AD0AIg-col+ACIAPg-FileId+ADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPg-PortalId+ADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPg-FileName+ADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPg-Extension+ADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPg-Size+ADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPg-Width+ADw-
I know it is much similar to html but how could i convert this into actual one. guide me.
this is works for me it requires itextsharp.dll
string FName = "filename.doc";
mygrid.AllowPaging = false;
mygrid.DataSource = datasource();
mygrid.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", FName));
Response.Charset = "";
Response.ContentType = "application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
mygrid.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

Resources