Effectuer une fusion horizontale ou une fusion verticale d’une ou plusieurs cellules de table dans les fichiers DOC / DOCX. FileFormat.Words fournit des méthodes pour travailler avec des tables dans les fichiers Word.

Comment fusionner les cellules de table dans des documents de mots

Présentation

La nouvelle version de [FileFormat.Words] 2 propose d’autres méthodes pour travailler avec des tables dans [Word] 3 Documents. La précédente [version] 4 contient des méthodes pour créer, modifier et lire les propriétés de la table tandis que la dernière version permet aux utilisateurs de fusionner programmatiquement des cellules de table dans des fichiers DOCS / DOCX. De plus, vous pouvez faire fusion horizontale ou fusion verticale des cellules de table en utilisant ce .NET open-source [bibliothèque] 5. De plus, il s’agit d’une bibliothèque facile à utiliser dont les méthodes ne sont pas complexes et n’ont pas besoin de dépendance tierce. Dans cet article de blog, nous apprendrons à fusionner les cellules de table dans des documents de mots. Commençons donc le processus d’installation et commençons à écrire le code source. Nous couvrirons les titres suivants dans cet article:

  • [Installation de l’API du générateur de table] 6
  • [Comment fusionner les cellules de table dans les documents de mots] 7

Installation de l’API du générateur de table

Veuillez visiter ce [lien] 8 pour des informations détaillées sur l’installation. Juste pour recaper, le processus d’installation de cette API .NET gratuite est relativement facile. Eh bien, vous pouvez télécharger le [package NuGet] 9 ou exécuter la commande suivante dans le gestionnaire de package NuGet:

Install-Package FileFormat.Words

Comment fusionner les cellules de table dans des documents de mots

Nous rédigerons un extrait de code pour obtenir une fusion horizontale et une fusion verticale des cellules de table. À cette fin, nous utiliserons les classes et méthodes exposées. Veuillez visiter ce guide pour savoir comment créer une table dans un document Word à l’aide de fileFormat.words. Nous utiliserons donc d’autres classes et méthodes dans notre extrait de code:

  • Créez un objet de la classe [VerticalMerge] 10.
  • [Mergerrestart] 11 La propriété est utilisée pour spécifier que l’élément doit démarrer une nouvelle région fusionnée verticalement dans le tableau.
  • Invoquez la méthode [APPEND] 12 pour attacher l’objet VerticalMerge avec l’objet TblCellProps.
  • Instancier une instance de la classe [horizontalmer] 13.
  • [Mergerrestart] 14 La propriété est utilisée pour spécifier que l’élément doit démarrer une nouvelle région fusionnée horizontalement dans le tableau.
  • Appelez la méthode [APPEND] 15 pour joindre l’objet Horizontalmerge avec l’objet TblCellProps.
  • [MergeContinue] 16 La propriété est utilisée pour spécifier que l’élément doit mettre fin à une région fusionnée horizontalement dans le tableau.
  • [MergeContinue] 17 La ​​propriété est utilisée pour spécifier que l’élément doit mettre fin à une région fusionnée verticalement dans le tableau.
using FileFormat.Words;
using FileFormat.Words.Table;
namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize an instance of the Document class.
            using (Document doc = new Document())
            {
                // Instantiate the constructor of the Body class with the Document class object.
                Body body = new Body(doc);
                // Create an object of the Table class.
                Table table = new Table();
                // Initialize the constructor of the TopBorder class to set the border of the top side of the table.
                TopBorder topBorder = new TopBorder();
                // Invoke the basicBlackSquares_border method to set the border style and border line width.
                topBorder.basicBlackSquares_border(20);
                // To set the border of the bottom side of the table.
                BottomBorder bottomBorder = new BottomBorder();
                bottomBorder.basicBlackSquares_border(20);
                // To set the border of the right side of the table.
                RightBorder rightBorder = new RightBorder();
                rightBorder.basicBlackSquares_border(20);
                // To set the border of the left side of the table.
                LeftBorder leftBorder = new LeftBorder();
                leftBorder.basicBlackSquares_border(20);
                // To set the inside vertical border of the table.
                InsideVerticalBorder insideVerticalBorder = new InsideVerticalBorder();
                insideVerticalBorder.basicBlackSquares_border(20);
                // To set the inside vehorizontalrtical border of the table.
                InsideHorizontalBorder insideHorizontalBorder = new InsideHorizontalBorder();
                insideHorizontalBorder.basicBlackSquares_border(20);
                // Create an instance of the TableBorders class. 
                TableBorders tableBorders = new TableBorders();
                // Append the object of the TopBorder class to the object of the TableBorders class.
                tableBorders.AppendTopBorder(topBorder);
                // Append the object of the BottomBorder class.
                tableBorders.AppendBottomBorder(bottomBorder);
                // Append the object of the RightBorder class.
                tableBorders.AppendRightBorder(rightBorder);
                // Append the object of the LeftBorder class.
                tableBorders.AppendLeftBorder(leftBorder);
                // Append the object of the InsideVerticalBorder class.
                tableBorders.AppendInsideVerticalBorder(insideVerticalBorder);
                // Append the object of the InsideHorizontalBorder class.
                tableBorders.AppendInsideHorizontalBorder(insideHorizontalBorder);

                // Initialize an instance of the TableProperties class.
                TableProperties tblProp = new TableProperties();
                // Invoke the Append method to attach the object of the TableBorders class.
                tblProp.Append(tableBorders);
                // Create an instance of the TableJustification class 
                TableJustification tableJustification = new TableJustification();
                // Call the AlignLeft method to position the table on left side of the document.
                tableJustification.AlignLeft();
                // Invoke the Append method to attach the tableJustification object to the tblProp object.
                tblProp.Append(tableJustification);

                // The AppendChild method will attach the table properties to the table.
                table.AppendChild(tblProp);

                // Create an object of the TableRow class to create a table row.
                TableRow tableRow = new TableRow();
                TableRow tableRow2 = new TableRow();

                // Initialize an instance of the TableCell class.
                TableCell tableCell = new TableCell();
                Paragraph para = new Paragraph();
                Run run = new Run();

                // Set the header of the first column by invoking the TableHeaders method.
                table.TableHeaders("Name");
                run.Text = "Mustafa";
                para.AppendChild(run);
                // Call the Append method to add text inside the table cell.
                tableCell.Append(para);

                // Create an object of the TableCellProperties table properties 
                TableCellProperties tblCellProps = new TableCellProperties();

                // Set the width of table cell by initializing the object of the TableCellWidth class and append to tblCellProps object.
                tblCellProps.Append(new TableCellWidth("2400"));
                // Append method will attach the tblCellProps object with the object of the TableCell class.
                tableCell.Append(tblCellProps);

                TableCell tableCell2 = new TableCell();
                Paragraph para2 = new Paragraph();
                Run run2 = new Run();

                // set the header of the second column
                table.TableHeaders("Nationality");
                run2.Text = "Pakistani";
                para2.AppendChild(run2);
                tableCell2.Append(para2);

                TableCellProperties tblCellProps2 = new TableCellProperties();
                // Create an object of the VerticalMerge class. 
                VerticalMerge verticalMerge = new VerticalMerge();
                // MergeRestart property is used to specify that the element shall start a new vertically merged region in the table.
                verticalMerge.MergeRestart = true;
                // Invoke the Append method to attach the verticalMerge object with the tblCellProps object.
                tblCellProps.Append(verticalMerge);

                // Instantiate an instance of the HorizontalMerge class. 
                HorizontalMerge horizontalMerge = new HorizontalMerge();
                // MergeRestart property is used to specify that the element shall start a new horizontally merged region in the table.
                horizontalMerge.MergeRestart = true;
                // Call the Append method to attach the horizontalMerge object with the tblCellProps object.
                tblCellProps2.Append(horizontalMerge);

                tblCellProps2.Append(new TableCellWidth("1400"));
                tableCell2.Append(tblCellProps2);

                TableCell tableCell3 = new TableCell();
                Paragraph para3 = new Paragraph();
                Run run3 = new Run();
                table.TableHeaders("Age");
                run3.Text = "30";
                para3.AppendChild(run3);
                tableCell3.Append(para3);

                HorizontalMerge horizontalMerge1 = new HorizontalMerge();
                // MergeContinue property is used to specify that the element shall end a horizontally merged region in the table.
                horizontalMerge1.MergeContinue = true;
                TableCellProperties tblCellProps3 = new TableCellProperties();
                tblCellProps3.Append(new TableCellWidth("1400"));
                tblCellProps3.Append(horizontalMerge1);

                tableCell3.Append(tblCellProps3);
                // Call the Append method to add cells into table row.
                tableRow.Append(tableCell);
                tableRow.Append(tableCell2);
                tableRow.Append(tableCell3);

                // create table cell
                TableCell _tableCell = new TableCell();
                Paragraph _para = new Paragraph();
                Run _run = new Run();

                _run.Text = "sultan";
                _para.AppendChild(_run);
                _tableCell.Append(_para);

                TableCellProperties tblCellProps1_ = new TableCellProperties();
                VerticalMerge verticalMerge2 = new VerticalMerge();
                // MergeContinue property is used to specify that the element shall end a vertically merged region in the table.
                verticalMerge2.MergeContinue = true;
                tblCellProps1_.Append(verticalMerge2);
                tblCellProps1_.Append(new TableCellWidth("2400"));
                _tableCell.Append(tblCellProps1_);


                TableCell _tableCell2 = new TableCell();
                Paragraph _para2 = new Paragraph();
                Run _run2 = new Run();

                _run2.Text = "British";
                _para2.AppendChild(_run2);
                _tableCell2.Append(_para2);

                TableCellProperties tblCellProps2_ = new TableCellProperties();
                tblCellProps2_.Append(new TableCellWidth("1400"));
                _tableCell2.Append(tblCellProps2_);

                TableCell _tableCell3 = new TableCell();
                Paragraph _para3 = new Paragraph();
                Run _run3 = new Run();

                _run3.Text = "2";
                _para3.AppendChild(_run3);
                _tableCell3.Append(_para3);

                TableCellProperties tblCellProps3_ = new TableCellProperties();
                tblCellProps3_.Append(new TableCellWidth("1400"));
                _tableCell3.Append(tblCellProps3_);

                tableRow2.Append(_tableCell);
                tableRow2.Append(_tableCell2);
                tableRow2.Append(_tableCell3);
                // Call the Append method to add the rows into table.
                table.Append(tableRow);
                table.Append(tableRow2);
                // The AppendChild method will add the table to the body of the document.
                body.AppendChild(table);
                // The Save method will save the Word document onto the disk.
                doc.Save("/Users/Mustafa/Desktop/Docs.docx");
            }

        }

    }
}

Copiez et collez le segment de code ci-dessus dans votre fichier principal et exécutez. Vous verrez le fichier Word généré avec le contenu indiqué dans l’image ci-dessous:

fusion des cellules de table

Conclusion

Nous terminons ce billet de blog ici avec l’espoir que vous ayez appris à fusionner les cellules de table dans Word documents par programme. De plus, nous avons également écrit le code source pour implémenter la fusion horizontale et la fusion verticale des cellules de table. Par conséquent, vous pouvez opter pour cette API du générateur de table .NET Open-source pour automatiser l’automatisation des fichiers Word. En fin de compte, n’oubliez pas de visiter la [documentation] 19 pour découvrir d’autres classes et méthodes. Enfin, [fileFormat.com] 20 écrit toujours des articles de blog de didacticiel sur des sujets intéressants. Alors, restez en contact pour des mises à jour régulières. De plus, vous pouvez nous suivre sur nos plateformes de médias sociaux, notamment [Facebook] 21, [LinkedIn] 22 et [Twitter] 23.

contribuer

Puisque [FileFormat.Words pour .NET] 5 est un projet open-source et est disponible sur [github] 24. Ainsi, la contribution de la communauté est très appréciée.

poser une question

Vous pouvez nous informer de vos questions ou questions sur notre [Forum] 25.

faqs

Comment fusionner les cellules dans un document Word? Vous pouvez fusionner les cellules de table en utilisant [VerticalMerge] 10 et [Horizontalmerge] 13 classes. Comment fusionner les cellules dans une table dans Doc? Veuillez suivre ce [lien] 7 pour apprendre les étapes et l’extrait de code pour réaliser cette fonctionnalité.

Voir aussi

  • [Comment créer un document Word en C # à l’aide de fileformat.words] 8
  • [Comment modifier un document Word en C # à l’aide de fileformat.words] 26
  • [Comment faire une table dans des fichiers Word à l’aide de fileformat.words] 27