5

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

如果我将 Excel 文档转换为 .zip 文件,我可以看到具有我需要的“threadComments.xml”文件,但不知道如何在 C# .NET 中以编程方式进行处理。

4

2 回答 2

0

我知道,您没有注意 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
于 2019-03-06T16:17:52.977 回答
-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;
                }
            }
        }
    }
于 2019-02-25T14:20:04.700 回答