C# 读取并显示word文档中的内容

我想将信道模型的介绍以及分析的情况,都在C#的程序设计中进行实现。然后就想到了将word文档直接显示到窗体中,并且有滚动条可以拉动进行查看。搜集了一些资料之后找到了合适的方法——利用RichTextBox打开一个有文字格式和图片的Word文档。

1.添加引用

要加入word文档,就需要在”解决方案资源管理器”的”引用”中加入”Microsoft Word 12.0 Object Library ”(我的是12.0,不同的VS版本会有不同)。然后查看“解决方案资源管理器”的“引用”,就会添加进去Microsoft.Office.Interop.Word。

对该引用修改属性:右键->属性,嵌入互操作类型:false。

在程序的引用中加入:

using Word = Microsoft.Office.Interop.Word;     

2. word文档读取函数

添加 richTextBox 控件,然后在窗体的load事件中写入代码:

private void Form3_Load(object sender, EventArgs e)
{
     //word文档的文件路径与文件名
     string fullPath = @"C:\Users\Administrator\Desktop\1.docx";
     OpenWord(fullPath);
}
            
public void OpenWord(string fileName)
{
        Word.Application app = new Word.Application();//可以打开word
        Word.Document doc = null;      //需要记录打开的word

        object missing = System.Reflection.Missing.Value;
        object File = fileName;
        object readOnly = false;//不是只读
        object isVisible = true;

        object unknow = Type.Missing;
   
    try
        {
            doc = app.Documents.Open(ref File, 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, ref missing);

            doc.ActiveWindow.Selection.WholeStory();//全选word文档中的数据
            doc.ActiveWindow.Selection.Copy();//复制数据到剪切板
            richTextBox.Paste();//richTextBox粘贴数据
            //richTextBox1.Text = doc.Content.Text;//显示无格式数据
        }
        finally
        {
            if (doc != null)
            {
                doc.Close(ref missing, ref missing, ref missing);
                doc = null;
            }

            if (app != null)
            {
                app.Quit(ref missing, ref missing, ref missing);
                app = null;
            }
        }   
}   

3. 使用OpenFileDialog 获取word文档:

除了上面的那种直接写入文件路径与文件名的方式(由于我要用的是那种方式,就先介绍了),还有一种是利用 OpenFileDialog 获取word文档,它可以弹出一个文件选择框,然后找到文件后点确定,也可以用于加载word文件。

在Form3的Load事件中写入代码:

OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "word文件|*.docx";
object fileName = 0;
if (dlg.ShowDialog() == DialogResult.OK)
{
     fileName = dlg.FileName;
}
string str = (string)fileName;
OpenWord(str);

这个的运行结果是弹出这样的选择框,可以选择word类型的文档:
在这里插入图片描述
4. 运行结果:

可以显示图片、文字以及表格,但是格式有点乱,我还要再找找原因。
在这里插入图片描述

  • 17
    点赞
  • 98
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
可以使用C#的OpenXML SDK来读取Word文档指定书签标记的图表。以下是大致的步骤: 1. 导入OpenXML SDK的命名空间。例如:`using DocumentFormat.OpenXml.Packaging;`和`using DocumentFormat.OpenXml.Wordprocessing;`。 2. 打开Word文档,并使用`WordprocessingDocument`类加载文档。例如:`WordprocessingDocument doc = WordprocessingDocument.Open("document.docx", true);`。 3. 获取文档的书签标记。可以使用`BookmarkStart`和`BookmarkEnd`元素来识别书签标记。例如,使用以下代码获取名为“bookmark1”的书签标记: ```csharp BookmarkStart bookmarkStart = doc.MainDocumentPart.Document.Body.Descendants<BookmarkStart>().Where(b => b.Name == "bookmark1").FirstOrDefault(); BookmarkEnd bookmarkEnd = doc.MainDocumentPart.Document.Body.Descendants<BookmarkEnd>().Where(b => b.Id.Value == bookmarkStart.Id.Value).FirstOrDefault(); ``` 4. 使用书签标记的位置信息,获取书签标记所在的段落和图表。例如,使用以下代码获取名为“bookmark1”的书签标记所在的段落和图表: ```csharp Paragraph paragraph = bookmarkStart.Parent as Paragraph; Drawing drawing = paragraph.Elements<Drawing>().FirstOrDefault(); ChartReference chartReference = drawing.Inline.Graphic.GraphicData.Descendants<ChartReference>().FirstOrDefault(); ``` 5. 获取图表数据,并使用相应的库(如OpenXML SDK、Excel Interop或EPPlus)进行分析和处理。例如,使用以下代码获取图表数据: ```csharp ChartPart chartPart = (ChartPart)doc.MainDocumentPart.GetPartById(chartReference.Id); OpenXmlReader reader = OpenXmlReader.Create(chartPart); while (reader.Read()) { if (reader.ElementType == typeof(SeriesText)) { string seriesText = reader.GetText(); // 处理图表数据 } } ``` 请注意,这只是一个简单的示例,实际情况可能需要根据具体的文档和需求进行调整。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值