fileFormat.words est une bibliothèque open source qui propose un module de processeur de mots gratuit qui vous permet d’ajouter / modifier des tables dans des documents Word.

Comment faire une table dans des fichiers Word à l'aide de fileformat.words

Présentation

Une table dans un mot document est considérée comme une partie intégrante en ce qui concerne la représentation des données. Il s’agit de l’élément de document le plus courant et offre une grande commodité en termes de construction de documents commerciaux. Étonnamment, vous pouvez créer une table dans un document Word sans utiliser ou installer MS Word sur votre machine locale. Oui, permettez-moi d’introduire un Processeur de texte gratuit qui vous permet de créer et de manipuler des documents de mots par programme. FileFormat.Words pour .NET est un package complet de traitement de document Word. Par conséquent, dans cet article de blog, nous apprendrons comment faire un tableau dans Word Documents en utilisant cette bibliothèque .NET FileFormat.Words. Ce billet de blog couvre les sections suivantes:

Générateur de table pour Word - Installation de l’API

FileFormat.Words pour .NET fournit une large gamme de fonctionnalités pour le traitement de texte MS. Cette API open source est très facile à installer. Cependant, vous pouvez télécharger son package NuGet de l’installer en exécutant la commande suivante dans le gestionnaire de package NuGet.

Install-Package FileFormat.Words

Comment créer une table dans un document Word programmatiquement

Écrivons du code pour voir cette open source word processo r en action. En fait, nous verrons comment faire une table dans un document Word à l’aide de la bibliothèque fileformat.words. Veuillez suivre les étapes suivantes et l’extrait de code:

  • Initialisez une instance de la classe Document.
  • Instancier le constructeur de la classe Body avec l’objet de classe de document.
  • Créez un objet de la classe Table.
  • Initialisez les constructeurs du Topborder, Bottomborter, Rightborder, Leftfer, à l’intérieur de Terticalborder et InsideHorizontalborder Réglez la bordure de tous les côtés de la table.
  • Invoquez la méthode BasicBlackSquares_Border pour définir le style de bordure et la largeur limite.
  • Créez une instance de la classe Tableborders.
  • Ajouter Les objets du Topber, Bottomborter, Rightborter, Leftborder, InsideVerticalborder et InsideHorizontalborder à l’objet de la classe Tableborders.
  • Initialisez une instance de la classe TableProperties.
  • Invoquez la méthode APPEND de la classe TableProperties pour joindre l’objet de la classe TableBorders.
  • Créez une instance de la classe TableJustification et appelez la méthode Alignleft pour positionner le tableau sur le côté gauche du document.
  • Invoquez la méthode APPEND pour joindre l’objet Table AJustification à l’objet TBLPROP.
  • La méthode APPENDCHILD de la classe de table attachera les propriétés du tableau au tableau.
  • Créez un objet de la classe TableRow pour créer une ligne de table.
  • Initialisez une instance de la classe TableCell.
  • Définissez l’en-tête de la première colonne en invoquant la méthode TableHeaders.
  • Appelez la méthode APPEND de la classe TableCell pour ajouter du texte à l’intérieur de la cellule de table.
  • Créez un objet des TableCellProperties Propriétés de la table
  • Définissez la largeur de la cellule de table en initialisant l’objet de la classe TableCellWidth et ajoutez à l’objet TBLCELLPROPS.
  • La méthode APPEND attachera l’objet TblCellProps avec l’objet de la classe TableCell.
  • Appelez la méthode APPEND pour ajouter les lignes au tableau.
  • La méthode APPENDCHILD ajoutera le tableau au corps du document.
  • La méthode Save enregistrera le document Word sur le disque.
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 propertiese 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 istance 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();
                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);

                TableCellProperties tblCellProps3 = new TableCellProperties();
                tblCellProps3.Append(new TableCellWidth("1400"));
                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();
                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("/Docs.docx");
            }

        }

    }
}

La sortie de l’extrait de code ci-dessus est affichée dans l’image ci-dessous:

Comment créer une table dans un document Word

Conclusion

Cela nous amène à la fin de cet article et nous espérons que vous avez appris comment faire une table dans un document Word à l’aide de la bibliothèque fileFormat.Words. De plus, ce guide vous aidera si vous cherchez à créer un générateur de table pour les documents Word en C #. De plus, cette API Word Processor est open source et vous pouvez trouver sa documentation ici. Enfin, FileFormat.com continue d’écrire des articles de blog sur d’autres sujets. De plus, vous pouvez nous suivre sur nos plateformes de médias sociaux, notamment Facebook, LinkedIn et Twitter.

contribuer

Puisque FileFormat.Words pour .NET est un projet open-source et est disponible sur github. 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.

Questions fréquemment posées - FAQS

** Comment créer une table dans un document Word?** Il est très facile de créer un tableau dans un document Word en utilisant cette open source .NET Library. De plus, vous pouvez explorer cette API plus loin. ** Comment créer un fichier docx en C #?** Veuillez suivre ce lien pour passer par un extrait de code détaillé et des étapes pour créer un fichier docx en C #. ** Comment créer un format de table personnalisé dans Word?** FileFormat.Words pour .NET est une bibliothèque gratuite qui propose des fonctionnalités pour manipuler et créer MS Word par programme. En fait, vous pouvez explorer cet espace de noms fileformat.words.table pour voir les méthodes et les propriétés.

Voir aussi