待宵

导航

NPOI处理Word文本中上下角标

读取Word文档,并将文本转成html标签,后面发现,经常有带上下角标的内容,于是一并处理了。

核心在于对XWPFRun对象中Subscript属性的处理。

 1         /// <summary>
 2         /// 读取Word,并识别文本中的上下角标
 3         /// </summary>
 4         /// <param name="fileName"></param>
 5         /// <returns></returns>
 6         public static string ReadWordTextExWithSubscript(string fileName)
 7         {
 8 
 9             string fileText = string.Empty;
10             StringBuilder sbFileText = new StringBuilder();
11 
12             #region 打开文档
13             XWPFDocument document = null;
14             try
15             {
16                 using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
17                 {
18                     document = new XWPFDocument(file);
19                 }
20             }
21             catch (Exception e)
22             {
23                 throw e;
24             }
25             #endregion
26             //正文段落
27             foreach (XWPFParagraph paragraph in document.Paragraphs)
28             {
29                 //获取段楼中的句列表
30                 IList<XWPFRun> runsLists = paragraph.Runs;
31 
32                 sbFileText.Append("<p>");
33                 foreach (XWPFRun run in runsLists)
34                 {
35                     switch (run.Subscript)
36                     {
37                         case VerticalAlign.BASELINE:
38                             sbFileText.Append(run.Text);
39                             break;
40                         //上角标
41                         case VerticalAlign.SUPERSCRIPT:
42                             sbFileText.Append("<sup>" + run.Text + "</sup>");
43                             break;
44                         //下角标
45                         case VerticalAlign.SUBSCRIPT:
46                             sbFileText.Append("<sub>" + run.Text + "</sub>");
47                             break;
48                         default:
49                             sbFileText.Append(run.Text);
50                             break;
51                     }
52                     
53                 }
54                 sbFileText.AppendLine("</p>");
55             }
56             fileText = sbFileText.ToString();
57 
58             return fileText;
59         }

Word文档:

输出:

<p>测试<sup>上</sup><sub>下</sub>ok。</p>
<p>CO<sub>2</sub></p>
<p>面积约6000km<sup>2</sup></p>

Html预览:

 

posted on 2018-09-02 19:27  待宵  阅读(1007)  评论(0编辑  收藏  举报