NPOI的XWPFParagraph对象中,是无法直接读取段落编号的,然而可以读取的是编号的样式名称(GetNumFmt),编号分组ID(GetNumID),编号样式(NumLevelText)等。具体如下:

/*
* 若干格式信息
* GetNumFmt: decimal, GetNumID: 1, GetNumIlvl: 0, NumLevelText: %1. => 1.
* GetNumFmt: decimal, GetNumID: 4, GetNumIlvl: 0, NumLevelText: %1) => 1)
* GetNumFmt: chineseCountingThousand, GetNumID: 2, GetNumIlvl: 0, NumLevelText: (%1) => (一)
* GetNumFmt: chineseCountingThousand, GetNumID: 3, GetNumIlvl: 0, NumLevelText: %1、 => 一、
* GetNumFmt: upperLetter, GetNumID: 5, GetNumIlvl: 0, NumLevelText: %1. => A.
* GetNumFmt: decimal, GetNumID: 6, GetNumIlvl: 0, NumLevelText: %1、 => 1、
*/

若干格式信息

于是封装了段落编号的处理类,几个关键点:

1、考虑频繁调用,使用单例。

2、依照NumLevelText内容替换编号的样式

3、编号分组发生变化时,编号要重置为1,采用字典记录

4、汉字、字母统一处理为数字编号

5、读取一个新Word时,字典内容要清空

段落处理类:

     /// <summary>
/// 段落处理类
/// Author: Matsuyoi
/// </summary>
class ParagraphNumHandle
{
#region 封装为单例
private static ParagraphNumHandle singleton = null;
public static ParagraphNumHandle GetInstance()
{
if (singleton == null)
singleton = new ParagraphNumHandle();
//获取单例后重置一次变量
singleton.Reset();
return singleton;
}
#endregion
//Num字典
private Dictionary<string, int> _Count;
private ParagraphNumHandle()
{
_Count = new Dictionary<string, int>();
}
/// <summary>
/// 重置
/// </summary>
private void Reset()
{
//清空字典
_Count.Clear();
}
/// <summary>
/// 处理段落中的编号,汉字与字母编号统一转为数字编号
/// </summary>
/// <param name="paragraph"></param>
/// <returns></returns>
public string GetParagraphNum(XWPFParagraph paragraph)
{
string result = "";
//若无编号格式信息,则返回空
if (string.IsNullOrEmpty(paragraph.GetNumFmt()) ||
string.IsNullOrEmpty(paragraph.GetNumID()) ||
string.IsNullOrEmpty(paragraph.NumLevelText))
{
return result;
} string key = paragraph.GetNumID() ?? "";
if (!_Count.ContainsKey(key))
{
//编号从1开始
_Count.Add(key, );
}
else
{
_Count[key] += ;
} string fmt = paragraph.NumLevelText.Replace("%1", "{0}");
result = string.Format(fmt, _Count[key].ToString()) + " ";
return result;
}
}

调用方式:

//段落编号处理
ParagraphNumHandle pnc = ParagraphNumHandle.GetInstance();
//正文段落
foreach (XWPFParagraph paragraph in document.Paragraphs)
{
//获取段楼中的编号
string num = pnc.GetParagraphNum(paragraph); ... }

延续上一篇《NPOI处理Word文本中上下角标》的示例,完整代码如下:

/// <summary>
/// 读取Word,并识别文本中的上下角标
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string ReadWordTextExWithSubscript(string fileName)
{ string fileText = string.Empty;
StringBuilder sbFileText = new StringBuilder(); #region 打开文档
XWPFDocument document = null;
try
{
using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
document = new XWPFDocument(file);
}
}
catch (Exception e)
{
throw e;
}
#endregion
//段落编号处理
ParagraphNumHandle pnc = ParagraphNumHandle.GetInstance();
//正文段落
foreach (XWPFParagraph paragraph in document.Paragraphs)
{
//获取段楼中的句列表
IList<XWPFRun> runsLists = paragraph.Runs;
//获取段楼中的编号
string num = pnc.GetParagraphNum(paragraph);
sbFileText.Append("<p>" + num);
foreach (XWPFRun run in runsLists)
{
switch (run.Subscript)
{
case VerticalAlign.BASELINE:
sbFileText.Append(run.Text);
break;
//上角标
case VerticalAlign.SUPERSCRIPT:
sbFileText.Append("<sup>" + run.Text + "</sup>");
break;
//下角标
case VerticalAlign.SUBSCRIPT:
sbFileText.Append("<sub>" + run.Text + "</sub>");
break;
default:
sbFileText.Append(run.Text);
break;
} }
sbFileText.AppendLine("</p>");
}
fileText = sbFileText.ToString(); return fileText;
}

测试:

Word文档:

输出:

<p>1. 第一段</p>
<p>2. 第二段</p>
<p>1) 第三段</p>
<p>(1) 第四段</p>
<p>(2) 第五段</p>
<p>1、 第六段</p>
<p>2、 第七段</p>
<p>1. 第八段</p>
<p>1、 第九段</p>
<p>2、 第十段</p>
<p>测试<sup>上</sup><sub>下</sub>ok。</p>
<p>CO<sub>2</sub></p>
<p>面积约6000km<sup>2</sup></p>

Html预览:

最新文章

  1. 有关binlog的那点事(二)(mysql5.7.13)
  2. 自动获取MyEcilipse注册名和注册码的方法
  3. mxnet的python包导入的前前后后
  4. 打开 Mac OS X 隐藏的用户资源库文件夹的方法
  5. iOS 检测有没有安装其它应用 和ios9下要注意的地方
  6. css匹配原理与优化
  7. 【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)
  8. (转)一步一步学习PHP(5)——类和对象
  9. 关于The requested PHP extension ext-pdo_sqlite * is missing from your system. Install or enable PHP&#39;s pdo_sqlite extension.的解决
  10. php json_decode返回失败的排查
  11. C# Winform窗口之间传值的多种方法浅析(转)
  12. .net core 使用阿里云短信发送SMS
  13. LVM备份(3)- pg_dumpall
  14. JS实现日期选择
  15. Log4Net 生成多个文件、文件名累加解决方法
  16. 关于Linux安装Mono 3.4的bug
  17. [USACO07JAN]Cow School
  18. 点击导出table表格
  19. SpringBoot-Jar打包方式
  20. 将sqllite3数据库迁移到mysql

热门文章

  1. Windows 下Jenkins进程误删,如何重启
  2. 【VS2019】F12跳转到源码
  3. iOS坐标转换失败?
  4. 使用http服务提供yum源
  5. Springboot项目启动后访问不到Controller
  6. jacoco覆盖率工具测试及性能分析
  7. 学习mysql,记录下常用的命令行语句
  8. jquery 判断浏览器版本
  9. 52. N-Queens II N皇后II
  10. recyclerview 主活动里监听点击事件