0

我像这样创建了一个表richtextbox

       //Since too much string appending go for string builder
       StringBuilder tableRtf = new StringBuilder();

       //beginning of rich text format,dont customize this begining line              
    tableRtf.Append(@"{\rtf1 ");             

    //create 5 rows with 3 cells each

        for (int i = 0; i < 5; i++)
        {

            tableRtf.Append(@"\trowd");

           //A cell with width 1000.
            tableRtf.Append(@"\cellx1000"); 

            //Another cell with width 2000.end point is 3000 (which is 1000+2000).
            tableRtf.Append(@"\cellx2000"); 

            //Another cell with width 1000.end point is 4000 (which is 3000+1000)
            tableRtf.Append(@"\cellx3000");

            //Another cell with width 1000.end point is 4000 (which is 3000+1000)
            tableRtf.Append(@"\cellx4000");


            tableRtf.Append(@"\intbl \cell \row"); //create row

        }

        tableRtf.Append(@"\pard");

        tableRtf.Append(@"}");

        this.misc_tb.Rtf = tableRtf.ToString(); 

现在我想知道如何将文本放在标题和每个单元格中。

你有想法吗?

4

5 回答 5

0

如果你想动态插入文本,你应该声明DataTable,例如:DataTable dt,你可以用dt动态改变数据。每次更改后,您应该渲染到 tableRtf 并将该 tableRtf 设置为 misc_tb.Rtf。以其他方式,您应该定义单元格位置(使用类似 IndexOf 方法)并在该位置插入文本。

更新: 也许这些链接会帮助你:

http://space.dl.sourceforge.net/project/netrtfwriter/netrtfwriter/latest%20-%200.9.1/RtfWriter_V0.9.1.zip

http://www.codeproject.com/Articles/11306/NRTFTTree-A-class-library-for-RTF-processing-in-C

在 RtfTable 类中有 cell(int row, int col) 方法,肯定会有所帮助

于 2012-09-24T11:34:45.243 回答
0

在您的代码中添加 AppendLine 以添加文本。我希望它会工作

tableRtf.Append(@"\cellx1000").AppendLine(" ID");       
tableRtf.Append(@"\cellx2000").AppendLine(" First Name");         
tableRtf.Append(@"\cellx3000").AppendLine(" Lastname");       
tableRtf.Append(@"\cellx4000").AppendLine(" Salary");
于 2015-07-05T08:29:18.557 回答
0

加上行中的数据

                StringBuilder tableRtf = new StringBuilder();

                tableRtf.Append(@"{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}");

                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {   

                    tableRtf.Append(@"\trowd");
                    tableRtf.Append(@"\cellx2500" + "  " + ds.Tables[0].Rows[j]["caption"].ToString());
                    tableRtf.Append(@"\intbl\cell");
                    tableRtf.Append(@"\cellx10000\intbl\cel");
                    tableRtf.Append("   "+ ds2.Tables[0].Rows[k][j].ToString() + @"\intbl\clmrg\cell\row");

                }

                tableRtf.Append(@"\pard");
                tableRtf.Append(@"}");                    
                richTextBox2.Rtf = tableRtf.ToString();
于 2016-09-07T08:31:37.383 回答
0

我们可以用硬编码数据填充单元格,如下 3 行填充数据:

      tableRtf.Append(@"\intbl   1" + @"\cell    Raj" + @"\cell    Bangalore" + @"\cell    India" + @"\row");
      tableRtf.Append(@"\intbl   2" + @"\cell    Peter" + @"\cell    Mumbai" + @"\cell   India" + @"\row");
      tableRtf.Append(@"\intbl   3" + @"\cell    Chris" + @"\cell    Delhi"+ @"\cell   India" + @"\row");

也可以在循环中将 DataTable 数据插入 RichTextBox 表,对于这些示例,请参阅链接Add text in a created table in a richtextbox

于 2016-08-21T06:15:02.450 回答
0

您可以将文本放在循环的最后一行,即:

tableRtf.Append(@"\intbl \cell \row");

在提到的示例中,每行中有 3 个单元格。所以,你应该把你的文本放在 \intbl 和 \row 之间。例如:

tableRtf.Append(@"\intbl  MyText1 \cell  MyText2 \cell  MyText3 \cell \row");
于 2016-04-01T19:54:59.670 回答