Einführung
Ich habe einen Artikel über die POI -Online -Vorschau geschrieben, in der auch die Verwendung von OpenOffice auch durchgeführt werden kann. Ich werde es hier ausführlich vorstellen.
Ich habe zwei Implementierungslogik:
1. Verwenden Sie JodConverter (basierend auf OpenOffice -Dienst), um Dateien (.doc, .docx, .xls, .ppt) in das HTML -Format zu konvertieren.
2. Verwenden Sie JodConverter (basierend auf OpenOffice -Dienst), um Dateien (.doc, .docx, .xls, .ppt) in PDF -Format umzuwandeln.
Jeder kann die Konvertierung in das HTML -Format verstehen, damit Sie es direkt im Browser anzeigen können, wodurch die Online -Vorschau -Funktion erkennt. Durch die Konvertierung in das PDF -Format muss der Benutzer Adobe Reader XI installieren, sodass Sie feststellen, dass das Ziehen des PDF direkt auf die Browser -Seite die Vorschau direkt öffnen kann, die auch die Online -Vorschau -Funktion erkennt.
Konvertieren Sie Dateien in das HTML -Format oder das PDF -Format
Laden Sie ohne weiteres den Code hoch.
Paket 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.util.date; import com.artofsolving.jodconverter.documentConverter; import com.artofsolving.jodconverter.openoffice.connection com.artofSolving.jodconverter.openoffice.converter.openofficedocumentConverter;/** *Verwenden Sie JodConverter (basierend auf OpenOffice -Dienst), um Dateien ( *.doc, *.docx, *.xls, *.ppt) in das HTML -Format oder das PDF -Format umzuwandeln. * Bevor Sie die Verwendung verwenden, überprüfen Sie bitte, ob der OpenOffice -Dienst aktiviert wurde. OpenOffice -Prozessname: Software.exe | Software.bin * * @author yjclsx */public class doc2htmlutil {private static doc2htmlutil doc2htmlutil; / *** Holen Sie sich doc2htmlutil Instance*/ public static synchronisierte doc2htmlutil getDoc2htmlutilinstance () {if (doc2htmlutil == null) {doc2htmlutil = new Doc2htmlutil (); } return doc2htmlutil; } / ** * Die Datei in html * * @param aus fileInputStream: * @throws ioException * / public String file2Html (inputStream aus fileInputStream, String tofilepath, String -Typ) veröffentlichen. SimpleDateFormat SDF = new SimpledateFormat ("yyyymmddhhmms"); String Timesuffix = SDF.Format (Datum); String docFileName = null; String 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; } Datei htmLoutputFile = neue Datei (tofilpath + file.separatorchar + htmFileName); Datei docInputFile = neue Datei (tofilpath + file.separatorchar + docFileName); if (htmloutputFile.Exists ()) htmloutputFile.delete (); htmLoutputFile.CreateNewFile (); if (docInputFile.exists ()) docInputFile.delete (); docInputFile.CreateNewFile (); / *** Erstellen Sie die Eingabedatei aus fileInputStream*/ try {outputStream os = new FileOutputStream (docInputFile); int bytesRead = 0; byte [] buffer = neues byte [1024 * 8]; while ((byteSread = fromFileInputStream.read (puffer))! = -1) {os.write (buffer, 0, bytesread); } os.close (); fromFileInputStream.close (); } catch (ioException e) {} openOfficeConnection Connection = new SocketopenofficeConnection (8100); try {Connection.Connect (); } catch (ConnectException e) {System.err.println ("Es gibt einen Fehler in der Dateikonvertierung. Überprüfen Sie bitte, ob der OpenOffice -Dienst gestartet wird."); } // DocumentConverter Converter = new OpenOfficedocumentConverter (Verbindung) konvertieren; Converter.Convert (docInputFile, htmLoutputFile); Connection.Disconnect (); // die Wortdatei nach Conversion docInputFile.Delete () löschen; return htmFileName; } / ** * Konvertieren Sie die Datei in pdf * * @param aus fileInputStream: * @throws ioException * / public String file2pdf (inputStream aus fileInputStream, String tofilepath, String -Typ) löst IOException {Datumsdatum = new Date () aus; SimpleDateFormat SDF = new SimpledateFormat ("yyyymmddhhmms"); String Timesuffix = SDF.Format (Datum); String docFileName = null; String 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; } Datei htmLoutputFile = neue Datei (tofilpath + file.separatorchar + htmFileName); Datei docInputFile = neue Datei (tofilpath + file.separatorchar + docFileName); if (htmloutputFile.Exists ()) htmloutputFile.delete (); htmLoutputFile.CreateNewFile (); if (docInputFile.exists ()) docInputFile.delete (); docInputFile.CreateNewFile (); / *** Erstellen Sie die Eingabedatei aus fileInputStream*/ try {outputStream os = new FileOutputStream (docInputFile); int bytesRead = 0; byte [] buffer = neues byte [1024 * 8]; while ((byteSread = fromFileInputStream.read (puffer))! = -1) {os.write (buffer, 0, bytesread); } os.close (); fromFileInputStream.close (); } catch (ioException e) {} openOfficeConnection Connection = new SocketopenofficeConnection (8100); try {Connection.Connect (); } catch (ConnectException e) {System.err.println ("Es gibt einen Fehler in der Dateikonvertierung. Überprüfen Sie bitte, ob der OpenOffice -Dienst gestartet wird."); } // DocumentConverter Converter = new OpenOfficedocumentConverter (Verbindung) konvertieren; Converter.Convert (docInputFile, htmLoutputFile); Connection.Disconnect (); // die Wortdatei nach Conversion docInputFile.Delete () löschen; return htmFileName; } public static void main (String [] args) löst ioException {doc2htmlutil coc2htmlutil = getdoc2htmlutilinstance () aus; Datei Datei = null; FileInputStream FileInputStream = null; file = new File ("d: /poi-test/exportExcel.xls"); fileInputStream = new FileInputStream (Datei); // coc2htmlutil.File2Html (FileInputStream, "D:/poi-test/openOffice/xls", "xls"); coc2htmlutil.file2pdf (FileInputStream, "D:/poi-test/openOffice/xls", "xls"); file = new File ("d: /poi-test/test.doc"); fileInputStream = new FileInputStream (Datei); // coc2htmlutil.File2Html (FileInputStream, "D:/poi-test/openOffice/doc", "doc"); coc2htmlutil.file2pdf (FileInputStream, "D:/poi-test/openOffice/doc", "doc"); Datei = neue Datei ("d:/poi-test/wöchentlich report template.ppt"); fileInputStream = new FileInputStream (Datei); // coc2htmlutil.File2Html (FileInputStream, "D:/poi-test/openOffice/ppt", "ppt"); coc2htmlutil.File2pdf (FileInputStream, "D:/poi-test/openOffice/ppt", "ppt"); file = new File ("d: /poi-test/test.docx"); fileInputStream = new FileInputStream (Datei); // Coc2htmlutil.File2Html (FileInputStream, "D:/poi-test/openOffice/docx", "docx"); coc2htmlutil.File2pdf (FileInputStream, "D:/poi-test/openOffice/docx", "docx"); }} Der Prozess der Konvertierung in HTML ist fast der gleiche Konvertieren in PDF. Der erste wird beim Erstellen der Ausgabedatei mit xxx.html bezeichnet, und letzteres wird mit xxx.pdf bezeichnet. Bei der Ausführung von Converter.convert (docInputFile, htmLoutputFile) wandelt sich JodConverter gemäß dem Namen des Dateityps in die entsprechende Datei um.
Beachten Sie, dass ein Fehler gemeldet wird, wenn beide Datei2HTML und Datei2PDF in der Hauptmethode aufgerufen werden. Konvertieren Sie entweder HTML oder PDF und Sie können nur einen auswählen. Vor der Ausführung müssen Sie den Dienst von OpenOffice beginnen: Software -headless -accept = ”Socket, Host = 127.0.0.1, Port = 8100; URP;" -nofirstStartWizard im Befehlsfenster im OpenOffice -Verzeichnis zu starten.
Die oben genannten muss das JAR -Paket von JodConverter einführen. Ich hoffe, es wird für das Lernen aller hilfreich sein, und ich hoffe, jeder wird Wulin.com mehr unterstützen.