【问题标题】:Adding multiple rows to a table in docx files with OpenXML使用 OpenXML 向 docx 文件中的表添加多行
【发布时间】:2021-06-03 15:27:10
【问题描述】:

我正在使用 TemplateEngine.Docx,模板引擎用于在 Visual Studio for C# 中生成 Word docx。基本上,我正在尝试使用 for 循环将多行添加到已编辑的 Word 文档中的表中。但是,当我遍历 if 语句时,它只会重写刚刚读取的数据。这是我的代码:

if (fieldName == "examrubric")
            {

                for (; ; )
                {
                    Console.WriteLine("To enter a row to the tabel type 'yes'\n");
                    string choice = Console.ReadLine();

                    if (choice == "yes")
                    {

                        Console.WriteLine("Enter a value for the question number:\n");
                        string qnum = Console.ReadLine();
                        Console.WriteLine("Enter a value for what the question is out of:\n");
                        string score = Console.ReadLine();
                        Console.WriteLine("Enter a value for how much was scored:\n");
                        string qscore = Console.ReadLine();
                        Console.WriteLine("Enter a value for score:\n");
                        string score2 = Console.ReadLine();
                        Console.WriteLine("Enter the total mark:\n");
                        string total = Console.ReadLine();


                        valuesToFill = new Content(
                                       new TableContent("examrubric")
                                       .AddRow(
                                       new FieldContent("qnum", qnum),
                                       new FieldContent("score", score),
                                       new FieldContent("qscore", qscore),
                                       new FieldContent("score2", score2),
                                       new FieldContent("total", total)));
                    }

                    else
                    {
                        break;
                    }
                }
            }

【问题讨论】:

    标签: c# .net visual-studio openxml


    【解决方案1】:

    您可以尝试以下代码在循环中将数据写入docx文件以避免重写数据。

    static void Main(string[] args)
            {
               
                File.Copy("test1.docx", "OutputDocument.docx");
                Console.WriteLine("To enter a row to the tabel type 'yes'\n");
                string choice = Console.ReadLine();
                Content valuesToFill = new Content(new TableContent("Team Members Table"));
                for (int i = 0; i < 2; i++)
                {
                    if (choice == "yes")
                    {
                        Console.WriteLine("Enter a value for the Name:\n");
                        string name = Console.ReadLine();
                        Console.WriteLine("Enter a value for the Role:\n");
                        string role = Console.ReadLine();
                        valuesToFill.Tables.First().AddRow(
                                new FieldContent("Name", name),
                                new FieldContent("Role", role)) ;
                        
                       
                    }
    
                }
                using (var outputDocument = new TemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true))
                {
                    outputDocument.FillContent(valuesToFill);
                    outputDocument.SaveChanges();
    
                }
            }
    

    测试结果:

    【讨论】:

    • 我试过了,但是它说没有在word文档中找到字段内容控件'xxxxxx'。
    • @Kevin Liu,该错误表示您需要为您的代码添加相应的内容控件。你可以参考链接TemplateEngine.Docx
    猜你喜欢
    • 1970-01-01
    • 2012-03-05
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多