【问题标题】:OpenXML in Microsoft Excel for CommentsMicrosoft Excel 中的 OpenXML 用于注释
【发布时间】:2019-07-01 07:57:38
【问题描述】:

我正在运行最新的 Office 365 Excel 版本 1901。我已更新到最新的 OpenXml SDK,但无法弄清楚如何以编程方式读取有关线程化 cmets 的信息,因为我看到的只是完整的摘要注释。即使使用最新的 OpenXml Nuget 包。

如果我将 Excel 文档转换为 .zip 文件,我可以看到“threadedComments.xml”文件,其中包含我需要的内容,但不知道如何在 C# .NET 中以编程方式进行。

【问题讨论】:

  • 没有想法?有人必须知道一点 OpenXML
  • 你能附上一个excel文件来玩吗?我猜很多人都有旧版本的 Excel,因此无法生成“threadedComments.xml”。只有旧的 cmets 选项,我认为现在是 Notes。
  • @Nexxas 请查看此链接 (social.msdn.microsoft.com/Forums/sqlserver/en-US/…) 这可以帮助您获得动态 cmets。让我知道这是否有帮助!
  • @VikrantMore 可以使用 openXML 添加/读取 cmets。他想阅读线程化的 cmets,这是 excel 中的一个新功能。
  • 看看这个:2.6.207 CT_ThreadedComments2.6.205 CT_ThreadedComment。它可能会帮助您解决问题。

标签: c# excel office365 openxml threaded-comments


【解决方案1】:

如果您知道 .zip 档案中的确切位置,您可以通过编程方式访问内容:

    static class Program
    {
        static void Main(string[] args)
        {
            using (var archive = ZipFile.OpenRead(args[0]))
            {
                var entry = archive.Entries.Where(_ => _.FullName.Equals("xl/comments1.xml", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                if (entry != null)
                {
                    var stopwatch = new Stopwatch();
                    stopwatch.Start();
                    var data = new List<string>(Decompress(entry.Open()));
                    var graph = new Graph(data);
                    stopwatch.Watch();
                    Console.ReadLine();
                }
            }
        }

        public static IEnumerable<string> Decompress(Stream stream)
        {
            using (var reader = new StreamReader(stream, Encoding.ASCII))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    yield return line;
                }
            }
        }
    }

【讨论】:

    【解决方案2】:

    我知道,您没有关注 VBA,但新的 CommentThreaded 对象现在至少可以工作(Excel 版本 1906,2019 年 6 月测试)。
    我在 Visual Studio C# 中实际测试过,但似乎仍然不支持。

    截至 2019 年 5 月 15 日,新对象 CommentThreadedMicrosoft 描述。
    在我的 Excel 版本 1906 中,VBA 完全支持它。

    这里有一些 VBA 代码来解释一下处理:

    Private Sub ExcelsNewCommentThreaded()
        Dim AllCommentsThreaded As Excel.CommentsThreaded
        Dim OneCommentThreaded As Excel.CommentThreaded
        Dim AllReplies As Excel.CommentsThreaded
        Dim OneReply As Excel.CommentThreaded
        Dim r As Range
    
        Set AllCommentsThreaded = ActiveSheet.CommentsThreaded
    
        ' loop over all threaded comments of a worksheet and get their info
        For Each OneCommentThreaded In AllCommentsThreaded
            With OneCommentThreaded
                Debug.Print .Author.Name, .Date, .Text
                For Each OneReply In .Replies
                    With OneReply
                        Debug.Print .Author.Name, .Date, OneReply.Text
                    End With
                Next OneReply
            End With
        Next OneCommentThreaded
    
        Set r = Selection.Cells(1)
    
        ' check if the selected cell already contains a threaded comment
        If r.CommentThreaded Is Nothing Then
            r.AddCommentThreaded ("my new comment")
        End If
    
        With r.CommentThreaded
            ' get text of comment
            Debug.Print .Text
    
            ' add some replies
            .AddReply ("my reply 1")
            .AddReply ("my reply 2")
    
            ' change text of comment
            Debug.Print .Text(Text:="text of comment changed")
            Debug.Print .Text
    
            ' change text of a reply
            .Replies(1).Text Text:="text of reply 1 changed"
            Debug.Print .Replies(1).Text
    
            ' delete second reply
            .Replies(2).Delete
    
            ' delete whole comment including its replies
            .Delete
        End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 2015-02-16
      相关资源
      最近更新 更多