我的网格视图上有一个rtf列,并希望以纯文本格式转换该列并将其显示为网格wiew

时间:2015-06-24 10:08:14

标签: c# webforms rtf

我的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();
        }
}

1 个答案:

答案 0 :(得分:1)

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());