FileFormat.words ist eine Open-Source-Bibliothek, in der ein kostenloses Textprozessormodul anbietet, mit dem Sie programmgesteuert Tabellen in Word-Dokumenten hinzufügen/ändern können.

So erstellen Sie eine Tabelle in Word -Dateien mit FileFormat.words

Übersicht

Eine Tabelle in einem Word Dokument wird als integraler Bestandteil in Bezug auf die Datendarstellung angesehen. Es ist das häufigste Dokumentelement und bietet große Bequemlichkeit in Bezug auf das Aufbau von Geschäftsdokumenten. Erstaunlicherweise können Sie eine Tabelle in einem Word -Dokument erstellen, ohne MS Word auf Ihrem lokalen Computer zu verwenden oder zu installieren. Ja, lassen Sie mich einen kostenlosen Textverarbeitungsprozessor vorstellen, mit dem Sie Wortdokumente programmatisch erstellen und manipulieren können. FileFormat.words für .NET ist ein komplettes Paket mit Word -Dokumentverarbeitung. Daher lernen wir in diesem Blogbeitrag wie man eine Tabelle in Word Dokumente mit dieser .NET -Bibliothek FileFormat.Words erstellen. Dieser Blog -Beitrag deckt die folgenden Abschnitte ab:

Tabellengenerator für Wort - API -Installation

FileFormat.Words für .NET bietet eine breite Palette von Funktionen für die MS -Textverarbeitung. Diese Open-Source-API ist sehr einfach zu installieren. Sie können jedoch das Nuget -Paket von Installation herunterladen, indem Sie den folgenden Befehl in den Nuget -Paket -Manager ausführen.

Install-Package FileFormat.Words

So erstellen Sie programmatisch eine Tabelle in einem Word -Dokument

Lassen Sie uns einen Code schreiben, um diese Open-Source Free Text Processo R in Aktion anzuzeigen. Tatsächlich werden wir sehen, wie Sie eine Tabelle in einem Word -Dokument mit der Dateiformat.Words -Bibliothek erstellen. Bitte befolgen Sie die folgenden Schritte und den Code -Snippet:

  • Initialisieren Sie eine Instanz der Dokument Klasse.
  • Sofortieren Sie den Konstruktor der Körper -Klasse mit dem Dokumentklassenobjekt.
  • Erstellen Sie ein Objekt der Tabelle Klasse.
  • Initialisieren Sie die Konstrukteure des Topborder, Bottomborder, rechtsborder, linke, InnereverticalBorder und Inside Horizontalborder Klassen zu Klassen zu Klassen zu Stellen Sie den Rand aller Seiten des Tisches fest.
  • Rufen Sie die BasicBlackSquares_Border -Methode auf, um den Grenzstil und die Grenzbreite festzulegen.
  • Erstellen Sie eine Instanz der TableBorders -Klasse.
  • Anhang Die Objekte des Topborders, des Bottomborders, des Rechtsborders, des Linksbahnens, des Innenvertreibers und in den Klassen von Horizontalborder an das Objekt der TableBorders -Klasse.
  • Initialisieren Sie eine Instanz der TableProperties -Klasse.
  • Rufen Sie die Methode Anhänge der TabelleProperties -Klasse auf, um das Objekt der TableBorders -Klasse anzuhängen.
  • Erstellen Sie eine Instanz der Tabelleneinstellung Klasse und rufen Sie die Methode Alignleft auf, um die Tabelle auf der linken Seite des Dokuments zu positionieren.
  • Rufen Sie die Methode anhang auf, um das Tabelleneinrichtungsobjekt an das TBLProp -Objekt anzuhängen.
  • Die appendchild -Methode der Tabellenklasse wird die Tabelleneigenschaften an der Tabelle angeschlossen.
  • Erstellen Sie ein Objekt der Tablerow -Klasse, um eine Tabellenzeile zu erstellen.
  • Initialisieren Sie eine Instanz der TableCell -Klasse.
  • Stellen Sie den Header der ersten Spalte ein, indem Sie die TableHeader -Methode aufrufen.
  • Rufen Sie die Methode 30 der Tabellenklasse an, um Text in der Tabellenzelle hinzuzufügen.
  • Erstellen Sie ein Objekt der TableCellProperties Tabelleneigenschaften
  • Legen Sie die Breite der Tabellenzelle durch initialisieren des Objekts der TableCellwidth -Klasse und fügen Sie dem TBLCELLProps -Objekt hinzu.
  • Die Methode Append wird das TblcellProps -Objekt mit dem Objekt der Tabellenklasse angehängt.
  • Rufen Sie die Methode Anhang auf, um die Zeilen zur Tabelle hinzuzufügen.
  • Die appendchild -Methode fügt die Tabelle dem Körper des Dokuments hinzu.
  • Die Speichern -Methode speichert das Wortdokument auf der Festplatte.
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");
            }

        }

    }
}

Die Ausgabe des obigen Code -Snippets wird im Bild unten angezeigt:

So erstellen Sie eine Tabelle in einem Word -Dokument

Schlussfolgerung

Dies bringt uns zum Ende dieses Artikels und wir hoffen, dass Sie gelernt haben So erstellen Sie eine Tabelle in einem Wort Dokument mit FileFormat.Words Library. Darüber hinaus hilft Ihnen dieser Leitfaden, wenn Sie einen Tabellengenerator für Word -Dokumente in C#erstellen möchten. Darüber hinaus ist diese freie Textverarbeitung API Open Source und Sie können ihre Dokumentation hier finden. Schließlich schreibt FileFormat.com weiterhin Blog -Beiträge zu anderen Themen. Darüber hinaus können Sie uns auf unseren Social -Media -Plattformen folgen, einschließlich Facebook, LinkedIn und Twitter.

beitragen

Da FileFormat.words für .NET ein Open-Source-Projekt ist und auf Github verfügbar ist. Der Beitrag der Gemeinschaft wird also sehr geschätzt.

stellen Sie eine Frage

Sie können uns über Ihre Fragen oder Abfragen in unserem Forum informieren.

häufig gestellte Fragen-FAQs

** Wie erstelle ich eine Tabelle in einem Word -Dokument?** Es ist sehr einfach, eine Tabelle im Word-Dokument mit dieser Open-Source-Bibliothek 5 zu erstellen. Darüber hinaus können Sie diese API weiter untersuchen. ** Wie erstelle ich eine DOCX -Datei in C#?** Bitte befolgen Sie diesen Link, um ein detailliertes Code -Snippet und Schritte zum Erstellen einer DOCX -Datei in C#durchzuführen. ** Wie erstelle ich ein benutzerdefiniertes Tabellenformat in Wort?** FileFormat.Words für .NET ist eine kostenlose Bibliothek, die Funktionen zum Manipulieren und Erstellen von MS -Word programmgesteuert bietet. Tatsächlich können Sie diesen Namespace FileFormat.words.table untersuchen, um die Methoden und Eigenschaften anzuzeigen.

Siehe auch