NPOI处理Word文本中上下角标,调用腾讯云API的实例

2019-10-23 06:08 来源:未知

演示数组为:

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

       //获取时间戳 .net framework
            /*
            DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 
            int time = (int)(DateTime.Now - dt).TotalSeconds;
            String Timestamp = time.ToString();
            */
            // .net core 获取时间戳
            DateTime dt = new DateTime(1970, 1, 1,0, 0, 0, DateTimeKind.Utc);
            int time = (int)(DateTime.Now.AddHours(-8) - dt).TotalSeconds;
            String Timestamp = time.ToString();
            //随机正整数,用于防止重放攻击
            Random rd = new Random();
            int rd_i = rd.Next();
            String nonce = Convert.ToString(rd_i);
            //SecretId
            String SecretId = "";
            //参数(用于编码)
            String PostStr = string.Format("Action=DescribeLVBChannelList&Nonce={0}&Region=bj&SecretId={1}&Timestamp={2}", nonce, SecretId, Timestamp);
            //地址
            String url = "https://live.api.qcloud.com/v2/index.php";
            //编码
            UTF8Encoding enc = new UTF8Encoding();
            String qm = "POSTlive.api.qcloud.com/v2/index.php"  + "?" + PostStr;
            byte[] dataToHash = enc.GetBytes(qm);
            /*
            var sha1 = SHA1.Create();
            var result = sha1.ComputeHash(dataToHash);
            */  
            HMACSHA1 hmac = new HMACSHA1()
            {
                Key = enc.GetBytes("")
            };
            var result = hmac.ComputeHash(dataToHash);
            string Signature = Convert.ToBase64String(result);
            //完整参数
            var completeUrl = string.Format("Action=DescribeLVBChannelList&Nonce={0}&Region=bj&SecretId={1}&Signature={2}&Timestamp={3}", nonce, SecretId, Signature, Timestamp);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ProtocolVersion = HttpVersion.Version10;
            byte[] data = Encoding.UTF8.GetBytes(completeUrl);
            request.ContentLength = data.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            HttpWebResponse response = null;
            int httpStatus = 200;
            string content;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                httpStatus = (int)response.StatusCode;
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                content = reader.ReadToEnd();
            }
            catch (WebException e)
            {
                response = (HttpWebResponse)e.Response;
                httpStatus = (int)response.StatusCode;
                using (Stream errData = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(errData))
                    {
                        content = reader.ReadToEnd();
                    }
                }
            }

  $a = array(9,3,5,8,2,7);  //下标为0,1,2,3,4,5

核心在于对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         }

  从数组的左边开始,依次两两比较相邻的2个数据的大小,如果发现左边的比右边的大,则将他们进行交换。这样进行“一趟”之后,必然可以确定最大的一个数据放在最右边。

Word文档:

  按此方式,对“剩余的数据”继续进行下一趟,则必然可以确定这些剩余数据的最大值放在剩余位置的最右边。

图片 1

演示:

输出:

原始数组: 9 3 5 8 2 7
第一趟后: 3 5 8 2 7 9
第二趟后: 3 5 2 7 8 9
第三趟后: 3 2 5 7 8 9
第四趟后: 2 3 5 7 8 9
第五趟后: 2 3 5 7 8 9

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

 

Html预览:

TAG标签:
版权声明:本文由澳门金莎娱乐网站发布于澳门金莎唯一指定官网,转载请注明出处:NPOI处理Word文本中上下角标,调用腾讯云API的实例