文章目录

  • 前言
  • 一、办公文档神器Toxy的使用
    • 1.安装对应的包
    • 2.Word文档操作
      • 2.1 普通文档解析
      • 2.2 表格文档解析
    • 3.Excel文件操作
    • 4.PDF文档操作
    • 5.图片文件操作
  • 总结

前言

作为一个开发者对于写代码操作各种办公文件是非常常见的需求,但是操作这些不同办公文件需要安装各种对应的第三方包来处理,下面就来介绍统一的第三方包Toxy,Toxy是一个.NET数据/文本提取框架,类似于Java中的Apache Tika。它支持许多流行的格式,如docx,xlsx,xls,pdf,csv,txt,epub,html等。

Toxy的架构图如下:

如上图所示,该项目对各种文件格式进行底层封装,我们只需专注业务的开发,不需要了解底层细节。

Toxy的网址:https://github.com/nissl-lab/toxy

一、办公文档神器Toxy的使用

Toxy相关对象如下:

  • ToxyDocument - 为文档提取的数据结构
  • ToxySpreadsheet - 为电子表格提取的数据结构
  • ToxyEmail - 为电子邮件提取的数据结构
  • ToxyBusinessCard - 为名片提取的数据结构
  • ToxyDom - 为基于 DOM 的文档提取的数据结构
  • ToxyMetadata - 为其他具有元数据的文件提取的数据结构

1.安装对应的包

Toxy

2.Word文档操作

2.1 普通文档解析


1、ITextParser解析文档

//解析docx文档
using ConsoleTest;
using NUnit.Framework;
using Toxy;ParserContext context = new ParserContext(TestDataSample.GetWordPath("SampleDoc.docx"));
ITextParser parser = ParserFactory.CreateText(context);
string doc = parser.Parse();Assert.IsNotNull(doc);string[] lines = doc.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
Assert.AreEqual(7, lines.Length);
Assert.AreEqual("I am a test document", lines[0]);
Assert.AreEqual("This is page 1", lines[1]);
Assert.AreEqual("I am Calibri (Body) in font size 11", lines[2]);
Assert.AreEqual("\n", lines[3]);
Assert.AreEqual("This is page two", lines[4]);
Assert.AreEqual("It’s Arial Black in 16 point", lines[5]);
Assert.AreEqual("It’s also in blue", lines[6]);Console.ReadLine();


2、IDocumentParser解析文档

//解析docx文档
using ConsoleTest;
using NUnit.Framework;
using Toxy;ParserContext context = new ParserContext(TestDataSample.GetWordPath("SampleDoc.docx"));
IDocumentParser parser = ParserFactory.CreateDocument(context);
ToxyDocument doc = parser.Parse();
Assert.AreEqual(7, doc.Paragraphs.Count);
Assert.AreEqual("I am a test document", doc.Paragraphs[0].Text);
Assert.AreEqual("This is page 1", doc.Paragraphs[1].Text);
Assert.AreEqual("I am Calibri (Body) in font size 11", doc.Paragraphs[2].Text);
Assert.AreEqual("\n", doc.Paragraphs[3].Text);
Assert.AreEqual("This is page two", doc.Paragraphs[4].Text);
Assert.AreEqual("It’s Arial Black in 16 point", doc.Paragraphs[5].Text);
Assert.AreEqual("It’s also in blue", doc.Paragraphs[6].Text);Console.ReadLine();

2.2 表格文档解析


1、IDocumentParser解析文档

//解析docx文档
using ConsoleTest;
using NUnit.Framework;
using Toxy;ParserContext context = new ParserContext(TestDataSample.GetWordPath("simple-table.docx"));
IDocumentParser parser = ParserFactory.CreateDocument(context);
ToxyDocument doc = parser.Parse();
Assert.AreEqual(8, doc.Paragraphs.Count);
Assert.AreEqual("This is a Word document that was created using Word 97 – SR2.  It contains a paragraph, a table consisting of 2 rows and 3 columns and a final paragraph.",doc.Paragraphs[0].Text);
Assert.AreEqual("This text is below the table.", doc.Paragraphs[1].Text);
Assert.AreEqual("Cell 1,1", doc.Paragraphs[2].Text);
Assert.AreEqual("Cell 1,2", doc.Paragraphs[3].Text);
Assert.AreEqual("Cell 1,3", doc.Paragraphs[4].Text);
Assert.AreEqual("Cell 2,1", doc.Paragraphs[5].Text);
Assert.AreEqual("Cell 2,2", doc.Paragraphs[6].Text);
Assert.AreEqual("Cell 2,3", doc.Paragraphs[7].Text);Console.ReadLine();

3.Excel文件操作

后续都已一种方式,不在像word那样了

//解析xlsx文档
using ConsoleTest;
using NUnit.Framework;
using Toxy;ParserContext context = new ParserContext(TestDataSample.GetExcelPath("Employee.xls"));
ISpreadsheetParser parser = ParserFactory.CreateSpreadsheet(context);
ToxySpreadsheet ss = parser.Parse();//获取Excel表格数量与名称
Assert.AreEqual(3, ss.Tables.Count);
Assert.AreEqual("Sheet1", ss.Tables[0].Name);
Assert.AreEqual("Sheet2", ss.Tables[1].Name);
Assert.AreEqual("Sheet3", ss.Tables[2].Name);//获取总行数
Assert.AreEqual(5, ss.Tables[0].Rows.Count);
Assert.AreEqual(0, ss.Tables[1].Rows.Count);
Assert.AreEqual(0, ss.Tables[2].Rows.Count);//获取指定行数
ToxyTable table = ss.Tables[0];
Assert.AreEqual(1, table.Rows[0].RowIndex);
Assert.AreEqual(2, table.Rows[1].RowIndex);
Assert.AreEqual(3, table.Rows[2].RowIndex);
Assert.AreEqual(4, table.Rows[3].RowIndex);
Assert.AreEqual(5, table.Rows[4].RowIndex);//获取总表格数
Assert.AreEqual(1, table.Rows[0].Cells.Count);
Assert.AreEqual(0, table.Rows[1].Cells.Count);
Assert.AreEqual(2, table.Rows[2].Cells.Count);
Assert.AreEqual(2, table.Rows[3].Cells.Count);
Assert.AreEqual(2, table.Rows[4].Cells.Count);//获取指定表格的值
Assert.AreEqual("Employee Info", table.Rows[0].Cells[0].ToString());
Assert.AreEqual(1, table.Rows[0].Cells[0].CellIndex);Console.ReadLine();

4.PDF文档操作

using NUnit.Framework;
using Toxy.Parsers;
using Toxy;string path = TestDataSample.GetPdfPath("Sample1.PDF");
var parser = new PDFDocumentParser(new ParserContext(path));
var result = parser.Parse();
//获取段落数
Assert.AreEqual(1474, result.Paragraphs.Count);
//判断第一段落的文本
Assert.AreEqual("LA MARCHE MONDIALE DES FEMMES : UN MOUVEMENT IRRÉVERSIBLE", result.Paragraphs[0].Text);Console.ReadLine();

5.图片文件操作

//解析jpg图片
using ConsoleTest;
using NUnit.Framework;
using Toxy;string path = Path.GetFullPath(TestDataSample.GetImagePath("sample_sony1.jpg"));
ParserContext context = new ParserContext(path);
IMetadataParser parser = (IMetadataParser)ParserFactory.CreateMetadata(context);
ToxyMetadata x = parser.Parse();
//属性数量
Assert.AreEqual(12, x.Count);
//图片宽模式时间等信息
Assert.AreEqual(2592, x.Get("PhotoHeight").Value);
Assert.AreEqual(95, x.Get("PhotoQuality").Value);
Assert.AreEqual(3872, x.Get("PhotoWidth").Value);
Assert.AreEqual("DSLR-A200", x.Get("Model").Value);
Assert.AreEqual((uint)400, x.Get("ISOSpeedRatings").Value);
Assert.AreEqual(5.6, x.Get("FNumber").Value);
Assert.AreEqual((double)35, x.Get("FocalLength").Value);
Assert.AreEqual((uint)52, x.Get("FocalLengthIn35mmFilm").Value);
Assert.AreEqual(new DateTime(2009, 11, 21, 12, 39, 39), x.Get("DateTime").Value);
Console.ReadLine();

总结

以上是部分文件格式的解析例子,更多功能,大家可以下载源码学习,针对每一种格式,都有非常详细的单元测试例子,看完例子完全不需要文档。

具体支持的文件如下:

【愚公系列】2023年02月 .NET CORE工具案例-办公文档神器Toxy的使用相关推荐

  1. 【愚公系列】2023年02月 .NET CORE工具案例-Lunar日历转换

    文章目录 前言 一.Lunar日历转换 1.安装包 2.相关代码 3.运行 二.用途 1.年历 2.月历 3.佛历 4.道历 5.上班摸鱼 前言 真正的日历产生,大约在一千一百多年前的唐顺宗永贞元年, ...

  2. 【愚公系列】2023年02月 .NET CORE工具案例-Caliburn.Micro的使用基于WPF的改造的MVVM案例

    文章目录 前言 1.Caliburn.Micro是什么 2.Caliburn.Micro的主要功能 一.Caliburn.Micro的使用基于WPF的改造 1.项目介绍 2.安装软件包 3.改造App ...

  3. 【愚公系列】2023年01月 .NET CORE工具案例-基于SqlSugar的多库多表融合查询

    文章目录 前言 一.基于SqlSugar的多库多表融合查询 1.安装包 2.订单表 3.添加连接 3.1 初始化添加 3.2 动态添加 4.查询 4.1 子表对主表(一对一)查询 4.2 主表对子表( ...

  4. 【愚公系列】2023年01月 .NET CORE工具案例-CS-Script脚本执行引擎

    文章目录 前言 一.CS-Script脚本执行引擎 1.安装包 2.具体使用 2.1 CompileMethod 2.2 LoadMethod 2.3 LoadCode 2.4 CompileCode ...

  5. 【愚公系列】2023年01月 .NET CORE工具案例- Magick.NET神级图片和视频操作库

    文章目录 前言 一.Magick.NET的使用 1.安装包 2.图像的操作 2.1 图像读取 2.1 图像大小改变 2.2 图像格式转换 2.3 PDF转换 2.4 添加水印.文本 2.5 图片合并 ...

  6. 【愚公系列】2023年04月 .NET CORE工具案例-二维码生成器QRCoder

    文章目录 前言 一.二维码生成器QRCoder 1.QRCoder是什么 2.安装包 3.普通二维码 3.1 创建二维码 3.2 设置二维码颜色 3.3 带logo的二维码 4.艺术二维码 4.1 创 ...

  7. 【愚公系列】2023年03月 .NET CORE工具案例-基于AntiXssUF的跨脚本XSS中间件

    文章目录 前言 一.AntiXssUF的跨脚本XSS中间件 1.安装包 2.添加依赖注入 3.使用方式 3.1 构造函数使用 3.2 模型绑定器使用 3.3 直接使用 前言 XSS是一种跨站脚本攻击, ...

  8. 【愚公系列】2022年12月 .NET CORE工具案例-滑块验证码和拼图验证功能实现

    文章目录 前言 1.滑块验证码的定义 2.滑块验证码的安全性分析 3.滑块验证码的第三方服务 一.滑动验证码和拼图验证功能实现 1.逻辑分析 2.新建项目 3.配置缓存 4.配置跨域 5.后端源码 6 ...

  9. 【愚公系列】2022年11月 .NET CORE工具案例-.NET 7中的WebTransport通信

    文章目录 前言 1.技术背景 2.QUIC相关概念 3.HTTP/3.0 一.WebTransport 1.WebTransport概念 2.WebTransport在js中的使用 3.WebTran ...

最新文章

  1. Android插件化原理解析——概要
  2. UITextView,UITextField 和UIAlertView 在ios8上 当pop时候出现闪bug
  3. Mozilla考虑支持H.264
  4. JavaScript基于对象编程
  5. Html5学习笔记1 元素 标签 属性
  6. 死亡搁浅运送系统服务器,死亡搁浅订单23寻物系统服务器流程介绍-死亡搁浅订单23寻物系统服务器怎么做_牛游戏网...
  7. optimize 回收表空间的一些说明
  8. 计算机组成原理 王道考研2021 第一章:计算机组成原理概述 -- 计算机硬件的基本组成、认识各个硬件部分
  9. c语言int占几个字节 vc,int类型占几个字节
  10. android fastboot流程,FastBoot的使用步骤
  11. 线程池的几种构造方法及使用的策略
  12. tcpreplay linux,Linux——Tcpreplay
  13. 计算机会计技术特点,会计电算化系统的特点
  14. 1小时1篇文学会用python进行AI修复!
  15. Sa-Token浅谈
  16. 【java简单小项目】勇者斗魔王小游戏
  17. 以太坊解析之二——POA共识过程与一些可能的修改方案
  18. 一文读懂量化数据efinance
  19. 设计一个按照时间片轮转法实现处理机调度的程序
  20. 八数码问题可解性及扩展

热门文章

  1. 图片清晰度“测量” 算法
  2. 阴阳师笔画下载过程中的404
  3. 详细介绍如何自研一款博客搬家功能
  4. 普通本科拿下同花顺和平安offer面试题Java分享,薪资22k
  5. 考研英语单词如何有效记忆?
  6. 牛客21天刷题_day#3
  7. Animoca Brands 冠名赞助了 MotoGP™ 阿拉贡大奖赛,并送出 VIP 体验券
  8. 【笔记分享】无功功率
  9. 湖南省大学生计算机应用竞赛,湖南省第16届大学生计算机程序设计竞赛
  10. 腾讯官宣 | 腾讯策略协作型 AI「绝悟」升级至王者荣耀电竞职业水平