Good Morning to all,
i have used below code used to generate pdf report from gridview displayed data,i am using gridview footer for sum of column but excel report not getting gridview footer row values and gridview color showing pdf report but lines notappear in pdf report .
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);
gvRecoveryCost.AllowPaging = false;
gvRecoveryCost.DataBind();
gvRecoveryCost.RenderControl(hw);
gvRecoveryCost.HeaderRow.Style.Add("width", "15%");
gvRecoveryCost.FooterRow.Style.Add("width", "15%");
gvRecoveryCost.HeaderRow.Style.Add("font-size", "10px");
gvRecoveryCost.Style.Add("text-decoration", "none");
gvRecoveryCost.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvRecoveryCost.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();
<asp:GridView ID="gvRecoveryCost" runat="server" AutoGenerateColumns="false" Width="1325px"
Font-Size="20px" Font-Bold="True" Font-Names="Calibri" PagerSettings-Mode="NumericFirstLast"
ShowFooter="true" BorderWidth="1px" CellPadding="4" ForeColor="#333333" GridLines="Both"
BorderColor="#df5015" BorderStyle="Solid" AllowSorting="false" OnRowDataBound="gvRecoveryCost_RowDataBound"
Visible="true">
<HeaderStyle BackColor="orange" Font-Bold="true" ForeColor="Black" />
<Columns>
<asp:TemplateField HeaderText="Category (A)" ControlStyle-ForeColor="Black">
<ItemTemplate>
<asp:Label ID="lblcprj" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Project") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="150" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Estimates -Oct 2015 (A)" Visible="false">
<ItemTemplate>
<asp:Label ID="lblinitbud" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ibudg","{0:N2}") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="B">
<ItemTemplate>
<asp:Label ID="lblvar" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "variance","{0:N2}") %>' />
<%--<asp:LinkButton ID="lblvar" runat="server" CommandName="Edit" Text='<%# DataBinder.Eval(Container.DataItem, "variance","{0:N2}") %>'
CommandArgument='<%#Eval("Project") %>' Font-Underline="true" />--%>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="C">
<ItemTemplate>
<asp:Label ID="lblbudval" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "budval","{0:N2}") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="D">
<ItemTemplate>
<asp:Label ID="lblpoval" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "poval","{0:N2}") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="E">
<ItemTemplate>
<asp:Label ID="lblactval" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "GRN","{0:N2}") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" BackColor="LightGray" />
<HeaderStyle HorizontalAlign="Center" BackColor="LightGray" />
</asp:TemplateField>
<asp:TemplateField HeaderText="F">
<ItemTemplate>
<asp:Label ID="lblinttrf" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "IntTrf","{0:N2}") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" BackColor="LightGray" />
<HeaderStyle HorizontalAlign="Center" BackColor="LightGray" />
</asp:TemplateField>
<asp:TemplateField HeaderText="J">
<ItemTemplate>
<asp:Label ID="lblstd" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "STD","{0:N2}") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" BackColor="LightGray" />
<HeaderStyle HorizontalAlign="Center" BackColor="LightGray" />
</asp:TemplateField>
<asp:TemplateField HeaderText="G=(E+F+J)">
<ItemTemplate>
<asp:Label ID="lblgrn" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "actval","{0:N2}") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="100" BackColor="LightGray" />
<HeaderStyle HorizontalAlign="Center" BackColor="LightGray" />
</asp:TemplateField>
<asp:TemplateField HeaderText="H =(D-E)">
<ItemTemplate>
<asp:Label ID="lblbtr" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "totbtr","{0:N2}") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="I = (C-D-F-J)">
<ItemTemplate>
<asp:Label ID="lblbb" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "totBudbal","{0:N2}") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="K = (G+H+I)">
<ItemTemplate>
<asp:Label ID="lbltctc" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "tottctc","{0:N2}") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="150" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void gvRecoveryCost_RowDataBound(object sender, GridViewRowEventArgs e)
{
SortedList formatCells1 = new SortedList();
formatCells1.Add("0", " ,4,1,1");
formatCells1.Add("1", "Cost To Date,4,1,2");
formatCells1.Add("2", " ,3,1,3");
SortedList formatCells = new SortedList();
//Format cells format:"
// formatCells.Add(<Column number>, <Header Name,number of column to colspan, number of row to rowspan>)
formatCells.Add("0", "Category,1,2");
formatCells.Add("1", "Approved Variance,1,1");
formatCells.Add("2", "Current Budget,1,1");
formatCells.Add("3", "PO's #,1,1");
formatCells.Add("4", "Against PO,1,1");
formatCells.Add("5", "Int. Transfer,1,1");
formatCells.Add("51", "STD Items,1,1");
formatCells.Add("52", "Total CTD,1,1");
formatCells.Add("53", "Balance To Receive,1,1");
formatCells.Add("54", "Budget Balance,1,1");
formatCells.Add("55", "ECTC,1,1");
GetMultiRowHeader(e, formatCells);
GetMultiRowHeader1(e, formatCells1);
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells.RemoveAt(0);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label proj = (Label)e.Row.FindControl("lblcprj");
Label lblinitbud = (Label)e.Row.FindControl("lblinitbud");
Label lblvar = (Label)e.Row.FindControl("lblvar");
Label lblbudval = (Label)e.Row.FindControl("lblbudval");
Label lblpoval = (Label)e.Row.FindControl("lblpoval");
Label lblactval = (Label)e.Row.FindControl("lblactval");
Label lblinttrf = (Label)e.Row.FindControl("lblinttrf");
Label lblgrn = (Label)e.Row.FindControl("lblgrn");
Label lblbtr = (Label)e.Row.FindControl("lblbtr");
Label lblbb = (Label)e.Row.FindControl("lblbb");
Label lbltctc = (Label)e.Row.FindControl("lbltctc");
Label lblstd = (Label)e.Row.FindControl("lblstd");
cgtot = cgtot + Convert.ToDouble(System.Convert.ToDouble(lblinitbud.Text.Trim() != string.Empty ? Convert.ToDouble(lblinitbud.Text.Trim()) : 0));
cgtot1 = cgtot1 + Convert.ToDouble(System.Convert.ToDouble(lblvar.Text.Trim() != string.Empty ? Convert.ToDouble(lblvar.Text.Trim()) : 0));
cgtot2 = cgtot2 + Convert.ToDouble(System.Convert.ToDouble(lblbudval.Text.Trim() != string.Empty ? Convert.ToDouble(lblbudval.Text.Trim()) : 0));
cgtot3 = cgtot3 + Convert.ToDouble(System.Convert.ToDouble(lblpoval.Text.Trim() != string.Empty ? Convert.ToDouble(lblpoval.Text.Trim()) : 0));
cgtot4 = cgtot4 + Convert.ToDouble(System.Convert.ToDouble(lblactval.Text.Trim() != string.Empty ? Convert.ToDouble(lblactval.Text.Trim()) : 0));
cgtot5 = cgtot5 + Convert.ToDouble(System.Convert.ToDouble(lblinttrf.Text.Trim() != string.Empty ? Convert.ToDouble(lblinttrf.Text.Trim()) : 0));
cgtot6 = cgtot6 + Convert.ToDouble(System.Convert.ToDouble(lblgrn.Text.Trim() != string.Empty ? Convert.ToDouble(lblgrn.Text.Trim()) : 0));
cgtot7 = cgtot7 + Convert.ToDouble(System.Convert.ToDouble(lblbtr.Text.Trim() != string.Empty ? Convert.ToDouble(lblbtr.Text.Trim()) : 0));
cgtot8 = cgtot8 + Convert.ToDouble(System.Convert.ToDouble(lblbb.Text.Trim() != string.Empty ? Convert.ToDouble(lblbb.Text.Trim()) : 0));
cgtot9 = cgtot9 + Convert.ToDouble(System.Convert.ToDouble(lbltctc.Text.Trim() != string.Empty ? Convert.ToDouble(lbltctc.Text.Trim()) : 0));
cgtot10 = cgtot10 + Convert.ToDouble(System.Convert.ToDouble(lblstd.Text.Trim() != string.Empty ? Convert.ToDouble(lblstd.Text.Trim()) : 0));
// lbltitle.Text = "Expected Cost to Complete (ECTC) - " + ddlproj.SelectedItem.Text + " :" + cgtot9 + "";
//if (proj.Text == "Material")
//{
// e.Row.BackColor = System.Drawing.Color.FromName("#FABF8F");
//}
//else if (proj.Text == "Sub-Contract")
//{
// e.Row.BackColor = System.Drawing.Color.FromName("#B1A0C7");
//}
//else if (proj.Text == "Project Expenses")
//{
// e.Row.BackColor = System.Drawing.Color.FromName("#C4BD97");
//}
//else if (proj.Text == "Project Budget")
//{
// e.Row.BackColor = System.Drawing.Color.FromName("#E6B8B7");
// //e.Row.Cells[9].ForeColor = Color.Red;
//}
//else if (proj.Text == "B&D Spares")
//{
// e.Row.BackColor = System.Drawing.Color.FromName("#E4DFEC");
//}
//else if (proj.Text == "Others")
//{
// e.Row.BackColor = System.Drawing.Color.FromName("#E4DFEC");
//}
e.Row.Cells[0].BackColor = System.Drawing.Color.Orange;
e.Row.Cells[1].BackColor = System.Drawing.Color.FromName("#FCD5B4");
e.Row.Cells[2].BackColor = System.Drawing.Color.FromName("#FCD5B4");
e.Row.Cells[3].BackColor = System.Drawing.Color.FromName("#C4D79B");
e.Row.Cells[4].BackColor = System.Drawing.Color.FromName("#FCD5B4");
// e.Row.Cells[5].BackColor = System.Drawing.Color.FromName("#FCD5B4");
// e.Row.Cells[6].BackColor = System.Drawing.Color.FromName("#FCD5B4");
// e.Row.Cells[7].BackColor = System.Drawing.Color.FromName("#FCD5B4");
// e.Row.Cells[8].BackColor = System.Drawing.Color.FromName("#FCD5B4");
e.Row.Cells[9].BackColor = System.Drawing.Color.FromName("#FCD5B4");
e.Row.Cells[10].BackColor = System.Drawing.Color.FromName("#FCD5B4");
e.Row.Cells[11].BackColor = System.Drawing.Color.FromName("#FCD5B4");
}
if ((e.Row.RowType == DataControlRowType.Footer))
{
e.Row.Cells[0].Text = "Total:";
//e.Row.Cells[0].BackColor = System.Drawing.Color.FromName("#006699");
e.Row.Cells[0].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[1].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[2].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[3].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[4].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[5].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[6].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[7].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[8].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[9].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[10].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[11].ForeColor = System.Drawing.Color.Red;
e.Row.BackColor = System.Drawing.Color.Yellow;
e.Row.Cells[1].Text = cgtot.ToString("n2");
e.Row.Cells[2].Text = cgtot1.ToString("n2");
e.Row.Cells[3].Text = cgtot2.ToString("n2");
e.Row.Cells[4].Text = cgtot3.ToString("n2");
e.Row.Cells[5].Text = cgtot4.ToString("n2");
e.Row.Cells[6].Text = cgtot5.ToString("n2");
e.Row.Cells[7].Text = cgtot10.ToString("n2");
e.Row.Cells[8].Text = cgtot6.ToString("n2");
e.Row.Cells[9].Text = cgtot7.ToString("n2");
e.Row.Cells[10].Text = cgtot8.ToString("n2");
e.Row.Cells[11].Text = cgtot9.ToString("n2");
// e.Row.Cells[11].Text = cgtot11.ToString("n2");
// e.Row.Cells[9].Text = cgtot8.ToString("0,0", CultureInfo.CreateSpecificCulture("hi-IN"));
e.Row.Cells[0].CssClass = "LabelText8";
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[0].Font.Bold = true;
e.Row.Cells[1].Font.Bold = true;
e.Row.Cells[2].Font.Bold = true;
e.Row.Cells[3].Font.Bold = true;
e.Row.Cells[4].Font.Bold = true;
e.Row.Cells[5].Font.Bold = true;
e.Row.Cells[6].Font.Bold = true;
e.Row.Cells[7].Font.Bold = true;
e.Row.Cells[8].Font.Bold = true;
e.Row.Cells[9].Font.Bold = true;
e.Row.Cells[10].Font.Bold = true;
e.Row.Cells[11].Font.Bold = true;
}
}