이 예제는 POI를 사용하여 참조를 위해 Excel을 작동시키는 특정 Java 코드를 공유합니다. 특정 내용은 다음과 같습니다
POI에 따라 POM.XML 구성에 따라 JAR 패키지는 다음과 같습니다.
<project xmlns = "http://maven.apache.org/pom/4.0.0"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance" xsi : schemalocation = "http://maven.apache.org/pom/4.0.0http://maven.apache.org/maven-v4_0_0.xsd"> <modelversion> 4.0.0 </modelversion> <groupid> exceldemo1 </groupid> <artifactid> exceldemo1 <Packaging> War </packaging> <snapshot </version> <name> ExcelDemo1 Maven WebApp </name> <Url> http://maven.apache.org </url> <pectients> <pection> <groupid> junit </groupid> <artifactid> junit> <version> 3.8. <Scope> 테스트 </scope> </dependency> <pectionency> <groupId> org.apache.poi </groupid> <artifactid> poi </artifactid> <bersion> 3.8 </dependency> </fexendencies> <빌딩> <finalname> exceldemo1 </finalname> </project> </project>
해당 Java 테스트 코드는 다음과 같습니다.
패키지 ExcelDemo1; import java.io.file; import java.io.fileoutputStream; import java.io.outputStream; import org.apache.poi.hssf.usermodel.hssfrow; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; 공개 클래스 ExcelDemo0 { / ** * Java는 Excel 파일을 생성하여 디스크에 작성하여 디스크 * * * * @author : tuzongxun * @title : main * @param @param args * @return void * @date apr 28,2016 7:32:52 PM * @throws * / public static void (문자열 [] args) { // c :/user/tuzongxun123/데스크탑 데스크탑, Windows와 Linux의 슬래시는 다르며 Java는 "/"가 처리해야합니다. file.separator는 크로스 플랫폼 파일을 구현할 수 있습니다. 파일 = 새 파일 ( "c :" + file.separator + "user" + file.separator + "tuzongxun123" + file.separator + "goodtop" + file.separator + "iofile" + file.separator + "user.xls"); try {outputStream outputStream = new FileOutputStream (파일); // Excel 파일을 만들고 여기에서 HSSF는 Excel2007 및 이전 버전에서 사용할 수 있으며 2007 년 이후에는 사용할 수 없습니다. XSSF HSSFWorkbook Workbook = New HSSFWorkbook (); // Excel 워크 시트 생성 hssfsheet sheet = workbook.createsheet ( "사용자"); // 워크 시트에 행을 추가합니다 hssfrow row = sheet.createrow (0); // 지정된 행 행에 두 셀을 추가합니다 .CreateCell (0) .SetCellValue ( "name"); row.createCell (1) .setCellValue ( "비밀번호"); // 출력 스트림을 호출하여 Excel 파일을 Disk Workbook.write (outputStream)에 씁니다. // 출력 스트림을 닫습니다. outputStream.close (); } catch (예외 e) {e.printstacktrace (); }}} 패키지 ExcelDemo1; import java.io.bufferedInputStream; import java.io.file; import java.io.fileInputStream; 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.poifs.filesystem.poifsfilesystem; / ** * Excel 파일 읽기 * * @author tuzongxun123 * */ public class exceldemo2 {public static void main (string [] agrs) {try {// 엑셀 파일 입력 스트림 파일 파일 파일 파일 파일 inputstream = new fileInputStream ( "c :" + file.separator + " +" + " +" + " +" + " +" file.separator + "Desktop" + file.separator + "iofile" + file.separator + "user.xls"); bufferedInputStream bufferedInputStream = newbufferedInputStream (fileInputStream); poifsfilesystem filesystem = new poifsfilesystem (bufferedInputStream); // Excel 파일 가져 오기 hssfworkbook hssfworkbook = new Hssfworkbook (FileSystem); // 이름을 기반으로 지정된 Excel 워크 시트를 가져옵니다. // 실제로 시트를 사용하여 (int i = 1 ;; i ++) {hssfrow row = sheet.getrow (i); if (row! = null) {String namestring1 = row.getCell (0) .getStringCellValue (); 문자열 암호 = row.getCell (i) .getStringCellValue (); System.out.println ( "이름 :" + namestring1); System.out.println ( "비밀번호 :" + 비밀번호); bufferedInputStream.close (); } else {bufferedInputStream.close (); 반품; }}} catch (예외 e) {e.printstacktrace (); }}}위의 내용은이 기사에 관한 모든 것입니다. 모든 사람들이 Java 프로그래밍을 배우는 것이 도움이되기를 바랍니다.