我将word内容复制到richtextbox而没有完美地丢失格式,但现在我正在编辑richtextbox中的内容。
现在我想在 C# 中使用 WinForms 将 Richtextbox 内容导出到 Word 文档而不丢失任何格式。你怎么做呢?
WordApp.ActiveDocument.SaveAsQuickStyleSet("abc.doc");
Range rng = WordApp.ActiveDocument.Range(0, 0);
for (int i = 0; i < _dgvrow.Cells.Count; ++i)
{
// add code to loop thru controls and TypeText into word document
Label lb = (Label)this.Controls["lblfield" + (i+1).ToString()];
rng.Text += lb.Text;
rng.Select();
Control ctrl = this.Controls["txtfield" + (i+1).ToString()];
if(ctrl is RichTextBox)
{
RichTextBox rb = (RichTextBox)ctrl;
rng.Text += rb.Text + Environment.NewLine;
rng.Select();
}
else if (ctrl is TextBox)
{
TextBox rb = (TextBox)ctrl;
rng.Text += rb.Text + Environment.NewLine;
rng.Select();
}
}