* imgpath:图片存放路径
public static void doPPTtoImage(HttpServletRequest request,String filepath,String sourceid) throws TransformerException {
String htmlname="exportPpt"+sourceid+".html";
String path =request.getSession().getServletContext().getRealPath("/view/ppt");
fileExists(path);
String imgpath =request.getSession().getServletContext().getRealPath("/view/ppt/images");
fileExists(imgpath);
String filename=request.getSession().getServletContext().getRealPath("/vod/mp4");
// 读入PPT文件
File file = new File(filename+"/"+filepath);
boolean isppt = checkFile(file);
if (!isppt) {
System.out.println("The image you specify don't exit!");
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();
FileOutputStream out =null;
String imghtml="";
for (int i = 0; i < slide.length; i++) {
System.out.print("第" + i + "页。");
TextRun[] truns = slide[i].getTextRuns();
for ( int k=0;k
";
DOMSource domSource = new DOMSource();
StreamResult streamResult = new StreamResult(out);
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);
out.close();
String ppthtml=""+imghtml+"";
FileUtils.writeStringToFile(new File(path, htmlname), ppthtml, "utf-8");
} catch (FileNotFoundException e) {
System.out.println(e);
// System.out.println("Can't find the image!");
} catch (IOException e) {
// 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")||suffixname.equals(".pptx")) {
isppt = true;
return isppt;
} else {
return isppt;
* ppt07转html
* filename:要读取的文件所在文件夹
* filepath:文件名
* htmlname:生成html名称
* path:html存放路径
* imgpath:图片存放路径
public static void pptTohtml07(HttpServletRequest request,String filepath,String sourceid) throws IOException, TransformerException{
String htmlname="exportPpt"+sourceid+".html";
String path=request.getSession().getServletContext().getRealPath("/view/ppt");
fileExists(path);
String imgpath =request.getSession().getServletContext().getRealPath("/view/ppt/images");
fileExists(imgpath);
String filename=request.getSession().getServletContext().getRealPath("/vod/mp4");
File file = new File(filename+"/"+filepath);
boolean isppt = checkFile(file);
if (!isppt) {
System.out.println("The image you specify don't exit!");
FileInputStream is = new FileInputStream(file);
XMLSlideShow ppt = new XMLSlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
System.out.println(pgsize.width+"--"+pgsize.height);
XSLFSlide[] pptPageXSLFSLiseList=ppt.getSlides();
FileOutputStream out=null;
String imghtml="";
for (int i = 0; i < pptPageXSLFSLiseList.length; i++) {
try {
for(XSLFShape shape : pptPageXSLFSLiseList[i].getShapes()){
if(shape instanceof XSLFTextShape) {
XSLFTextShape tsh = (XSLFTextShape)shape;
for(XSLFTextParagraph p : tsh){
for(XSLFTextRun r : p){
r.setFontFamily("宋体");
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
// clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
// render
pptPageXSLFSLiseList[i].draw(graphics);
String Imgname = imgpath +"/"+ (i+1) + ".jpg";
out = new FileOutputStream(Imgname);
javax.imageio.ImageIO.write(img, "jpg", out);
//图片在html加载路径
String imgs="images/"+(i + 1) + ".jpg";
imghtml+="
";
} catch (Exception e) {
System.out.println(e);
System.out.println("第"+i+"张ppt转换出错");
System.out.println("7success");
DOMSource domSource = new DOMSource();
StreamResult streamResult = new StreamResult(out);
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);
out.close();
String ppthtml=""+imghtml+"";
FileUtils.writeStringToFile(new File(path, htmlname), ppthtml, "utf-8");
今天比较忙,就不贴结果图了。。。
poi操作ppt转html由于ppt都是由一页一页图片组成,所以ppt要先转成图片在放入html页面 /** * ppt03转html * filename:要读取的文件所在文件夹 * filepath:文件名 * htmlname:生成html名称 * path:html存放路径 * imgpath:图片存放路径
把jacob.jar加载到工程里。
在C:\WINDOWS\system32;C:\Program Files\
Java
\jdk1.5.0_04\bin;C:\Program Files\
Java
\jdk1.5.0_04\jre\bin;下添加jacob.dll文件。
OfficeToXML.
java
文件是
实现
代码。
我自己工程用到的代码,现共享出来给大家!
心比天高,仗剑走天涯,保持热爱,奔赴向梦想!低调,谦虚,自律,反思,成长,还算是比较正能量的博主,公益免费传播……内心特别想在AI界做出一些可以推进历史进程影响力的东西(兴趣使然,有点小情怀,也有点使命感呀)……
09-23
https://blog.csdn.net/yjclsx/article/details/51441632
https://blog.csdn.net/qq_369
03
131/article/details/82529676
直接上代码了:
package com.allen.utils;
import
java
.awt.Color;
import
java
.awt...
- -之
poi
实现
word、excel、
ppt
转
html
###简介
java
实现
在线
预览
功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云
在线
预览
服务就可以了,例如永中office、office web 365(http://www.officeweb365.com/)他们都有云
在线
预览
服务,就是要钱0.0
如果想要免费的,可以用openoffice,还需要借助其他的工具(例如swfTools、FlexPaper等)才行,可参考这篇文章http://
使用jacob时,把dll放到window/system32下就成,不需要注册
1、如果出现下面的错误
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Version
Description: An unknown COM error has o
PPT
转
Html
5(带动画,自动发布Nginx,支持大文件拆分)
为什么要将
PPT
转
Html
5
众所周知,
PPT
常用于会议演示、教学演示等应用场景,常用的载体有office,wps等,这些基本可以满足我们日常工作需要。但对于有深层次需求的个人或企业,需要在自有平台上展示
PPT
,又需要保护
PPT
不被盗用,又需要保留
PPT
原有动画,这时候常规的office或wps就无法直接对接到平台。因此,需要通过编程的方式,将
PPT
转换
成
Html
5。
1、
java
环境
2、Python环境
3、Nginx环境
要在
Java
中使用Apache
POI
将Excel
转换
为
HTML
并含图片,你可以使用Apache
POI
和ApacheI-OOXML依赖项来完成此
操作
下面是一个简单的示例代码:
import org.apache.
poi
.ss.usermodel.*;
import org.
poi
.xssf.usermodel.XSSFWorkbook;
import org.apache.
poi
.xssf.usermodel.XSSFPictureData;
import
java
.io.*;
import.util.Base64;
public ExcelTo
Html
Converter {
static void main(String[] args) try {
//读取Excel文件
fileInputStream = new FileInputStream("input.xlsx");
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
// 创建
HTML
输出流
FileOutputStream fileOutputStream = new FileOutputStream("output.
html
");
PrintWriter printWriter = new PrintWriter(fileOutputStream);
// 输出
HTML
头部
printWriter.println("<!DOCTYPE
html
>");
printWriter.println("<
html
>");
printWriter.println("<head>");
printWriter.println("<title>Excel to
HTML
</title>");
printWriter.println("</head>");
printWriter.println("<body>");
// 遍历每行
for (Row row : sheet) {
printWriter.println("<tr>");
// 遍历每个单元格
for (Cell cell : row) {
printWriter.print("<td>");
// 获取单元格内容
CellType cellType = cell.getCellType();
if (cellType == CellType.STRING) {
printWriter.print(cell.getStringCellValue());
} else if (cellType == CellType.NUMERIC) {
printWriter.print(cell.getNumericCellValue());
} else if (cellType == CellType.BOOLEAN) {
printWriter.print(cell.getBooleanCellValue());
// 检查单元格是否包含图片
if (cell instanceof XSSFSheet) {
XSSFSheet xssfSheet = (XSSFSheet) sheet;
for (
POI
XMLDocumentPart part : xssfSheet.getRelations()) {
if (part instanceof XSSFPictureData) {
XSSFPictureData pictureData = (XSSFPictureData) part;
String base64Image = Base64.getEncoder().encodeToString(pictureData.getData());
printWriter.print("<img src=\"data:image/png;base64," + base64Image + "\"/>");
printWriter.println("</td>");
printWriter.println("</tr>");
// 输出
HTML
尾部
printWriter.println("</body>");
printWriter.println("</
html
>");
printWriter.close();
System.out.println("Excel
转换
为
HTML
成功!");
} catch (IOException e) {
e.printStackTrace();
此代码将读取名为input.xlsx的Excel文件,并将其
转换
为output.
html
的
HTML
文件。在
HTML
中,它将遍历每个单元格并将其内容输出为
HTML
表格。如果单元格包含图片,它将使用Base64编码将图片嵌入到
HTML
中。
请注意,此代码仅处理XLSX格式的Excel文件。如果你的文件是XLS格式,你需要使用HSSFWorkbook和HSSFPictureData来进行处理。
希望这可以帮助到你!