c#怎么实现读取word文档
1个回答
展开全部
引用spire.doc.dll
提取word文档的文字到txt文件:
using Spire.Doc;
using Spire.Doc.Documents;
using System.IO;
namespace ReadText
{
class Program
{
static void Main(string[] args)
{
//创建Document对象
Document doc = new Document();
//加载Word文档
doc.LoadFromFile("input.docx");
//创建StringBuilder对象
StringBuilder sb = new StringBuilder();
//遍历Word文档中每一个section
foreach (Section section in doc.Sections)
{
//遍历section中每一个段落
foreach (Paragraph paragraph in section.Paragraphs)
{
//提取Word的每一行文字存入StringBuilder对象
sb.AppendLine(paragraph.Text);
}
}
//写入txt文档
File.WriteAllText("output.txt", sb.ToString());
}
}
}
提取word文档中图片,保存到本地:
using Spire.Doc;
using Spire.Doc.Fields;
using Spire.Doc.Documents;
namespace ReadImage
{
class Program
{
static void Main(string[] args)
{
//创建Document对象并加载Word文档
Document doc = new Document();
doc.LoadFromFile(@"Image.doc");
int index = 0;
//遍历Word文档中每一个section
foreach (Section section in doc.Sections)
{
//遍历section中的每个段落
foreach (Paragraph paragraph in section.Paragraphs)
{
//遍历段落中的每个DocumentObject
foreach (DocumentObject docObject in paragraph.ChildObjects)
{
//判断DocumentObject是否为图片
if (docObject.DocumentObjectType == DocumentObjectType.Picture)
{
//保存图片到指定路径并设置图片格式
DocPicture picture = docObject as DocPicture;
String imageName = String.Format(@"images\Image-{0}.png", index);
picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);
index++;
}
}
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |