java實(shí)現(xiàn)在線預(yù)覽--poi實(shí)現(xiàn)word、excel、ppt轉(zhuǎn)html的方法
java實(shí)現(xiàn)在線預(yù)覽
- -之poi實(shí)現(xiàn)word、excel、ppt轉(zhuǎn)html,具體內(nèi)容如下所示:
###簡(jiǎn)介
java實(shí)現(xiàn)在線預(yù)覽功能是一個(gè)大家在工作中也許會(huì)遇到的需求,如果公司有錢,直接使用付費(fèi)的第三方軟件或者云在線預(yù)覽服務(wù)就可以了,例如永中office、office web 365(http://www.officeweb365.com/)他們都有云在線預(yù)覽服務(wù),就是要錢0.0
如果想要免費(fèi)的,可以用openoffice,還需要借助其他的工具(例如swfTools、FlexPaper等)才行,可參考這篇文章http://blog.csdn.net/z69183787/article/details/17468039,寫的挺細(xì)的,實(shí)現(xiàn)原理就是:
1.通過(guò)第三方工具openoffice,將word、excel、ppt、txt等文件轉(zhuǎn)換為pdf文件;
2.通過(guò)swfTools將pdf文件轉(zhuǎn)換成swf格式的文件;
3.通過(guò)FlexPaper文檔組件在頁(yè)面上進(jìn)行展示。
當(dāng)然如果裝了Adobe Reader XI,那把pdf直接拖到瀏覽器頁(yè)面就可以直接打開(kāi)預(yù)覽,這樣就不需要步驟2、3了,前提就是客戶裝了Adobe Reader XI這個(gè)pdf閱讀器。
我這里介紹通過(guò)poi實(shí)現(xiàn)word、excel、ppt轉(zhuǎn)html,這樣就可以放在頁(yè)面上了。
###word轉(zhuǎn)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 = "人員選擇系分.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(); i++) {
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, "人員選擇系分.html"), content, "utf-8");
}
}
###excel轉(zhuǎn)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(); i++) {
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");
}
}
###ppt轉(zhuǎn)html
其實(shí)只是ppt轉(zhuǎn)圖片,有了圖片后放到頁(yè)面上去,點(diǎn)擊下一頁(yè)就一張張顯示就可以了。這里只介紹ppt轉(zhuǎn)圖片的過(guò)程。
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) {
// 讀入PPT文件
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; i++) {
System.out.print("第" + i + "頁(yè)。");
TextRun[] truns = slide[i].getTextRuns();
for ( int k=0;k<truns.length;k++){
RichTextRun[] rtruns = truns[k].getRichTextRuns();
for(int l=0;l<rtruns.length;l++){
int index = rtruns[l].getFontIndex();
String name = rtruns[l].getFontName();
rtruns[l].setFontIndex(1);
rtruns[l].setFontName("宋體");
// System.out.println(rtruns[l].getText());
}
}
BufferedImage img = new BufferedImage(pgsize.width,pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.BLUE);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slide[i].draw(graphics);
// 這里設(shè)置圖片的存放路徑和圖片的格式(jpeg,png,bmp等等),注意生成文件路徑
FileOutputStream out = new FileOutputStream("D:/poi-test/pptToImg/pict_"+ (i + 1) + ".jpeg");
javax.imageio.ImageIO.write(img, "jpeg", out);
out.close();
}
System.out.println("success!!");
return true;
} catch (FileNotFoundException e) {
System.out.println(e);
// System.out.println("Can't find the image!");
} catch (IOException e) {
}
return false;
}
// function 檢查文件是否為PPT
public static boolean checkFile(File file) {
boolean isppt = false;
String filename = file.getName();
String suffixname = null;
if (filename != null && filename.indexOf(".") != -1) {
suffixname = filename.substring(filename.indexOf("."));
if (suffixname.equals(".ppt")) {
isppt = true;
}
return isppt;
} else {
return isppt;
}
}
}
ppt轉(zhuǎn)圖片有個(gè)缺陷,就是ppt里不是宋體的字有些可能會(huì)變成框框。
以上都需要引入poi的jar包。
要實(shí)現(xiàn)在線預(yù)覽,只需把轉(zhuǎn)換得到的html在新標(biāo)簽頁(yè)打開(kāi)或者鑲嵌到某塊區(qū)域就可以展現(xiàn)了。
總結(jié)
以上所述是小編給大家介紹的java實(shí)現(xiàn)在線預(yù)覽--poi實(shí)現(xiàn)word、excel、ppt轉(zhuǎn)html的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Apache?Commons?Config管理配置文件核心功能使用
這篇文章主要為大家介紹了Apache?Commons?Config管理和使用配置文件核心深入探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Java String源碼分析并介紹Sting 為什么不可變
這篇文章主要介紹了Java String源碼分析并介紹Sting 為什么不可變的相關(guān)資料,需要的朋友可以參考下2017-02-02
Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能
這篇文章主要介紹了Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能,本文是小編整理的代碼筆記,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05
使用Spring底層組件實(shí)現(xiàn)Aware接口
這篇文章主要介紹了使用Spring底層組件實(shí)現(xiàn)Aware接口,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
20秒教你學(xué)會(huì)java?List函數(shù)排序操作示例
這篇文章主要為大家介紹了20秒教你學(xué)會(huì)List函數(shù)排序操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

