为什么某些特殊字符 (“Ü”) 显示为编码的 HTML 实体 (ü)

本文关键字:#252 HTML 实体 编码 amp 显示 特殊字符 为什么 | 更新日期: 2023-09-27 18:20:38

这是我的DetailViewSqlDataSource

<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateRows="False" HeaderText="Rechnung Details" DataKeyNames="InvoiceID" CssClass="table table-striped table-bordered">
    <Fields>
        <asp:BoundField DataField="InvoiceID" HeaderText="Rechnungs Nummer" InsertVisible="False" ReadOnly="True" SortExpression="InvoiceID" />
        <asp:BoundField DataField="Date" HeaderText="Datum" SortExpression="Date" DataFormatString="{0:dd/MM/yyyy}"/>
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ReadOnly="true" />
        <asp:BoundField DataField="Tell" HeaderText="Telefon" SortExpression="Tell" ReadOnly="true" />
        <asp:BoundField DataField="Address" HeaderText="Addresse" SortExpression="Address" ReadOnly="true"/>
        <asp:BoundField DataField="Amount" HeaderText="Betrag" SortExpression="Amount" />
        <asp:BoundField DataField="Paid" HeaderText="Bezahlter Betrag" SortExpression="Paid" />
        <asp:BoundField DataField="Rest" HeaderText="Rest" SortExpression="Rest" ReadOnly="true" />
        <asp:BoundField DataField="PaymentType" HeaderText="Zahlungsart" SortExpression="PaymentType" />
        <asp:CheckBoxField DataField="Shipped" HeaderText="erledigt" SortExpression="Shipped" />
        <asp:BoundField DataField="Comment" HeaderText="Bericht" SortExpression="Comment" ReadOnly="true" />
        <asp:CommandField ShowEditButton="True" />
        <asp:CommandField ShowDeleteButton="True" />
    </Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RechnungConnectionString %>" 
    SelectCommand="SELECT Invoice.[InvoiceID], Invoice.[CustomerID], Invoice.[Date], Invoice.[Amount], Invoice.[Paid], Invoice.[Rest], Invoice.[PaymentType], Invoice.[Shipped], Customer.[CustomerID], Customer.[Name], Customer.[Tell], Customer.[Address], Customer.[Comment] FROM [Invoice] INNER JOIN [Customer] ON Invoice.[CustomerID] = Customer.[CustomerID] WHERE Invoice.[InvoiceID] = @InvoiceID"
    UpdateCommand="UPDATE [Invoice] SET Invoice.[Date]=@Date, Invoice.[Amount]=@Amount, Invoice.[Paid]=@Paid, Invoice.[PaymentType]=@PaymentType, Invoice.[Shipped]=@Shipped WHERE [InvoiceID]=@InvoiceID"
    DeleteCommand="DELETE FROM Invoice WHERE [InvoiceID] = @InvoiceID">
     <SelectParameters>
      <asp:QueryStringParameter Name="InvoiceID" QueryStringField="ID" />
    </SelectParameters>
</asp:SqlDataSource>

这是我的按钮,用于从DetailView的数据创建PDF:

        protected void CreatePDF_Click(object sender, EventArgs e)
    {
        string InvoiceID = DetailsView1.Rows[0].Cells[1].Text;
        string Date = DetailsView1.Rows[1].Cells[1].Text;
        //string Name = DetailsView1.Rows[2].Cells[1].Text;
        StreamReader srn = new StreamReader(DetailsView1.Rows[2].Cells[1].Text, Encoding.GetEncoding("iso-8859-1"));
        string Name = srn.ReadToEnd();
        string Tell = DetailsView1.Rows[3].Cells[1].Text;
        //string Address = DetailsView1.Rows[4].Cells[1].Text;
        StreamReader sra = new StreamReader(DetailsView1.Rows[4].Cells[1].Text, Encoding.GetEncoding("iso-8859-1"));
        string Address = sra.ReadToEnd();
        string Amount = DetailsView1.Rows[5].Cells[1].Text;
        string TimeNow = Convert.ToString(DateTime.Now.Date);


        string pdfpath = Server.MapPath("~/PDF/");
        string imagepath = Server.MapPath("~/Image/AlsterLogo.jpg");
        string pdfName = Name + " Rechnungs Nummer = " + InvoiceID;
        //Response.ContentType = "application/pdf";
        //Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
        //Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Document pdfDoc = new Document(PageSize.A4, 15f, 15f, 30f, 30f);
        PdfWriter.GetInstance(pdfDoc, new FileStream(Path.Combine(pdfpath, pdfName) + ".pdf" , FileMode.Create));
        pdfDoc.Open();
        iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imagepath);
        jpg.Alignment = Element.ALIGN_CENTER;
        Font arial8U = FontFactory.GetFont("Arial", 8, Font.UNDERLINE, BaseColor.BLACK);
        Font arial8 = FontFactory.GetFont("Arial", 8, BaseColor.BLACK);
        Font arial10 = FontFactory.GetFont("Arial", 10, BaseColor.BLACK);
        Font arial10B = FontFactory.GetFont("Arial", 10, Font.BOLD, BaseColor.BLACK);
        PdfPTable table = new PdfPTable(2);
        table.DefaultCell.Border = Rectangle.NO_BORDER;
        table.TotalWidth = 500f;
        table.LockedWidth = true;
        float[] widths = new float[] { 325f , 175f };
        table.SetWidths(widths);
        PdfPCell cell = new PdfPCell(new Phrase("ASM Transporte Mundsburger Damm 38, 22087 Hamburg", arial8U));
        cell.Border = Rectangle.NO_BORDER;
        PdfPCell cell1 = new PdfPCell(new Phrase("Herr/Frau/Firma", arial10));
        cell1.Border = Rectangle.NO_BORDER;
        PdfPCell cell2 = new PdfPCell(new Phrase("Tel: 040-29 12 12", arial10));
        cell2.Border = Rectangle.NO_BORDER;
        PdfPCell cell3 = new PdfPCell(new Phrase("Fax: 040-29823820", arial10));
        cell3.Border = Rectangle.NO_BORDER;
        PdfPCell cell4 = new PdfPCell(new Phrase("Sachbearbeiter: S.Dadras", arial10));
        cell4.Border = Rectangle.NO_BORDER;
        PdfPCell cell5 = new PdfPCell(new Phrase("mail: dadras@alster-umzuege.de", arial8));
        cell5.Border = Rectangle.NO_BORDER;
        PdfPCell cell6 = new PdfPCell(new Phrase("Rechnung-Nr: " + InvoiceID, arial10B));
        cell6.HorizontalAlignment = 1;
        //cell6.Border = Rectangle.NO_BORDER;
        PdfPCell cell7 = new PdfPCell(new Phrase("Hamburg " + TimeNow, arial10));
        cell7.Border = Rectangle.NO_BORDER;


        table.AddCell(cell);
        table.AddCell("");
        table.AddCell(cell1);
        table.AddCell("");
        table.AddCell(Name);
        table.AddCell(cell2);
        table.AddCell(Address);
        table.AddCell(cell3);
        table.AddCell("");
        table.AddCell(cell4);
        table.AddCell("");
        table.AddCell(cell5);
        table.AddCell("");
        table.AddCell(cell6);
        table.AddCell("");
        table.AddCell(cell7);

        pdfDoc.Add(jpg);
        pdfDoc.Add(table);
        pdfDoc.Close();
    }

问题是当PDF创建时,它无法读取特殊字符的Like Ü! 我该如何解决这个问题?!

为什么某些特殊字符 (“Ü”) 显示为编码的 HTML 实体 (&#252;)

在向

pdf 添加文本的任何地方(或至少在您希望特殊字符的地方(使用 HttpUtility.HtmlDecode (http://msdn.microsoft.com/en-us/library/system.web.httputility.htmldecode(v=vs.110(.aspx