i have two pages which contains gridviews i have two buttons on each page which will help to print the gridviews as Excel and Pdf
one page contains normal gridview one page contains multi header
I created Multi header by using the faallowing code
public static void GetMultiRowHeader(GridViewRowEventArgs e, SortedList GetCels)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow row;
IDictionaryEnumerator enumCels = GetCels.GetEnumerator();
row = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
while (enumCels.MoveNext())
{
String[] count = enumCels.Value.ToString().Split(Convert.ToChar(","));
TableCell Cell;
Cell = new TableCell();
Cell.RowSpan = Convert.ToInt16(count[2].ToString());
Cell.ColumnSpan = Convert.ToInt16(count[1].ToString());
Cell.Controls.Add(new LiteralControl(count[0].ToString()));
Cell.HorizontalAlign = HorizontalAlign.Center;
Cell.ForeColor = System.Drawing.Color.White;
row.Cells.Add(Cell);
}
e.Row.Parent.Controls.AddAt(0, row);
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SortedList formatCells = new SortedList();
SortedList formatcells2 = new SortedList();
formatcells2.Add("1", ",9,1");
formatcells2.Add("10", "Ground Status (Extent in Sq.Mtrs),2,1");
formatcells2.Add("12", "Court case Status,2,1");
formatcells2.Add("14", "Allotted to Govt Depts(others and Exemptions),2,1");
formatcells2.Add("16", ",2,1");
GetMultiRowHeader(e, formatcells2);
GetMultiRowHeader(e, formatCells);
}
I Generated Excel by using fallowing code
protected void ImageButtonExcel_Click(object sender, ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ulcact.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}
int j = 1;
foreach (GridViewRow gvrow in GridView1.Rows)
{
if (j <= GridView1.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
I created Pdf by using the fallowing code
protected void ImageButtonPdf_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ulcact.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
GridView1.HeaderRow.Style.Add("width", "15%");
GridView1.HeaderRow.Style.Add("font-size", "10px");
GridView1.Style.Add("text-decoration", "none");
GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
GridView1.Style.Add("font-size", "8px");
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();
}
The problems i faced are
1.while in the administration mode i have two more columns in the gridview for selecting and updating i wanted to exclude them from printing as excel
(i,e simply i wanted to exclude first two colums from excel when generating Excel sheet)
2.in the multi header form i am getting the fallowing error (i used iTextSharp for generating Pdf
The number of columns in PdfPTable constructor must be greater than zero.
Line 129: PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
Line 130: pdfDoc.Open();
Line 131: htmlparser.Parse(sr);
Line 132: pdfDoc.Close();
Line 133: Response.Write(pdfDoc);
Error Line:Source File: c:\Users\ARTISAN\Documents\Visual Studio 2008\WebSites\ULCHYD\ulcact.aspx.cs Line: 131