Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to realize online Preview

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use java to achieve online preview. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The details are as follows:

# introduction

Java to implement online preview function is a requirement that everyone may encounter in their work. If the company has money, you can directly use paid third-party software or cloud online preview service, such as Yongzhong office, office web 365.They all have cloud online preview service, which is for money.

If you want to be free, you can use openoffice, and you need to use other tools (such as swfTools, FlexPaper, etc.). You can refer to this article http://blog.csdn.net/z69183787/article/details/17468039, which is written in detail, and the implementation principle is as follows:

1. Convert word, excel, ppt, txt and other files into pdf files through the third-party tool openoffice

two。 Convert pdf files to swf format files through swfTools

3. Display on the page through the FlexPaper document component.

Of course, if you install Adobe Reader XI, then drag pdf directly to the browser page to open the preview, so that there is no need for steps 2 and 3, as long as the customer installs the pdf reader Adobe Reader XI.

Here I introduce the implementation of word, excel, ppt to html through poi, so that it can be put on the page.

# converting word to html

Package wordToHtml;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStream;import java.util.List;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.apache.commons.io.FileUtils;import org.apache.poi.hwpf.HWPFDocument Import org.apache.poi.hwpf.converter.PicturesManager;import org.apache.poi.hwpf.converter.WordToHtmlConverter;import org.apache.poi.hwpf.usermodel.Picture;import org.apache.poi.hwpf.usermodel.PictureType;import org.w3c.dom.Document;public class PoiWordToHtml {public static void main (String [] args) throws Throwable {final String path = "D:\\ poi-test\\ wordToHtml\"; final String file = "personnel choose department score .doc"; InputStream input = new FileInputStream (path + file); HWPFDocument wordDocument = new HWPFDocument (input) WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter (DocumentBuilderFactory.newInstance (). NewDocumentBuilder (). NewDocument ()); wordToHtmlConverter.setPicturesManager (new PicturesManager () {public String savePicture (byte [] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {return suggestedName;}}); wordToHtmlConverter.processDocument (wordDocument); List pics = wordDocument.getPicturesTable (). GetAllPictures (); if (pics! = null) {for (int I = 0; I < pics.size (); iTunes +) {Picture pic = (Picture) pics.get (I) Try {pic.writeImageContent (new FileOutputStream (path + pic.suggestFullFileName ());} catch (FileNotFoundException e) {e.printStackTrace ();} Document htmlDocument = wordToHtmlConverter.getDocument (); ByteArrayOutputStream outStream = new ByteArrayOutputStream (); DOMSource domSource = new DOMSource (htmlDocument); StreamResult streamResult = new StreamResult (outStream); TransformerFactory tf = TransformerFactory.newInstance (); Transformer serializer = tf.newTransformer (); serializer.setOutputProperty (OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty (OutputKeys.INDENT, "yes") Serializer.setOutputProperty (OutputKeys.METHOD, "html"); serializer.transform (domSource, streamResult); outStream.close (); String content = new String (outStream.toByteArray ()); FileUtils.writeStringToFile (new File (path, "personnel selection department score .html"), content, "utf-8");}}

# converting excel to html

Package excelToHtml;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.List;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.apache.commons.io.FileUtils Import org.apache.poi.hssf.converter.ExcelToHtmlConverter;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hwpf.converter.PicturesManager;import org.apache.poi.hwpf.converter.WordToHtmlConverter;import org.apache.poi.hwpf.usermodel.Picture;import org.apache.poi.hwpf.usermodel.PictureType;import org.w3c.dom.Document;public class PoiExcelToHtml {final static String path = "D:\\ poi-test\\ excelToHtml\"; final static String file = "exportExcel.xls" Public static void main (String args []) throws Exception {InputStream input=new FileInputStream (path+file); HSSFWorkbook excelBook=new HSSFWorkbook (input); ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter (DocumentBuilderFactory.newInstance (). NewDocumentBuilder (). NewDocument ()); excelToHtmlConverter.processWorkbook (excelBook); List pics = excelBook.getAllPictures (); if (pics! = null) {for (int I = 0; I < pics.size (); iTunes +) {Picture pic = (Picture) pics.get (I) Try {pic.writeImageContent (new FileOutputStream (path + pic.suggestFullFileName ());} catch (FileNotFoundException e) {e.printStackTrace ();} Document htmlDocument = excelToHtmlConverter.getDocument (); ByteArrayOutputStream outStream = new ByteArrayOutputStream (); DOMSource domSource = new DOMSource (htmlDocument); StreamResult streamResult = new StreamResult (outStream); TransformerFactory tf = TransformerFactory.newInstance (); Transformer serializer = tf.newTransformer () Serializer.setOutputProperty (OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty (OutputKeys.INDENT, "yes"); serializer.setOutputProperty (OutputKeys.METHOD, "html"); serializer.transform (domSource, streamResult); outStream.close (); String content = new String (outStream.toByteArray ()); FileUtils.writeStringToFile (new File (path, "exportExcel.html"), content, "utf-8");}}

# converting ppt to html

In fact, just ppt to turn the picture, with the picture on the page, click on the next page to show one by one on it. Here is only the process of converting ppt to pictures.

Package pptToImg;import java.awt.Dimension; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import org.apache.poi.hslf.model.TextRun; import org.apache.poi.hslf.record.Slide Import org.apache.poi.hslf.usermodel.RichTextRun; import org.apache.poi.hslf.usermodel.SlideShow; public class PPTtoImage {public static void main (String [] args) {/ / read the PPT file File file = new File ("D:/poi-test/pptToImg/test.ppt"); doPPTtoImage (file);} public static boolean doPPTtoImage (File file) {boolean isppt = checkFile (file) If (! isppt) {System.out.println ("The image you specify don't exit!"); return false;} try {FileInputStream is = new FileInputStream (file); SlideShow ppt = new SlideShow (is); is.close (); Dimension pgsize = ppt.getPageSize (); org.apache.poi.hslf.model.Slide [] slide = ppt.getSlides () For (int I = 0; I < slide.length; iTunes +) {System.out.print ("page" + I + ".) ; TextRun [] truns = slide [I] .getTextRuns (); for (int kumb0politics k)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report