c#中,如何实现在窗体中打开一个word文件??

qinzhiyang 2007-07-03 10:26:15
不需要word的工具条什么的,只需要文档内容就可以。能实现吗?
...全文
1303 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuhuiITren 2007-07-05
  • 打赏
  • 举报
回复
mark
qinzhiyang 2007-07-05
  • 打赏
  • 举报
回复
TO:zxkid
那么word文档中的图片怎么办?
zxcayumi 2007-07-05
  • 打赏
  • 举报
回复
关注
wwwiii520 2007-07-05
  • 打赏
  • 举报
回复
ref oPath,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt
我想一下``這是什么意思```為什么要寫這么多個重復的``??
lengshuangzi 2007-07-05
  • 打赏
  • 举报
回复
能实现,WEB WIN 中都可以
菜单和Word时间都可以自定义
不过你的分太少了......
qinzhiyang 2007-07-03
  • 打赏
  • 举报
回复
TO:QH_HQ(非正派)
谢谢你,不过我要的效果不是用word打开doc文档,而是要把doc文档的内容在窗体中显示就可以。
你还有什么办法吗?
上面几种方法我也试过了,但是都是用word打开。
如果用流的方式读取文档内容,我就是不知道,读出来的内容应该写在什么容器里才能显示呢?
QH_HQ 2007-07-03
  • 打赏
  • 举报
回复
忘了 告诉你 加上命名空间

using System.Diagnostics;
QH_HQ 2007-07-03
  • 打赏
  • 举报
回复

楼主 是不是 打开运行 就可以了

我能 提供一个方法 .. 可以打开 任何一个文件...代码非常简单

首先打开一个文件 对话框,随便选择一个文件 也包括word
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
运行该文件
try
{
if(textBox1.Text.Length != 0)
{
myProcess = Process.Start(textBox1.Text);
myProcess.WaitForExit();
}
}
catch (Exception ee)
{
richTextBox1.Text += "异常:"+ee.ToString()+"\n";
}
casablancaliu 2007-07-03
  • 打赏
  • 举报
回复
能不能直接读出来啊
F15Eagle 2007-07-03
  • 打赏
  • 举报
回复
<asp:HyperLink id="HyperLink1" style="Z-INDEX: 104; LEFT: 88px; POSITION: absolute; TOP: 304px" runat="server" NavigateUrl="/我的文件夹A/我的文件夹B/我的文件A.doc">我的文件A</asp:HyperLink>
楼主用这个就可以了,还可以指定这个路径。
HyperLink1.NavigateUrl="/我的文件夹A/我的文件夹B/我的文件B.doc";
qinzhiyang 2007-07-03
  • 打赏
  • 举报
回复
补充一下,我做的是WindowsApplication,不是web。
adaptcw 2007-07-03
  • 打赏
  • 举报
回复
http://www.lihuasoft.net/article/show.php?id=2815
还可以参考这篇文章,:)
adaptcw 2007-07-03
  • 打赏
  • 举报
回复
1.首先在方案中添加引用,选取com组件,选择Microsoft Word 10.0 object library.(我安装的是office xp)。
2.在程序的开头引用部分,添加如下语句:
using Word=Microsoft.Office.Interop.Word;
using System.Reflection;
3.定义如下变量,并启动word程序:
Word._Application oWapp;
Word._Document oDoc;
object oPath=文件路径;
object misOpt = System.Reflection.Missing.Value;

oWapp=new Word.Application();
oWapp.Visible=true;

oDoc=(Word._Document)(oWapp.Documents.Open(ref oPath,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt));
注:这段代码是C#的实现,各个参数的含义参看Office xp中的VBA帮助;oPath代表了文件路径,通过C#的装箱操作,可以直接将代表路径的字符串赋给它;到微软的网站上下载 Office XP Primary Interop Assemblies,它使得Office xp 的组件成为受托管的代码,且其使用更为方便。


这也是转贴别人的,说明一下
F15Eagle 2007-07-03
  • 打赏
  • 举报
回复
能,做链接直接打开
zxkid 2007-07-03
  • 打赏
  • 举报
回复
通过COM将doc转成rtf, 然后在RichTextbox加载
hjs1573 2007-07-03
  • 打赏
  • 举报
回复
我在 网上也 没有看到 什么好的办法

都是先把 WORD 转换为其他形式的...
qinzhiyang 2007-07-03
  • 打赏
  • 举报
回复
其实我只要显示,不要设置,还有什么好办法?
QH_HQ 2007-07-03
  • 打赏
  • 举报
回复
==============
接上去
=========
调用方法:
CCWordApp test;
test = new CCWordApp();
test.Open(fi.FullName);
//这里为什么是2不是1呢?没整明白
if (test.Count < 2)
{
test.GoToTheEnd();
test.InsertText("略");
test.Save();
Content.Text += "处理了下面的文件:" + fi.Name + "\r\n";
}
else
{
Content.Text += "略过了下面的文件:" + fi.Name + "\r\n";
}

test.Quit();

QH_HQ 2007-07-03
  • 打赏
  • 举报
回复
接 上去 :
=================

public void SetFont()
{
oWordApplic.Selection.Font.Bold = 0;
oWordApplic.Selection.Font.Italic = 0;
oWordApplic.Selection.Font.Subscript = 0;

}

public void SetFontName(string strType)
{
oWordApplic.Selection.Font.Name = strType;

}

public void SetFontSize(int nSize)
{
oWordApplic.Selection.Font.Size = nSize;

}

public void InsertPagebreak()
{

object pBreak = (int)Word.WdBreakType.wdPageBreak;
oWordApplic.Selection.InsertBreak(ref pBreak);
}



public void GotoBookMark(string strBookMarkName)
{

object missing = System.Reflection.Missing.Value;

object Bookmark = (int)Word.WdGoToItem.wdGoToBookmark;
object NameBookMark = strBookMarkName;
oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
}

public void GoToTheEnd()
{

object missing = System.Reflection.Missing.Value;
object unit;
unit = Word.WdUnits.wdStory;
oWordApplic.Selection.EndKey(ref unit, ref missing);

}
public void GoToTheBeginning()
{

object missing = System.Reflection.Missing.Value;
object unit;
unit = Word.WdUnits.wdStory;
oWordApplic.Selection.HomeKey(ref unit, ref missing);

}

public void GoToTheTable(int ntable)
{

object missing = System.Reflection.Missing.Value;
object what;
what = Word.WdUnits.wdTable;
object which;
which = Word.WdGoToDirection.wdGoToFirst;
object count;
count = 1;
oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
oWordApplic.Selection.Find.ClearFormatting();

oWordApplic.Selection.Text = "";


}

public void GoToRightCell()
{

object missing = System.Reflection.Missing.Value;
object direction;
direction = Word.WdUnits.wdCell;
oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing);
}

public void GoToLeftCell()
{

object missing = System.Reflection.Missing.Value;
object direction;
direction = Word.WdUnits.wdCell;
oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing);
}

public void GoToDownCell()
{

object missing = System.Reflection.Missing.Value;
object direction;
direction = Word.WdUnits.wdLine;
oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing);
}

public void GoToUpCell()
{

object missing = System.Reflection.Missing.Value;
object direction;
direction = Word.WdUnits.wdLine;
oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);
}
public void InsertPageNumber(string strType, bool bHeader)
{
object missing = System.Reflection.Missing.Value;
object alignment;
object bFirstPage = false;
object bF = true;
switch (strType)
{
case "Center":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment =

Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
break;
case "Right":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment =

Word.WdPageNumberAlignment.wdAlignPageNumberRight;
break;
case "Left":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment, ref bFirstPage);
break;
}

}

}
}
QH_HQ 2007-07-03
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;

namespace ControlWord
{

public class CCWordApp
{
private Word.ApplicationClass oWordApplic;// a reference to Word application
private Word.Document oDoc;// a reference to the document

//用于读取打开Word的字符数
public int Count = 0;

public CCWordApp()
{

oWordApplic = new Word.ApplicationClass();
}


public void Open(string strFileName)
{
object fileName = strFileName;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;

oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing);

Count = oDoc.Characters.Count;
oDoc.Activate();
}

// 功能:WORD退出


public void Quit()
{
object missing = System.Reflection.Missing.Value;
oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
}

public void Save()
{
oDoc.Save();
}

public void SaveAs(string strFileName)
{
object missing = System.Reflection.Missing.Value;
object fileName = strFileName;

oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref

missing);
}


public void SaveAsHtml(string strFileName)
{
object missing = System.Reflection.Missing.Value;
object fileName = strFileName;
object Format = (int)Word.WdSaveFormat.wdFormatHTML;
oDoc.SaveAs(ref fileName, ref Format, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref

missing);
oDoc.Close(ref missing, ref missing, ref missing);

}
public void CopyAll()
{
oWordApplic.Selection.WholeStory();
oWordApplic.Selection.Copy();

}

public void PasetAll()
{

oWordApplic.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);


}
public void Clear()
{

object Unit = (int)Word.WdUnits.wdCharacter;
object Count = 1;
oWordApplic.Selection.WholeStory();
oWordApplic.Selection.Delete(ref Unit, ref Count);
}


public void InsertText(string strText)
{
oWordApplic.Selection.TypeText(strText);
}

public void InsertLineBreak()
{
oWordApplic.Selection.TypeParagraph();
}
public void InsertLineBreak(int nline)
{
for (int i = 0; i < nline; i++)
oWordApplic.Selection.TypeParagraph();
}


public void SetAlignment(string strType)
{
switch (strType)
{
case "Center":
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
break;
case "Left":
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
break;
case "Right":
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
break;
case "Justify":
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
break;
}

}



public void SetFont(string strType)
{
switch (strType)
{
case "Bold":
oWordApplic.Selection.Font.Bold = 1;
break;
case "Italic":
oWordApplic.Selection.Font.Italic = 1;
break;
case "Underlined":
oWordApplic.Selection.Font.Subscript = 0;
break;
}

}

111,092

社区成员

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

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

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