Apache의 POI 프로젝트는 MS Office 문서를 처리하는 데 사용될 수 있으며 CodePlex에는 .NET 버전도 있습니다. POI 프로젝트는 OOXML 및 OLE2 파일 형식을 기반으로 다양한 Java API를 생성하고 유지 관리 할 수 있습니다. 대부분의 MS 사무실은 OLE2 형식입니다. POI는 HSMF 하위 프로젝트를 통해 Outlook을 지원하고 HDGF 하위 프로젝트를 통해 Visio를 지원하며 HPBF 하위 프로젝트를 통해 게시자를 지원합니다.
POI를 사용하여 단어를 추출하는 간단한 예 :
POI-3.7.jat 및 POI-ScratchPad-3.7.AJR의 두 패키지를 소개해야합니다.
코드 사본은 다음과 같습니다.
패키지 MSOFFICE;
import java.io.file;
import java.io.fileInputStream;
import java.io.ioexception;
import java.io.inputstream;
import org.apache.poi.hwpf.hwpfdocument;
import org.apache.poi.hwpf.extractor.wordextractor;
import org.apache.poi.hwpf.usermodel.characterrun;
import org.apache.poi.hwpf.usermodel.paragraph;
import org.apache.poi.hwpf.usermodel.range;
import org.apache.poi.hwpf.usermodel.section;
공개 클래스 단어 {
// 모든 컨텐츠를 직접 추출합니다
public static string readdoc1 (inputstream is)은 ioexception {
WordExtractor 추출기 = 새로운 WordExtractor (IS);
return extractor.getText ();
}
// 섹션별로 추출, 단락 단락, 문자열 문자 문자
public static void readdoc2 (inputstream is)는 ioexception {
hwpfdocument doc = 새로운 hwpfdocument (IS);
범위 r = doc.getRange ();
for (int x = 0; x <r.numsections (); x ++) {
섹션 S = R.GetSection (x);
for (int y = 0; y <s.numparagraphs (); y ++) {
단락 p = s.getParagraph (y);
for (int z = 0; z <p.numcharacterruns (); z ++) {
문자 런 런 = p.getCharacterRun (z);
문자열 text = run.text ();
System.out.print (텍스트);
}
}
}
}
public static void main (String [] args) {
파일 파일 = 새 파일 ( "/home/orisun/1.doc");
노력하다 {
fileInputStream fin = new FileInputStream (file);
문자열 cont = readdoc1 (fin);
System.out.println (계속);
fin.close ();
fin = new FileInputStream (파일);
readdoc2 (Fin);
fin.close ();
} catch (ioexception e) {
e.printstacktrace ();
}
}
}
POI 추출 PPT의 예 :
코드 사본은 다음과 같습니다.
패키지 MSOFFICE;
import java.io.file;
import java.io.fileInputStream;
import java.io.ioexception;
import java.io.inputstream;
import org.apache.poi.hslf.hslfslideshow;
import org.apache.poi.hslf.extractor.powerpointextractor;
import org.apache.poi.hslf.model.slide;
import org.apache.poi.hslf.model.textrun;
import org.apache.poi.hslf.usermodel.slideshow;
공개 클래스 ppt {
// 슬라이드의 모든 내용을 직접 추출합니다
public static string readdoc1 (inputstream is)은 ioexception {
PowerPointExtractor 추출기 = New PowerPointExtractor (IS);
return extractor.getText ();
}
// 하나의 슬라이드 슬라이드 한 슬라이드를 읽습니다
public static void readdoc2 (inputstream is)는 ioexception {
슬라이드 쇼 SS = New Slideshow (New HSLFSLIDSHOW (IS));
슬라이드 [] 슬라이드 = ss.getSlides ();
for (int i = 0; i <slides.length; i ++) {
// 슬라이드의 제목을 읽습니다
문자열 제목 = 슬라이드 [i] .getTitle ();
System.out.println ( "제목 :"+제목);
// 슬라이드의 내용을 읽습니다 (제목 포함)
textrun [] runs = 슬라이드 [i] .gettextruns ();
for (int j = 0; j <runs.length; j ++) {
System.out.println (runs [j] .getText ());
}
}
}
public static void main (String [] args) {
파일 = 새 파일 ( "/home/orisun/2.ppt");
노력하다{
fileInputStream fin = new FileInputStream (file);
문자열 cont = readdoc1 (fin);
System.out.println (계속);
fin.close ();
fin = new FileInputStream (파일);
readdoc2 (Fin);
fin.close ();
} catch (ioexception e) {
e.printstacktrace ();
}
}
}
Excel 파일은 여러 통합 문서로 구성되며 통합 문서는 여러 시트로 구성됩니다.
POI 추출 엑셀의 간단한 예 :
코드 사본은 다음과 같습니다.
패키지 MSOFFICE;
import java.io.file;
import java.io.fileInputStream;
import java.io.ioexception;
import java.io.inputstream;
import java.util.iterator;
import org.apache.poi.hssf.usermodel.hssfcell;
import org.apache.poi.hssf.usermodel.hssfrow;
import org.apache.poi.hssf.usermodel.hssfsheet;
import org.apache.poi.hssf.usermodel.hssfworkbook;
import org.apache.poi.hssf.extractor.excelextractor;
import org.apache.poi.poifs.filesystem.poifsfilesystem;
import org.apache.poi.ss.usermodel.row;
공개 클래스 Excel {
// Excel의 모든 내용을 직접 읽으십시오
public static string readdoc1 (inputstream is)은 ioexception {
hssfworkbook wb = 새로운 hssfworkbook (새로운 poifsfilesystem (is));
ExcelExtractor 추출기 = 새로운 ExcelExtractor (WB);
extractor.setformulasnotresults (false);
추출기 setIncludESheetNames (true);
return extractor.getText ();
}
// 읽을 때 시트, 행 또는 셀을 세분화합니다
public static double getavg (inputstream is)는 ioexception {
hssfworkbook wb = 새로운 hssfworkbook (새로운 poifsfilesystem (is));
// 첫 번째 시트를 가져옵니다
hssfsheet sheet = wb.getsheetat (0);
이중 분자 = 0.0;
이중 분모 = 0.0;
// 한 줄로 시트를 전송합니다
반복자 <줄> riter = sheet.rowiterator ();
while (riter.hasnext ()) {
hssfrow row = (hssfrow) riter.next ();
hssfcell cell1 = row.getCell (4);
hssfcell cell2 = row.getcell (4);
if (cell1.getCellType ()! = hssfcell.cell_type_numeric) {
System.err.println ( "번호 유형 오류!");
System.exit (-2);
}
if (cell2.getCellType ()! = hssfcell.cell_type_numeric) {
System.err.println ( "번호 유형 오류!");
System.exit (-2);
}
분모+= double.parsedouble (cell2.tostring (). trim ());
Molecule+= double.parsedouble (cell2.tostring (). trim ())*float.parsefloat (cell1.toString (). trim ());
}
리턴 분자/분모;
}
public static void main (String [] args) {
파일 파일 = 새 파일 ( "/home/orisun/3.xls");
노력하다{
fileInputStream fin = new FileInputStream (file);
문자열 cont = readdoc1 (fin);
System.out.println (계속);
fin.close ();
fin = new FileInputStream (파일);
System.out.println ( "가중 평균 점수" + getavg (Fin));
fin.close ();
} catch (ioexception e) {
e.printstacktrace ();
}
}
}