소개
POI 온라인 미리보기에 관한 기사를 작성했으며 OpenOffice를 사용할 수도 있다고 말했습니다. 여기에 자세히 소개하겠습니다.
두 가지 구현 로직이 있습니다.
1. JodConverter (OpenOffice 서비스 기반)를 사용하여 파일 (.doc, .docx, .xls, .ppt)을 HTML 형식으로 변환하십시오.
2. JodConverter (OpenOffice 서비스 기반)를 사용하여 파일 (.doc, .docx, .xls, .ppt)을 PDF 형식으로 변환하십시오.
모든 사람은 HTML 형식으로의 전환을 이해할 수 있으므로 온라인 미리보기 기능을 실현하는 브라우저에서 직접 볼 수 있습니다. PDF 형식으로 변환하려면 사용자가 Adobe Reader XI를 설치해야하므로 PDF를 브라우저 페이지로 직접 드래그하면 미리보기를 직접 열 수 있으며 온라인 미리보기 기능도 실현할 수 있습니다.
파일을 HTML 형식 또는 PDF 형식으로 변환하십시오
더 이상 고민하지 않고 코드를 업로드하십시오.
package com.pdfpreview.util; import java.io.file; import java.io.fileInputStream; import java.io.fileoutputStream; import java.io.ioException; import java.io.inputStream; import java.io.outputStream; import java.net.net.connectection; java.text. java.util.date; import com.artofsolving.jodconverter.documentConverter; import com.artofSolving.jodConverter.openOffice.connection.openOfficeConnection; import com.artofSolving.jodConverter.openOffice.connection.sucteOffeConnection; 가져 오기; com.artofSolving.jodconverter.openOffice.converter.openofficedocumentConverter;/** *JodConverter (OpenOffice Service 기반)를 사용하여 파일 ( *.doc, *.docx, *.xls, *.ppt)을 HTML 형식 또는 pdf 형식으로 변환합니다. * 사용하기 전에 OpenOffice 서비스가 활성화되었는지 확인하십시오. OpenOffice 프로세스 이름 : Software.exe | Software.bin * * @author yjclsx */public class doc2htmlutil {private static doc2htmlutil doc2htmlutil; / *** get get get get get get doc2htmlutil 인스턴스*/ public static synchronized doc2htmlutil getdoc2htmlutilinstance () {if (doc2htmlutil == null) {doc2htmlutil = new doc2htmlutil (); } return doc2htmlutil; } / ** * 파일 변환 html * * @param fromfileInputStream : * @Throws ioException * / public String file2html (inputStream fromfileInputStream, String tofilepath, String type) IoException {date date = new 날짜 (); simpledateformat sdf = 새로운 simpledateformat ( "yyyymmddhhmmss"); String TimesUffix = sdf.format (날짜); 문자열 docfilename = null; 문자열 htmfilename = null; if ( "doc".equals (type)) {docfilename = "doc_" + timesuffix + ".doc"; htmfilename = "doc_" + timesuffix + ".html"; } else if ( "docx".equals (type)) {docfilename = "docx_" + timesuffix + ".docx"; htmfilename = "docx_" + timesuffix + ".html"; } else if ( "xls".equals (type)) {docfilename = "xls_" + timesuffix + ".xls"; htmfilename = "xls_" + timesuffix + ".html"; } else if ( "ppt".equals (type)) {docfilename = "ppt_" + timesuffix + ".ppt"; htmfilename = "ppt_" + timesuffix + ".html"; } else {return null; } 파일 htmloutputfile = 새 파일 (tofilepath + file.separatorchar + htmfilename); 파일 docinputfile = 새 파일 (tofilepath + file.separatorchar + docfilename); if (htmloutputfile.exists ()) htmloutputfile.delete (); htmloutputfile.createnewfile (); if (docinputfile.exists ()) docinputfile.delete (); docinputfile.createnewfile (); / *** FromfileInputStream에서 입력 파일을 빌드*/ try {outputStream os = 새 FileOutputStream (docInputFile); int bytesread = 0; 바이트 [] 버퍼 = 새로운 바이트 [1024 * 8]; while ((bytesread = fromfileInputStream.read (buffer))! = -1) {os.write (buffer, 0, bytesread); } os.close (); FromfileInputStream.close (); } catch (ioexception e) {} OpenOfficeConnection Connection = New SocketOpenOfficeCeConcection (8100); try {connection.connect (); } catch (ConnectException e) {System.err.println ( "파일 변환에 오류가 있으면 OpenOffice 서비스가 시작되었는지 확인하십시오."); } // DocumentConverter 변환 변환 = 새 OpenOfficedOcumentConverter (Connection); converter.convert (docinputfile, htmloutputfile); Connection.disconnect (); // 변환 후 Word 파일을 삭제합니다. docinputfile.delete (); 반환 htmfilename; } / ** * 파일을 pdf로 변환 * * @param fromfileInputStream : * @throws ioException * / public String file2pdf (inputStream fromfileInputStream, String tofilepath, String Type) IoException {date date = new 날짜 (); simpledateformat sdf = 새로운 simpledateformat ( "yyyymmddhhmmss"); String TimesUffix = sdf.format (날짜); 문자열 docfilename = null; 문자열 htmfilename = null; if ( "doc".equals (type)) {docfilename = "doc_" + timesuffix + ".doc"; htmfilename = "doc_" + timesuffix + ".pdf"; } else if ( "docx".equals (type)) {docfilename = "docx_" + timesuffix + ".docx"; htmfilename = "docx_" + timesuffix + ".pdf"; } else if ( "xls".equals (type)) {docfilename = "xls_" + timesuffix + ".xls"; htmfilename = "xls_" + timesuffix + ".pdf"; } else if ( "ppt".equals (type)) {docfilename = "ppt_" + timesuffix + ".ppt"; htmfilename = "ppt_" + timesuffix + ".pdf"; } else {return null; } 파일 htmloutputfile = 새 파일 (tofilepath + file.separatorchar + htmfilename); 파일 docinputfile = 새 파일 (tofilepath + file.separatorchar + docfilename); if (htmloutputfile.exists ()) htmloutputfile.delete (); htmloutputfile.createnewfile (); if (docinputfile.exists ()) docinputfile.delete (); docinputfile.createnewfile (); / *** FromfileInputStream에서 입력 파일을 빌드*/ try {outputStream os = 새 FileOutputStream (docInputFile); int bytesread = 0; 바이트 [] 버퍼 = 새로운 바이트 [1024 * 8]; while ((bytesread = fromfileInputStream.read (buffer))! = -1) {os.write (buffer, 0, bytesread); } os.close (); FromfileInputStream.close (); } catch (ioexception e) {} OpenOfficeConnection Connection = New SocketOpenOfficeCeConcection (8100); try {connection.connect (); } catch (ConnectException e) {System.err.println ( "파일 변환에 오류가 있으면 OpenOffice 서비스가 시작되었는지 확인하십시오."); } // DocumentConverter 변환 변환 = 새 OpenOfficedOcumentConverter (Connection); converter.convert (docinputfile, htmloutputfile); Connection.disconnect (); // 변환 후 Word 파일을 삭제합니다. docinputfile.delete (); 반환 htmfilename; } public static void main (String [] args)은 ioexception {doc2htmlutil coc2htmlutil = getDoc2htmlutilinstance (); 파일 파일 = null; fileInputStream fileInputStream = null; file = 새 파일 ( "d : /poi-test/exportexcel.xls"); fileInputStream = new FileInputStream (file); // coc2htmlutil.file2html (fileInputStream, "d :/poi-test/openOffice/xls", "xls"); coc2htmlutil.file2pdf (fileInputStream, "d :/poi-test/openOffice/xls", "xls"); file = 새 파일 ( "d : /poi-test/test.doc"); fileInputStream = new FileInputStream (file); // coc2htmlutil.file2html (fileInputStream, "d :/poi-test/openOffice/doc", "doc"); coc2htmlutil.file2pdf (fileInputStream, "d :/poi-test/openOffice/doc", "doc"); file = 새 파일 ( "d :/poi-test/weekly report template.ppt"); fileInputStream = new FileInputStream (file); // coc2htmlutil.file2html (fileInputStream, "d :/poi-test/openOffice/ppt", "ppt"); coc2htmlutil.file2pdf (fileInputStream, "d :/poi-test/openOffice/ppt", "ppt"); file = 새 파일 ( "d : /poi-test/test.docx"); fileInputStream = new FileInputStream (file); // coc2htmlutil.file2html (fileInputStream, "d :/poi-test/openOffice/docx", "docx"); coc2htmlutil.file2pdf (fileInputStream, "d :/poi-test/openOffice/docx", "docx"); }} HTML로 변환하는 프로세스는 PDF로 변환하는 것과 거의 동일합니다. 첫 번째는 출력 파일을 작성할 때 xxx.html로 명명되었으며 후자는 xxx.pdf입니다. converter.convert (docinputfile, htmloutputfile);를 실행할 때 JodConverter는 파일 유형 이름에 따라 해당 파일로 변환합니다.
기본 메소드에서 File2HTML 및 File2PDF가 모두 호출되면 오류 가보고됩니다. HTML 또는 PDF를 변환하면 하나만 선택할 수 있습니다. 또한 실행하기 전에 OpenOffice 서비스를 시작해야합니다. 소프트웨어 실행 -Headless -Accept = "소켓, 호스트 = 127.0.0.1, 포트 = 8100; URP;" -OpenOffice 디렉토리의 명령 창에있는 nofirstStartWizard를 시작합니다.
위의 내용은 JodConverter의 JAR 패키지를 소개해야합니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.