C#中显示Word内容,无法显示原有格式

lhw7791086 2014-08-06 05:26:48
添加引用Microsoft Office Interop.Word 12.0.0.0;
string path = openFileDialog1.FileName;
Word.ApplicationClass Word_App = new Word.ApplicationClass();
object fileobj = path;
object nullobj = System.Reflection.Missing.Value;
Word.Document wd = Word_App.Documents.Open(ref fileobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj);
textBox1.Text = wd.Content.Text;
wd.Close(ref nullobj, ref nullobj, ref nullobj);
Word_App.Quit(ref nullobj, ref nullobj, ref nullobj);


显示出来的word内容,没有换行符、表格等格式,请问如何将word的内容按原有格式输出来?
...全文
240 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhw7791086 2014-08-14
  • 打赏
  • 举报
回复
解决了一点点,用richtextbox可以显示部分格式
lhw7791086 2014-08-11
  • 打赏
  • 举报
回复
引用 4 楼 Z65443344 的回复:
要显示格式,而不是单纯导入文本,还是用word控件直接显示为好
前辈可否详细说明一点?什么word控件?我没有找到额。。。
於黾 2014-08-08
  • 打赏
  • 举报
回复
要显示格式,而不是单纯导入文本,还是用word控件直接显示为好
eeell_2423891094 2014-08-08
  • 打赏
  • 举报
回复
输出Word数据到文本文件显示 private Word.Application G_wa;//定义Word应用程序字段 private object G_missing = //定义G_missing字段并添加引用 System.Reflection.Missing.Value; private OpenFileDialog G_OpenFileDialog;//定义打开文件对话框字段 private FolderBrowserDialog G_FolderBrowserDailog;//定义浏览文件夹对话框字段 private void btn_Get_Click(object sender, EventArgs e) { btn_Get.Enabled = false;//启用创建按钮 ThreadPool.QueueUserWorkItem(//开始线程池 (pp) =>//使用lambda表达式 { G_wa = new Microsoft.Office.Interop.Word.Application();//创建应用程序对象 object P_OpenFileDialog = //创建object对象 G_OpenFileDialog.FileName; Word.Document P_Document = G_wa.Documents.Open(//打开Word文档 ref P_OpenFileDialog, ref G_missing, ref G_missing, ref G_missing , ref G_missing, ref G_missing, ref G_missing, ref G_missing , ref G_missing, ref G_missing, ref G_missing, ref G_missing , ref G_missing, ref G_missing, ref G_missing, ref G_missing); Word.Range P_Range = P_Document.Range( ref G_missing, ref G_missing); string P_Str = P_Range.Text; string G_str_path = string.Format(//计算文件保存路径 @"{0}\{1}", G_FolderBrowserDailog.SelectedPath, DateTime.Now.ToString("yyyy年M月d日h时m分s秒fff毫秒") + ".txt"); using (StreamWriter P_StreamWrite = new StreamWriter(//创建StreamWriter对象 G_str_path, true)) { P_StreamWrite.Write(P_Str);//向文件中写入字符串 P_StreamWrite.Flush();//将信息写入基础流 } ((Word._Document)P_Document).Close(ref G_missing, ref G_missing, ref G_missing); ((Word._Application)G_wa.Application).Quit(//退出应用程序 ref G_missing, ref G_missing, ref G_missing); this.Invoke(//调用窗体线程 (MethodInvoker)(() =>//使用lambda表达式 { Clipboard.Clear();//清空剪切板 MessageBox.Show(//提示已经创建Word "成功创建文本文件!", "提示!"); btn_Get.Enabled = true;//停用创建按钮 })); }); }
lhw7791086 2014-08-07
  • 打赏
  • 举报
回复
引用 1 楼 zhoumeiwen 的回复:
显示在textbox里?这基本做不到吧,或者做到也及其复杂
那请问在哪里可以比较好的显示?
周美文 2014-08-07
  • 打赏
  • 举报
回复
显示在textbox里?这基本做不到吧,或者做到也及其复杂

110,535

社区成员

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

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

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