導入
POIオンラインプレビューに関する記事を書きました。これは、OpenOfficeの使用も実行できると述べています。ここで詳しく紹介します。
実装ロジックが2つあります。
1。JodConverter(OpenOffice Serviceに基づく)を使用して、ファイル(.doc、.docx、.xls、.ppt)をHTML形式に変換します。
2。JodConverter(OpenOffice Serviceに基づく)を使用して、ファイル(.doc、.docx、.xls、.ppt)をPDF形式に変換します。
誰もがHTML形式への変換を理解できるため、オンラインプレビュー機能を実現するブラウザで直接表示できます。 PDF形式に変換するには、ユーザーがAdobe Reader XIをインストールする必要があるため、PDFをブラウザページに直接ドラッグするとプレビューを直接開くことができ、オンラインプレビュー機能も実現できます。
ファイルをHTML形式またはPDF形式に変換します
これ以上苦労せずに、コードをアップロードするだけです。
パッケージ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.Connecnect.net.connecnect.connecnect.net.connect.connect.connecnect.connecnect.connect.connect.connect.connect.connectexputstream; java.util.date; Import com.artofsolving.jodconverter.documentconverter; Import com.artofsolving.jodconverter.openoffice.connection.openofficeconnection; Import com.artofsolving.jodconverter.openoffice.connection.sockopenofficeconnection; 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 doc2htmlutil instance*/ public static synchronized doc2htmlutil getdoc2htmlutilinstance(){if(doc2htmlutil == null){doc2htmlutil = new doc2htmlutil(); } doc2htmlutilを返します。 } / ** *ファイルをhtmlに変換 * * @param fromfileinputStream: * @throws ioexception * / public string file2html(fromfryfileinputStream、string tofilepath、string type)shrows ioexception {date date = new date(); SimpleDateFormat sdf = new SimpledateFormat( "yyyymmddhhmmss"); string timesuffix = sdf.format(date);文字列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; } file htmloutputfile = new file(tofilepath + file.separatorchar + htmfilename);ファイルdocinputfile = new file(tofilepath + file.separatorChar + docfileName); if(htmloutputfile.exists())htmloutputfile.delete(); htmloutputfile.createNewfile(); if(docinputfile.exists())docinputfile.delete(); docinputfile.createNewfile(); / *** fromfileinputStreamから入力ファイルをビルド*/ try {outputStream os = new fileoutputStream(docinputfile); int bytesRead = 0; byte [] buffer = new byte [1024 * 8]; while((bytesRead = fromFileInputStream.Read(バッファー))!= -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( "ファイル変換にエラーがあります。OpenOfficeサービスが開始されているかどうかを確認してください。"); } // documentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docinputfile、htmloutputfile); connection.disconnect(); //変換後に単語ファイルを削除しますdocinputfile.delete(); HTMFILENAMEを返します。 } / ** *ファイルをpdfに変換 * * * @param fromfileinputStream: * @throws ioexception * / public string file2pdf(fromfileinputstream、string tofilepath、string type)throws ioexception {date date = new date(); SimpleDateFormat sdf = new SimpledateFormat( "yyyymmddhhmmss"); string timesuffix = sdf.format(date);文字列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; } file htmloutputfile = new file(tofilepath + file.separatorchar + htmfilename);ファイルdocinputfile = new file(tofilepath + file.separatorChar + docfileName); if(htmloutputfile.exists())htmloutputfile.delete(); htmloutputfile.createNewfile(); if(docinputfile.exists())docinputfile.delete(); docinputfile.createNewfile(); / *** fromfileinputStreamから入力ファイルをビルド*/ try {outputStream os = new fileoutputStream(docinputfile); int bytesRead = 0; byte [] buffer = new byte [1024 * 8]; while((bytesRead = fromFileInputStream.Read(バッファー))!= -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( "ファイル変換にエラーがあります。OpenOfficeサービスが開始されているかどうかを確認してください。"); } // documentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docinputfile、htmloutputfile); connection.disconnect(); //変換後に単語ファイルを削除しますdocinputfile.delete(); HTMFILENAMEを返します。 } public static void main(string [] args)throws ioexception {doc2htmlutil coc2htmlutil = getDoc2htmlutilInstance();ファイルファイル= null; fileInputStream fileInputStream = null; file = new 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 = new 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 = new file( "d:/poi-test/weeklyレポート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 = new 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を変換すると、1つしか選択できません。また、実行する前に、OpenOfficeのサービスを開始する必要があります。ソフトウェアを実行 - Headless -Accept =” Socket、Host = 127.0.0.1、port = 8100; urp; " -nofirstStartWizard OpenOfficeディレクトリのコマンドウィンドウの開始を開始します。
上記では、JodConverterのJARパッケージを紹介する必要があります。私はそれがすべての人の学習に役立つことを願っています、そして、私は誰もがwulin.comをもっとサポートすることを願っています。