openxml wordprocessing扩展属性

时间:2018-01-02 11:31:51

标签: html attributes tags openxml

我正在使用" AlternativeChunck"

将HTML表格转换为openxml
AlternativeFormatImportPart AFIT = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, altChunkId);
AFIT.FeedData(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(myHTML)));
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
mainPart.Document.Body.Append(altChunk);

表格单元格在转换为openxml表后需要一些属性。至少我需要一个ID标记或属性来在转换为XML后查找每个单元格并根据该ID执行一些操作。如何在html中定义ID转换后的XML标签?

1 个答案:

答案 0 :(得分:1)

我做了两个假设:

  1. 您不是自己构建HTML,否则您将在构建HTML时向每个单元格添加id属性。
  2. HTML是格式良好的XML。
  3. 基于上述假设,我会在将XML附加到文档正文之前使用XSLT或LINQ to XML预处理HTML。如果执行LINQ to XML,请尝试以下方法:

        public static string PreProcessHTML(string html)
        {
            string rtn = "";
    
            System.IO.StringReader rdr = new StringReader(html);
            XElement root = XElement.Load(rdr);
            var tds = root.Descendants("td");
            int i = 0;
            foreach (XElement td in tds)
            {
                td.SetAttributeValue("id", "id" + i.ToString());
                i++;
            }
    
            rtn = root.ToString(SaveOptions.None);
            return rtn;
        }
    
相关问题