Java로 Word 문서에 테이블을 만드는 방법

이 간단한 단계별 가이드에서는 Java를 사용하여 Word 문서에 테이블을 만드는 방법을 다룹니다. 이 자습서의 도움으로 하나 이상의 테이블을 삽입하여 요구 사항에 따라 정보를 구성할 수 있습니다. 마지막 단계에서 출력 파일은 DOCX 파일로 저장되지만 모든 Word 파일 형식으로 저장할 수 있습니다.

Java로 Word 문서에 테이블을 만드는 단계

  1. Maven 저장소에서 Aspose.Words for Java 추가
  2. Document 클래스를 사용하여 빈 Word 문서 초기화
  3. Table 클래스 인스턴스 초기화
  4. 테이블에 새 Row 생성
  5. 셀을 만들고 셀 안에 새 단락 추가
  6. 문서 끝에 표 삽입
  7. DOCX 파일로 표와 함께 Word 문서 저장

이 단계를 사용하여 Java를 사용하여 Word 문서에 테이블을 삽입할 수 있습니다. 셀에 정보를 추가하고 Java를 사용하여 프로그래밍 방식으로 문서 내용을 구성할 수 있습니다.

Java로 Word 문서에 테이블을 만드는 코드

import com.aspose.words.Cell;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.Paragraph;
import com.aspose.words.Row;
import com.aspose.words.Run;
import com.aspose.words.Table;
public class CreateTableInWordUsingJava
{
public static void main(String[] args) throws Exception { //main function for CreateTableInWordUsingJava class
// Initialize a license to avoid trial version watermark in the output Word file after adding comments
License license = new License();
license.setLicense("Aspose.Words.lic");
// Initialize a blank Word document using Document class
Document wordDocument = new Document();
// Initialize a new Table class instance
Table wordTable = new Table(wordDocument);
// Create a new Row in the Table
wordTable.getRows().add(new Row(wordDocument));
// Create single Cell in the Table Row
wordTable.getFirstRow().getCells().add(new Cell(wordDocument));
// Add a new Paragraph inside Cell
wordTable.getFirstRow().getFirstCell().appendChild(new Paragraph(wordDocument));
// Add text content inside the Table Cell
wordTable.getFirstRow().getFirstCell().getFirstParagraph().getRuns().add(new Run(wordDocument, "Text in Table Row 1 and Cell 1"));
// Insert a Table at the last portion of Word Document
wordDocument.getFirstSection().getBody().insertBefore(wordTable, wordDocument.getFirstSection().getBody().getLastParagraph());
// Save Word document with Table in DOCX format
wordDocument.save("word_table.docx");
}
}

이 Java 코드에서는 빈 Word 문서를 초기화한 다음 테이블을 추가하고 내용을 특정 셀에 삽입했습니다. 그런 다음 마지막에 표를 삽입하여 Java를 사용하여 Word 문서에서 표를 만드는 방법을 보여줍니다.

이 튜토리얼에서는 Java*를 사용하여 *워드 문서에 표를 삽입하는 방법을 배웠습니다. 그러나 Word 문서의 표에 행을 추가하려면 Java를 사용하여 DOCX의 테이블에 행을 추가하는 방법 문서를 참조하세요.

 한국인