การแนะนำ
ฉันได้เขียนบทความเกี่ยวกับ POI Online Preview ซึ่งยังกล่าวอีกว่าการใช้ 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
โดยไม่ต้องกังวลใจเพิ่มเติมเพียงอัปโหลดรหัส
แพ็คเกจ com.pdfpreview.util; นำเข้า java.io.file; นำเข้า java.io.fileinputstream; นำเข้า java.io.fileoutputstream; นำเข้า java.io.ioException; นำเข้า java.io.inputstream; นำเข้า Java.io.outtream; java.util.date; นำเข้า com.artofsolving.jodconverter.documentconverter; นำเข้า com.artofsolving.jodconverter.openoffice.connection.openofficeconnection; com.artofsolving.jodconverter.openoffice.converter.openofficedocumentconverter;/** *ใช้ jodconverter (ใช้บริการ OpenOffice) เพื่อแปลงไฟล์ ( *.doc, *.docx, *.xls, *.ppt) เป็นรูปแบบ HTML หรือรูปแบบ PDF * ก่อนใช้โปรดตรวจสอบว่าเปิดใช้งานบริการ OpenOffice หรือไม่ ชื่อกระบวนการ OpenOffice: Software.exe | Software.bin * * @author yjclsx */คลาสสาธารณะ doc2htmlutil {ส่วนตัวคงที่ doc2htmlutil doc2htmlutil; / *** รับอินสแตนซ์ doc2htmlutil*/ สาธารณะแบบคงที่แบบคงที่ doc2htmlutil getDoc2htmlutilinstance () {ถ้า (doc2htmlutil == null) {doc2htmlutil = new doc2htmlutil (); } ส่งคืน doc2htmlutil; } / ** * แปลงไฟล์เป็น html * * @param fromfileinputstream: * @throws ioexception * / สตริงสาธารณะ file2html (inputstream fromfileinputstream, สตริง tofilepath, ประเภทสตริง) โยน ioexception {วันที่ = วันที่ใหม่ (วันที่ใหม่); SimpledateFormat SDF = ใหม่ SimpleDateFormat ("yyyymmddhhmmss"); String timesuffix = sdf.format (วันที่); สตริง docfilename = null; สตริง htmfilename = null; if ("doc" .equals (type)) {docFileName = "doc_" + timesuffix + ".doc"; htmfilename = "doc_" + timesuffix + ".html"; } อื่นถ้า ("docx" .equals (type)) {docfilename = "docx_" + timesuffix + ".docx"; htmfilename = "docx_" + timesuffix + ".html"; } อื่นถ้า ("xls" .equals (ประเภท)) {docfilename = "xls_" + timesuffix + ".xls"; htmfilename = "xls_" + timesuffix + ".html"; } อื่นถ้า ("ppt" .equals (ประเภท)) {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*/ ลอง {OutputStream OS = ใหม่ fileOutputStream (docInputFile); int bytesread = 0; ไบต์ [] บัฟเฟอร์ = ไบต์ใหม่ [1024 * 8]; ในขณะที่ ((bytesRead = fromFileInputStream.read (บัฟเฟอร์))! = -1) {os.write (บัฟเฟอร์, 0, bytesRead); } os.close (); FromFileinputStream.close (); } catch (ioexception e) {} การเชื่อมต่อ OpenOfFicEconnection = ใหม่ socketOpenofficeconnection (8100); ลอง {connection.connect (); } catch (ConnectException E) {System.err.println ("มีข้อผิดพลาดในการแปลงไฟล์โปรดตรวจสอบว่าบริการ OpenOffice เริ่มต้นขึ้นหรือไม่"); } // แปลง DocumentConverter Converter = ใหม่ OpenFficEdocumentCumentConverter (การเชื่อมต่อ); Converter.Convert (DocInputFile, HTMLOUTPUTFILE); Connection.disconnect (); // ลบไฟล์คำหลังจากแปลง docinputfile.delete (); ส่งคืน htmfilename; } / ** * แปลงไฟล์เป็น pdf * * @param fromfileinputstream: * @throws ioexception * / public String file2pdf (inputstream fromfileinputstream, สตริง tofilepath, ประเภทสตริง) โยน ioexception {วันที่ = วันที่ใหม่); SimpledateFormat SDF = ใหม่ SimpleDateFormat ("yyyymmddhhmmss"); String timesuffix = sdf.format (วันที่); สตริง docfilename = null; สตริง htmfilename = null; if ("doc" .equals (type)) {docFileName = "doc_" + timesuffix + ".doc"; htmfilename = "doc_" + timesuffix + ".pdf"; } อื่นถ้า ("docx" .equals (type)) {docfilename = "docx_" + timesuffix + ".docx"; htmfilename = "docx_" + timesuffix + ".pdf"; } อื่นถ้า ("xls" .equals (ประเภท)) {docfilename = "xls_" + timesuffix + ".xls"; htmfilename = "xls_" + timesuffix + ".pdf"; } อื่นถ้า ("ppt" .equals (ประเภท)) {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*/ ลอง {OutputStream OS = ใหม่ fileOutputStream (docInputFile); int bytesread = 0; ไบต์ [] บัฟเฟอร์ = ไบต์ใหม่ [1024 * 8]; ในขณะที่ ((bytesRead = fromFileInputStream.read (บัฟเฟอร์))! = -1) {os.write (บัฟเฟอร์, 0, bytesRead); } os.close (); FromFileinputStream.close (); } catch (ioexception e) {} การเชื่อมต่อ OpenOfFicEconnection = ใหม่ socketOpenofficeconnection (8100); ลอง {connection.connect (); } catch (ConnectException E) {System.err.println ("มีข้อผิดพลาดในการแปลงไฟล์โปรดตรวจสอบว่าบริการ OpenOffice เริ่มต้นขึ้นหรือไม่"); } // แปลง DocumentConverter Converter = ใหม่ OpenFficEdocumentCumentConverter (การเชื่อมต่อ); Converter.Convert (DocInputFile, HTMLOUTPUTFILE); Connection.disconnect (); // ลบไฟล์คำหลังจากแปลง docinputfile.delete (); ส่งคืน htmfilename; } โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่น IOException {doc2htmlutil coc2htmlutil = getDoc2htmlutilinstance (); ไฟล์ไฟล์ = null; FileInputStream FileInputStream = null; ไฟล์ = ไฟล์ใหม่ ("d: /poi-test/exportexcel.xls"); FileInputStream = ใหม่ FileInputStream (ไฟล์); // COC2HTMLUTIL.FILE2HTML (FileInputStream, "d:/poi-test/openoffice/xls", "xls"); coc2htmlutil.file2pdf (fileinputstream, "d:/poi-test/openoffice/xls", "xls"); ไฟล์ = ไฟล์ใหม่ ("D: /poi-test/test.doc"); FileInputStream = ใหม่ FileInputStream (ไฟล์); // COC2HTMLUTIL.FILE2HTML (FileInputStream, "d:/poi-test/openoffice/doc", "doc"); coc2htmlutil.file2pdf (fileinputstream, "d:/poi-test/openoffice/doc", "doc"); ไฟล์ = ไฟล์ใหม่ ("D:/POI-TEST/TEMPLATE รายงานประจำสัปดาห์ template.ppt"); FileInputStream = ใหม่ FileInputStream (ไฟล์); // COC2HTMLUTIL.FILE2HTML (FileInputStream, "d:/poi-test/openOffice/ppt", "ppt"); coc2htmlutil.file2pdf (fileinputstream, "d:/poi-test/openoffice/ppt", "ppt"); ไฟล์ = ไฟล์ใหม่ ("d: /poi-test/test.docx"); FileInputStream = ใหม่ FileInputStream (ไฟล์); // 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; " -nofirstStartWizard ในหน้าต่างคำสั่งในไดเรกทอรี OpenOffice เพื่อเริ่มต้น
ข้างต้นจำเป็นต้องแนะนำแพ็คเกจ Jar ของ Jodconverter ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น