Archive for the ‘ASP.Net’ Category

Links to ITextSharp dll

Posted: May 25, 2007 in ASP.Net

http://sourceforge.net/projects/itextsharp/

Export to PDF in ASP.Net

Posted: May 25, 2007 in ASP.Net

This code snippet is used to Export a DataTable content into PDF and send it to client through the browser. Office 2003 should be installed at the client end.

A copy of the pdf will be created at the server and the same is sent to the client along with the response stream as attachments.

I am using ITextSharp dll which is freeware and which does the conversion from Datatable to PDF format. You can go and change the pdf format in the CreatePDFForm function.

//Is needed at the top of your code for referencing the following namespaces
using iTextSharp.text.pdf;
using iTextSharp.text;

#region Export to PDF
public void CreatePDFReport(DataTable dt, string reportName)
{
try
{
CreatePdfForm(dt, reportName);
string path = System.IO.Path.GetTempPath();
System.IO.FileStream fs = new System.IO.FileStream((path + “/” + reportName), System.IO.FileMode.OpenOrCreate);
byte[] byteArray = new byte[fs.Length];
fs.Read(byteArray, 0, Convert.ToInt32(fs.Length));
Response.AddHeader(“Content-disposition”, “attachment; filename=” + fs.Name);
Response.ContentType = “application/octet-stream”;
Response.BinaryWrite(byteArray);
fs.Close();
Response.End();
}
catch (System.Threading.ThreadAbortException te)
{
Response.Write(te);
}
}
#endregion

#region PDF conversion functions
private void CreatePdfForm(DataTable dt, string headerText)
{
string pdfFileName = headerText;
Document document = new Document(PageSize.A4.Rotate(), 50, 50, 50, 50);
string path = System.IO.Path.GetTempPath();
PdfWriter.GetInstance(document, new System.IO.FileStream(path + pdfFileName, System.IO.FileMode.Create));
document.Open();
//page header
Phrase headerPhrase = new Phrase(headerText, FontFactory.GetFont(“Verdana”, 24));
HeaderFooter header = new HeaderFooter(headerPhrase, false);
header.Border = iTextSharp.text.Rectangle.NO_BORDER;
header.Alignment = Element.ALIGN_LEFT;
document.Header = header;
document.Add(headerPhrase);
//page footer
HeaderFooter footer = new HeaderFooter(new Phrase(“Page”), true);
footer.Border = iTextSharp.text.Rectangle.NO_BORDER;
document.Footer = footer;
//build the content of the document
int numColumns = dt.Columns.Count;
PdfPTable datatable = new PdfPTable(numColumns);
datatable.DefaultCell.Padding = 3;
datatable.WidthPercentage = 100; // percentage
datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
//header row
datatable.DefaultCell.BorderWidth = 1;
datatable.DefaultCell.GrayFill = 0.7f;
//get column names
foreach (DataColumn column in dt.Columns)
{
string columnName = column.ColumnName;
Phrase phrase = new Phrase(columnName, FontFactory.GetFont(“Arial”, 8, iTextSharp.text.Font.BOLD));
datatable.AddCell(phrase);
}
datatable.HeaderRows = 1;
// this is the end of the table header
//content of report
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
string columnName = column.ColumnName;
datatable.DefaultCell.GrayFill = 0.85f;
Phrase phrase = new Phrase(row[columnName].ToString(), FontFactory.GetFont(“Arial”, 8));
datatable.AddCell(phrase);
}
}
document.Add(datatable);
document.Close();
}

#endregion

Export to Excel in ASP.Net

Posted: May 25, 2007 in ASP.Net

This is code for exporting a DataSet content into excel report and sending it to client through browser. The client should have Office 2003 atleast installed on this system.

A excel file will be initially created at the server end and then it is sent to the client through the browser.

Also, the thread abort exception occurs here because we are causing the Response to end. So, we need to suppress the exception.

#region Export to Excel
public void CreateExcelReport(DataSet ds, string reportName)
{
if (ds != null && ds.Tables.Count > 0)
{
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
string path = System.IO.Path.GetTempPath();
System.IO.FileStream fs = new System.IO.FileStream((path + “/” + reportName), System.IO.FileMode.OpenOrCreate);
try
{
//Clear the Response
Response.Clear();
//Set the Response headers
Response.AppendHeader(“Content-Type”, “application/vnd.ms-excel”);
Response.AppendHeader(“Content-Disposition”, “attachment; filename=” + fs.Name);
Response.Charset = “”;
//Create an instance of GridView
GridView gridView = new GridView();
//Iterating through each DataTable within the DataSet
foreach (DataTable dataTbl in ds.Tables)
{
gridView.DataSource = dataTbl;
gridView.DataBind();
}
//Output the gridView contents into HtmlWriter
gridView.RenderControl(htmlWrite);
Response.Write(stringWrite);
Response.End();
}
catch (System.Threading.ThreadAbortException te)
{
//Suppress ThreadAbort exception
}
finally
{
//Cleanup the objects
stringWrite.Close();
htmlWrite.Close();
fs.Close();
}
}
}
#endregion