如何在没有html标签的情况下从gridview读取字符串?

时间:2015-10-16 06:34:41

标签: c# asp.net gridview

我尝试导出为pdf,但我想保留宽度......

<asp:GridView ID="GridView1" runat="server" CssClass="auto-style1" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="30">
            <Columns>
                ... 
                <asp:TemplateField HeaderText="Name" HeaderStyle-Width="70" ItemStyle-Width="70">
                <ItemTemplate>
                         <div><%#Eval("FirstName")%> <br /><%#Eval("LastName")%></div>
                </ItemTemplate>
                </asp:TemplateField>    
                ....

C#:

iTextSharp.text.Table table = new iTextSharp.text.Table(GridView1.Columns.Count);
            table.Cellpadding = 5;
            int[] widths = new int[GridView1.Columns.Count];
            for (int x = 0; x < GridView1.Columns.Count; x++)
            {
                widths[x] = (int)GridView1.Columns[x].ItemStyle.Width.Value;
                string cellText = Server.HtmlDecode(GridView1.HeaderRow.Cells[x].Text);
                iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
                table.AddCell(cell);
            }
            table.SetWidths(widths);
                Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    {
                        for (int j = 0; j < GridView1.Columns.Count; j++)
                        {
                            if (GridView1.Columns[j] is TemplateField)
                            {

                            DataBoundLiteralControl lc = GridView1.Rows[i].Cells[j].Controls[0] as DataBoundLiteralControl;
                             string s = lc.Text.Trim().ToString();  
                        // Problem : s=<div> Andra <br />Grig</div>
string cellText1 = Server.HtmlDecode(s);
                            iTextSharp.text.Cell cell1 = new iTextSharp.text.Cell(cellText1);
                            table.AddCell(cell1);
                        }
                        else
                        {
                            string cellText = Server.HtmlDecode(GridView1.Rows[i].Cells[j].Text);
                            iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
                            table.AddCell(cell);
                        }
                    }
                }
            }

pdfDoc.Open(); 
pdfDoc.Add(table); 
pdfDoc.Close(); 
Response.ContentType = "application/pdf"; 
Response.AddHeader("content-disposition", "attachment;" + "filename=GridViewExport.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End();

在pdf中,“s”用html标签显示,我不想要那样。 请有人帮帮我吗?

0 个答案:

没有答案