簡體   English   中英

使用 OpenXML 向 docx 文件中的表添加多行

[英]Adding multiple rows to a table in docx files with OpenXML

我正在使用 TemplateEngine.Docx,模板引擎在 Visual Studio 中為 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;
                    }
                }
            }

您可以嘗試以下代碼在循環中將數據寫入 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();

            }
        }

測試結果:

在此處輸入圖像描述

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM