我在网格视图中有一个rtf列,希望将该列转换为纯文本并显示到网格视图中

本文关键字:视图 网格 文本 显示 转换 希望 有一个 rtf | 更新日期: 2023-09-27 17:59:16

我的sql server数据库中有一个rtf列,我想将其转换为纯文本,这样我就可以在网格视图列中显示为纯文本。比我正在将网格视图转换为单词。我被困于将rtf列转换为纯文本

代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.SqlTypes; 
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;
using Itenso.Rtf.Converter.Text;
using Itenso.Rtf.Support;
  public partial class WebForm1 : System.Web.UI.Page{
    protected void Page_Load(object sender, EventArgs e)
        {
            string strQuery = "select * from customers";
            SqlCommand cmd = new SqlCommand(strQuery);
            DataTable dt = GetData(cmd);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        private DataTable GetData(SqlCommand cmd)
        {
            DataTable dt = new DataTable();
            String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            try
            {
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                sda.Dispose();
                con.Dispose();
            }
        }
 protected void btnExportWord_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Export.doc");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-word ";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView1.AllowPaging = false;
            GridView1.DataBind();
            GridView1.RenderControl(hw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
}

我在网格视图中有一个rtf列,希望将该列转换为纯文本并显示到网格视图中

public string getplaintext(string rtftext)
     {  
        string plainText="";
       //Create the RichTextBox. (Requires a reference   to System.Windows.Forms.dll.)
       using(System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox());
       {
             // Convert the RTF to plain text.
             rtBox.Rtf = rtftext;
             plainText = rtBox.Text;
           // Now just remove the new line constants
           plainText = plainText.Replace("'r'n", ",");
          // Output plain text to file, encoded as UTF-8.
         }
          return plainText;
      }

用法

var value=getplaintext(dt[0][1].toString())