Java implements the Online Preview sample code (openOffice implementation) and the sample code openoffice

Source: Internet
Author: User

Java implements the Online Preview sample code (openOffice implementation) and the sample code openoffice

Introduction

I have written an article about online preview of poi, which also says that openOffice can also be used. Here I will introduce it in detail.

There are two implementation Logics:

Use jodconverter(based on the openofficeservice documentation to convert the file (.doc、.docx、.xls、.ppt) to the html format.

Second, use jodconverter(based on the openofficeservice license to convert the file (.doc).docx).xls#.ppt) to pdf format.

Everyone can understand the conversion to html format, so that you can view it directly in the browser, and then implement the online preview function. to convert it to pdf format, you need to install Adobe Reader XI, in this way, you will find that you can directly open the preview by dragging the pdf file directly to the browser page, thus implementing the online preview function.

Convert a file to html or pdf Format

If you don't have much to say, go directly to the code.

Package com1_preview. 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. connectException; import java. text. simpleDateFormat; import java. util. date; import com. artofsolving. jodconverter. documentConverter; import com. artofsolving. jodconverter. openoffice. connection. openO FficeConnection; import com. artofsolving. jodconverter. openoffice. connection. socketOpenOfficeConnection; import com. artofsolving. jodconverter. openoffice. converter. openOfficeDocumentConverter;/*** use jodconverter (based on the OpenOffice service) (*. doc ,*. docx ,*. xls ,*. ppt) is converted to html or pdf format. * check whether the OpenOffice service is enabled before use. The OpenOffice process name is soffice.exe | soffice. bin ** @ author yjclsx */public class Doc2HtmlUtil {private sta Tic lutil;/*** get Doc2HtmlUtil instance */public static synchronized doc2HtmlUtil finished () {if (Doc2HtmlUtil = null) {Doc2HtmlUtil = new doc2HtmlUtil ();} return doc2HtmlUtil;}/*** convert the file to html ** @ param fromFileInputStream: * @ throws IOException */public String file2Html (InputStream fromFileInputStream, String toFilePath, String type) throws IOException {Date Date = new Date (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmss"); String timesuffix = sdf. format (date); 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;} File htmlOutputFile = new File (toFilePath + File. separatorChar + htmFileName); File docInputFile = new File (toFilePath + File. sepa RatorChar + docFileName); if (htmlOutputFile. exists () htmlOutputFile. delete (); htmlOutputFile. createNewFile (); if (docInputFile. exists () docInputFile. delete (); docInputFile. createNewFile ();/*** build the input file by fromFileInputStream */try {OutputStream OS = new FileOutputStream (docInputFile); int bytesRead = 0; byte [] buffer = new byte [1024*8]; while (bytesRead = fromFileInputStream. read (buffer ))! =-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 ("file conversion error. Check whether the OpenOffice service is enabled. ");} // Convert DocumentConverter converter = new OpenOfficeDocumentConverter (connection); converter. convert (docInputFile, htmlOutputFile); connection. disconnect (); // after the conversion, delete the word file docInputFile. delete (); return htmFileName;}/*** convert the file to pdf ** @ param fromFileInputStream: * @ throws IOException */public String file2pdf (InputStream fromFileInputStream, String toFilePath, string type) throws IOExcep Tion {Date date = new Date (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmss"); String timesuffix = sdf. format (date); 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 _ "+ t Imesuffix + ". 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); File docInputFile = new File (toFilePath + F Ile. separatorChar + docFileName); if (htmlOutputFile. exists () htmlOutputFile. delete (); htmlOutputFile. createNewFile (); if (docInputFile. exists () docInputFile. delete (); docInputFile. createNewFile ();/*** build the input file by fromFileInputStream */try {OutputStream OS = new FileOutputStream (docInputFile); int bytesRead = 0; byte [] buffer = new byte [1024*8]; while (bytesRead = fromFileInputStream. read (buff Er ))! =-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 ("file conversion error. Check whether the OpenOffice service is enabled. ");} // Convert DocumentConverter converter = new OpenOfficeDocumentConverter (connection); converter. convert (docInputFile, htmlOutputFile); connection. disconnect (); // after the conversion, delete the word file docInputFile. delete (); return htmFileName;} public static void main (String [] args) throws IOException {Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance (); File file = 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 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 = 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 ");}}

When converter. convert (docInputFile, htmlOutputFile) is executed, jodconverter converts the file type name to the corresponding file.

Note: When calling file2Html and file2pdf in the main method, an error is reported, either html or pdf. You can only select one file. Also, before executing the command, you need to start the openOffice service: Execute soffice-headless-accept = "socket, host = 127.0.0.1, port = 8100; urp in the Command window under the openOffice directory; "-nofirststartwizard can be started.

The jar package of jodconverter needs to be introduced above. I hope it will be helpful for everyone's learning, and I hope you can support the house of helping customers more.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.