C#을 사용하여 Word 문서에서 테이블을 만드는 방법

이 자습서에서는 C#을 사용하여 Word 문서에서 표를 만드는 두 가지 방법을 나열합니다. 먼저 Aspose DocumentBuilder를 사용하여 C#을 사용하여 Word에서 테이블을 만들고 나중에 Word DOM(문서 개체 모델)에서 직접 C#을 사용하여 Word 테이블을 만듭니다.

C#을 사용하여 Word 문서에서 표를 만드는 단계

  1. 솔루션의 NuGet에서 Aspose.Words for .NET 패키지 참조
  2. Aspose.Words 및 Aspose.Words.Tables 네임스페이스 가져오기
  3. DocumentBuilder 또는 Table 클래스 인스턴스 만들기
  4. InsertCell 또는 Table.Rows.Cells.Add 메서드를 사용하여 테이블 행에 셀 추가
  5. DocumentBuilder.Write 또는 Cell.AppendChild 메서드를 사용하여 단락 텍스트 삽입
  6. 테이블에 여러 행 생성 및 Word 문서에서 테이블 생성 완료
  7. 디스크에 DOCX 형식으로 표와 함께 Word 문서로 저장

다음 Aspose DocumentBuilder 코드 예제는 C# Word 테이블 생성을 위한 .NET 응용 프로그램에서 사용할 수 있습니다.

C#을 사용하여 Word 문서에서 테이블을 만드는 코드

using Aspose.Words;
using Aspose.Words.Tables;
namespace CreateTableInWordDocumentUsingCsharp
{
class CreateTableWord
{
static void Main(string[] args)
{
// Set license before C# Word table creation
License setupToCreateTable = new License();
setupToCreateTable.SetLicense(@"license file path.lic");
// Document Builder object to create Table in Word document
DocumentBuilder toCreateTableInWord = new DocumentBuilder();
// Mark the start of Word Table
Table tableInWord = toCreateTableInWord.StartTable();
// Insert a new Row & then first Cell in Word Table
toCreateTableInWord.InsertCell();
// Write text in Table Cell
toCreateTableInWord.Write("Table Row 1 and Cell 1");
// Insert a new Cell in same Word Table Row
toCreateTableInWord.InsertCell();
// Insert an Image in Word Table Cell
toCreateTableInWord.InsertImage("insert image in word table.jpg");
// Mark the end of Table Row
toCreateTableInWord.EndRow();
// Mark end of Word Table creation
toCreateTableInWord.EndTable();
// Save Word DOCX document with Table on disk
toCreateTableInWord.Document.Save("c# word table created in.docx");
}
}
}

기본적으로 이 테이블 만들기 Word C# 앱을 사용하면 Word 문서에 테이블을 삽입할 수 있습니다. 먼저 첫 번째 테이블 행에 두 개의 셀을 추가하고 나중에 첫 번째 셀에 텍스트 내용을 쓴 다음 Word C#에 이미지 추가 마지막 셀에 씁니다.

C#(DOM 클래스)을 사용하여 Word 테이블을 만드는 코드

using Aspose.Words;
using Aspose.Words.Tables;
namespace CreateWordTable
{
class CreateTableInWord
{
static void Main(string[] args)
{
// Set license before creating Word table
License license = new License();
license.SetLicense(@"license file path.lic");
Document wordDocument = new Document(); // Create empty Word document
Table wordTable = new Table(wordDocument); // Create Table object
wordTable.Rows.Add(new Row(wordDocument)); // Create a Row inside Table
wordTable.FirstRow.Cells.Add(new Cell(wordDocument)); // Create a single Cell in Table Row
wordTable.FirstRow.FirstCell.AppendChild(new Paragraph(wordDocument)); // Add a Paragraph inside Cell
// Write text content inside Word Table Cell
wordTable.FirstRow.FirstCell.FirstParagraph.Runs.Add(new Run(wordDocument, "Text in Table Row 1 and Cell 1"));
// Insert Table at the end of Word Document
wordDocument.FirstSection.Body.InsertBefore(wordTable, wordDocument.FirstSection.Body.LastParagraph);
// Save Word document to DOCX format
wordDocument.Save("c# word table created in.docx");
}
}
}

위의 Aspose Word 테이블 생성 C# 코드 예제는 Table, Row, Cell 및 Paragraph 클래스를 사용하여 Word 문서 개체 모델에 테이블 요소를 추가하기 위한 대체 접근 방식을 보여줍니다.

 한국인