مقدمة
لقد كتبت مقالة حول 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
دون مزيد من اللغط ، فقط قم بتحميل الرمز.
package com.pdfpreview.util ؛ استيراد java.io.file ؛ استيراد java.io.fileInputStream ؛ استيراد java.io.fileOutputStream ؛ استيراد java.io.ioException ؛ import java.io.inputstream ؛ import java.io java.util.date ؛ استيراد com.artofsolving.jodconverter.documentConverter ؛ استيراد com.artofsolving.jodconverter.openoffice.connection.openofficeConnection ؛ import com.artofsolving.jodconverter com.artofsolving.jodconverter.openoffice.converter.openofficedOcumentConverter ؛/** *استخدم jodconverter (استنادًا إلى خدمة OpenOffice) لتحويل الملفات ( *.doc ، *.docx ، *.xls ، *.ppt) * قبل الاستخدام ، يرجى التحقق مما إذا كان قد تم تمكين خدمة OpenOffice. OpenOffice عملية الاسم: software.exe | software.bin * * author yjclsx */public class doc2htmlutil {private static doc2htmlutil doc2htmlutil ؛ / *** احصل على مثيل doc2htmlutil*/ doc2htmlutil static static public getDoc2htmlutilinstance () {if (doc2htmlutil == null) {doc2htmlutil = new doc2htmlutil () ؛ } return doc2htmlutil ؛ } / ** * تحويل الملف إلى html * * param fromFileInputStream: * throws ioException * / public string file2html (inputStream fromFileInputStream ، سلسلة tofilepath ، نوع السلسلة) يلقي ioexception {date date = new date () ؛ SimplEdateFormat SDF = جديد spiStFormat ("Yyyymmddhhmmss") ؛ string timesuffix = sdf.format (date) ؛ سلسلة docfilename = فارغة ؛ سلسلة htmfilename = فارغة ؛ 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 (type)) {docfiLename = "XLS_" + timesuffix + ".xls" ؛ htmfilename = "xls_" + timesuffix + ".html" ؛ } آخر إذا ("ppt" .equals (type)) {docfiLename = "ppt_" + timesuffix + ".ppt" ؛ htmfilename = "ppt_" + timesuffix + ".html" ؛ } آخر {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 () ؛ / *** قم بإنشاء ملف الإدخال من FileInputStream*/ Try {OutputStream OS = جديد fileOutputStream (docinputFile) ؛ int bytesread = 0 ؛ Byte [] Buffer = New Byte [1024 * 8] ؛ بينما ((bytesRead = fromFileInputStream.Read (Buffer))! = -1) {os.write (Buffer ، 0 ، bytesread) ؛ } os.close () ؛ fromFileInputStream.close () ؛ } catch (IoException e) {} OpenOfficEconnection Connection = New SocketOpenOfficEconnection (8100) ؛ حاول {connection.connect () ؛ } catch (connectexception e) {system.err.println ("يوجد خطأ في تحويل الملف ، يرجى التحقق مما إذا كانت خدمة OpenOffice قد بدأت.") ؛ }. converter.convert (docinputfile ، htmloutputfile) ؛ connection.disconnect () ؛ // حذف ملف الكلمة بعد التحويل docinputfile.delete () ؛ إرجاع htmfilename. } / ** * تحويل الملف إلى pdf * * param fromFileInputStream: * throws ioException * / public string file2pdf (inputStream fromFileInputStream ، سلسلة tofilepath ، نوع السلسلة) يلقي ioexception {date date = new date () ؛ SimplEdateFormat SDF = جديد spiStFormat ("Yyyymmddhhmmss") ؛ string timesuffix = sdf.format (date) ؛ سلسلة docfilename = فارغة ؛ سلسلة htmfilename = فارغة ؛ 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 (type)) {docfiLename = "XLS_" + timesuffix + ".xls" ؛ htmfilename = "xls_" + timesuffix + ".pdf" ؛ } آخر إذا ("ppt" .equals (type)) {docfiLename = "ppt_" + timesuffix + ".ppt" ؛ htmfilename = "ppt_" + timesuffix + ".pdf" ؛ } آخر {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 () ؛ / *** قم بإنشاء ملف الإدخال من FileInputStream*/ Try {OutputStream OS = جديد fileOutputStream (docinputFile) ؛ int bytesread = 0 ؛ Byte [] Buffer = New Byte [1024 * 8] ؛ بينما ((bytesRead = fromFileInputStream.Read (Buffer))! = -1) {os.write (Buffer ، 0 ، bytesread) ؛ } os.close () ؛ fromFileInputStream.close () ؛ } catch (IoException e) {} OpenOfficEconnection Connection = New SocketOpenOfficEconnection (8100) ؛ حاول {connection.connect () ؛ } catch (connectexception e) {system.err.println ("يوجد خطأ في تحويل الملف ، يرجى التحقق مما إذا كانت خدمة OpenOffice قد بدأت.") ؛ }. converter.convert (docinputfile ، htmloutputfile) ؛ connection.disconnect () ؛ // حذف ملف الكلمة بعد التحويل docinputfile.delete () ؛ إرجاع htmfilename ؛ } الفراغ الثابت العام (سلسلة [] args) يلقي ioException {doc2htmlutil coc2htmlutil = getDoc2htmlutilinstance () ؛ ملف الملف = فارغ ؛ 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/weekly report 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: تنفيذ البرامج -Accept = "Socket ، Host = 127.0.0.1 ، Port = 8100 ؛ urp ؛" -nofirststartwizard في نافذة الأوامر في دليل OpenOffice للبدء.
ما سبق يحتاج إلى تقديم حزمة جرة Jodconverter. آمل أن يكون ذلك مفيدًا لتعلم الجميع ، وآمل أن يدعم الجميع wulin.com أكثر.