C# 如何把RichTextBox保存成Word文档

ee19870221 2010-12-07 01:42:48
我想把RichTextBox里的内容保存成.doc格式的,不想用.rtf格式的,占的空间太大啦。

有没有高手帮帮忙。

急急急。。。。。。
...全文
570 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-12-07
  • 打赏
  • 举报
回复
richtextbox内部是RTF格式的,直接用richTextBox.LoadFile(strFileName,RichTextBoxStreamType.PlainText)
或者先把word文档装成RTF格式后就可以用LoadFile
http://www.codeproject.com/KB/cs/eRichTextBox.aspx
qin198066 2010-12-07
  • 打赏
  • 举报
回复
我是新手,过来学习一下哟
winner213 2010-12-07
  • 打赏
  • 举报
回复
2楼辛苦,学习了!
如果RickTextBox提供了SaveFile的功能,那么3楼老兄的方法真的是最简单的哈
全栈深入 2010-12-07
  • 打赏
  • 举报
回复
最简单的方法:


string filePath = "D:\\1.doc";
richTextBox1.SaveFile(filePath, RichTextBoxStreamType.RichText);
happyday1799 2010-12-07
  • 打赏
  • 举报
回复
按照上边的方法添加过COM组件后,这是引用:using Word = Microsoft.Office.Interop.Word;

帮你写了个DEMO,这种方法需要你先建立一个WORD文档,你试一下吧:


///打开Word文档,并且返回对象wDoc,wDoc
///
/// 打开Word文档,并且返回对象wDoc,wDoc
///
/// 完整Word文件路径+名称
/// 返回的Word.Document wDoc对象
/// 返回的Word.Application对象
private static void CreateWordDocument(string FileName, ref Word.Document wDoc, ref Word.Application WApp)
{
if (FileName == "") return;
Word.Document thisDocument = null;
Word.FormFields formFields = null;
Word.Application thisApplication = new Word.ApplicationClass();
thisApplication.Visible = true;
thisApplication.Caption = "";
thisApplication.Options.CheckSpellingAsYouType = false;
thisApplication.Options.CheckGrammarAsYouType = false;

Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = true;
Object AddToRecentFiles = false;

Object PasswordDocument = System.Type.Missing;
Object PasswordTemplate = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePasswordDocument = System.Type.Missing;
Object WritePasswordTemplate = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XMLTransform = System.Type.Missing;

try
{
Word.Document wordDoc =
thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
ref NoEncodingDialog, ref XMLTransform);

thisDocument = wordDoc;
wDoc = wordDoc;
WApp = thisApplication;
formFields = wordDoc.FormFields;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}

/// <summary>
/// Word填充数据
/// </summary>
private void WordLoadData()
{
Word.Document wDoc = null;
Word.Application wApp = null;
this.CreateWordDocument(@"e:\新建 Microsoft Office Word 文档.doc", ref wDoc, ref wApp);

wApp.Selection.TypeText(this.richTextBox1.Text); //将richTextBox1的内容添加到新建 Microsoft Office Word 文档.doc
}
happyday1799 2010-12-07
  • 打赏
  • 举报
回复
需要添加COM组件的,这里讲的非常详细:

http://xiaotan.blogbus.com/logs/10019380.html

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧