종종 프로세스를 자동화하고 프로그래밍 방식으로 문서를 조작해야합니다. 결과 문서를 대량으로 작성하고 읽기, 처리 및 저장하는 문서를 작성해야합니다. 다양한 파일 형식을 동시에 작업해야합니다. 운 좋게도 Java 개발자의 경우 Word, 스프레드 시트, 프리젠 테이션, 이메일 및 [다이어그램]과 함께 작업 할 오픈 소스 API가 있습니다. ] 파일 형식-아파치 포이. 이 크로스 플랫폼 API는 JVM (Java Virtual Machine) 기반 언어와 함께 작동하도록 설계되었습니다.

설치 방법

Apache Poi를 설치하는 것은 쉽지 않습니다. Maven 기반 프로젝트에 종속성을 추가하기 만하면됩니다. pom.xml에 다음 의존성을 추가하고 Apache Poi로 시작할 수 있습니다.

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>     
  <groupId>org.apache.poi</groupId>     
  <artifactId>poi</artifactId>     
  <version>4.1.0</version> 
</dependency>

단어 문서를 만듭니다

Apache Poi를 사용하면 xwpfdocument 를 사용하여 Word 문서를 만들고 xwpfparagraph 클래스를 사용하여 단락을 삽입 할 수 있습니다. 다음 코드 스 니펫은 API를 사용하여 워드 문서를 만드는 방법을 보여줍니다.

// initialize a blank document
XWPFDocument document = new XWPFDocument();
// create a new file
FileOutputStream out = new FileOutputStream(new File("createdocument.docx"));
// create a new paragraph paragraph
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("File Format Developer Guide -  " +
            "Learn about computer files that you come across in " +
            "your daily work at: www.fileformat.com ");
document.write(out);
out.close();
System.out.println("Document created successfully")

다음은 결과 출력 문서입니다. *