java利⽤第三⽅jar实现excel,word,ppt,txt转pdf格式如果遇到利⽤aspose 把excel转pdf遇到复杂⽂件被拆分成多页的情况,添加如下代码:
PdfSaveOptions pOptions = new PdfSaveOptions();
pOptions.setAllColumnsInOnePagePerSheet(true);
excel.save(destFilePath,pOptions);
⽂件上传代码:
public String fileUpload(HttpServletRequest request, HttpServletResponse response) {
String saveDirectory = ServletContext().getRealPath("/upload");//上传路径
// 将当前上下⽂初始化给 CommonsMutipartResolver (多部分解析器)
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
if (multipartResolver.isMultipart(request)) {
// 将request变成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 获取multiRequest 中所有的⽂件名
Iterator iter = FileNames();
String extName = "";// 扩展名
while (iter.hasNext()) {
// ⼀次遍历所有⽂件
MultipartFile file = ().toString());
if (file != null) {
try {
InputStream is = InputStream();
//获取⽂件md5值判断是否有重复,如果有⽂件就不上传,直接读取
String fileHashCode = MD5Util.md5HashCode32(is);
String originalFileName = OriginalFilename();
//获取扩展名
extName = originalFileName.substring(originalFileName.lastIndexOf("."));
//源⽂件上传地址
String sourceFilePath = saveDirectory + File.separator + fileHashCode + extName;
//⽣成的⽬标⽂件地址
String destinatFilePath = RealPath("/convert")+File.separator+fileHashCode + ".pdf";
// 表⽰⽂件已存在不需要上传
if (new File(sourceFilePath).exists()) {
continue;//跳出本次循环
} else {
// ⽂件转换⼯具⽤于转换⽂件,最好新开线程,对⽂件较⼤的需要等待时间。参数(源⽂件路径,⽬标⽂件路径) vert(sourceFilePath, destinatFilePath);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return "请选择⽂件上传!";
}
return "⽂件上传请求配置错误!";
}
定义函数⽤于判断调⽤哪个转换函数:
public class FileConvertUtils {
public static final Logger log = Logger(FileConvertUtils.class);
public static void convert(final String sourceFilePath ,final String destFilePath){
String fileType = sourceFilePath.substring(sourceFilePath.lastIndexOf("."));
ExecutorService executor = wCachedThreadPool() ;
executor.submit(() -> {
try {
//要执⾏的业务代码,
if(!"".equals(fileType)&&fileType!=null){
if(".doc".equals(fileType)||".docx".equals(fileType)||".wps".equals(fileType)){
OfficeToPdfUtils.word2Pdf(sourceFilePath,destFilePath);
}else if(".txt".equals(fileType)){
OfficeToPdfUtils.word2Pdf(sourceFilePath,destFilePath);
}else if(".xls".equals(fileType)||".xlsx".equals(fileType)){
}else if(".ppt".equals(fileType)||".pptx".equals(fileType)){
OfficeToPdfUtils.ppt2Pdf(sourceFilePath,destFilePath);
}else if(".pdf".equals(fileType)){
}
}
}catch(Exception e) {
e.printStackTrace();
throw new RuntimeException("报错啦!!");
}
});
}
}
⽂件转换核⼼代码:
package com.vert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lls.Workbook;
import com.aspose.pdf.SaveFormat;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.hhwy.fweb.framework.api.utils.IoTools;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
public class OfficeToPdfUtils {
/**
* The constant LOG.
*
*/
private static final Logger LOG = Logger(OfficeToPdfUtils.class);
/**
* 获取license
*
* @return
*/
public static boolean getWordLicense() {
boolean result = false;
try {
InputStream license = new FileInputStream(new ABSPATH() + "l"));// license路径,这⾥我引⽤的是公司的包获取路径, com.aspose.words.License aposeLic = new com.aspose.words.License();
aposeLic.setLicense(license);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static boolean getPPTLicense() {
boolean result = false;
try {
InputStream license = new FileInputStream(new ABSPATH() + "l"));// license路径
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(license);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static boolean getExcelLicense() {
boolean result = false;
try {
try {
InputStream license = new FileInputStream(new ABSPATH() + "l"));// license路径 lls.License aposeLic = new lls.License();
aposeLic.setLicense(license);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void word2Pdf(String resourceFilePath, String destFilePath) {
InputStream wordIn = null;
try {
wordIn = new FileInputStream(new File(resourceFilePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
FileOutputStream fileOS = null;
// 验证License
if (!getWordLicense()) {
<("验证License失败!");
return;
}
try {
Document doc = new Document(wordIn);
fileOS = new FileOutputStream(new File(destFilePath));
// 保存转换的pdf⽂件
doc.save(fileOS, com.aspose.words.SaveFormat.PDF);
} catch (Exception e) {
<("error:", e);
} finally {
try {
if(fileOS != null){
fileOS.close();
}
} catch (IOException e) {
<("error:", e);
}
}
}
public static void ppt2Pdf(String resourceFilePath, String destFilePath){
InputStream wordIn = null;
try {
wordIn = new FileInputStream(new File(resourceFilePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
FileOutputStream fileOS = null;
// 验证License
if (!getPPTLicense()) {
<("验证License失败!");
return;
}
try {
fileOS = new FileOutputStream(new File(destFilePath));
Presentation ppt = new Presentation(wordIn);
//此处不可写SaveFormat.Pdf,这样转换能⽣成⽂件,但是转换的⽂件⽆法打开
ppt.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
} catch (Exception e) {
<("error:", e);
} finally {
try {
if(fileOS != null){
fileOS.close();
fileOS.close();
}
} catch (IOException e) {
<("error:", e);
}
}
}
public static void word2Html(String resourceFilePath, String destFilePath) { InputStream wordIn = null;
try {
wordIn = new FileInputStream(new File(resourceFilePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
FileOutputStream fileOS = null;
// 验证License
if (!getWordLicense()) {
<("验证License失败!");
return;
}
try {
Document doc = new Document(wordIn);
fileOS = new FileOutputStream(new File(destFilePath));
// 保存转换的pdf⽂件
doc.save(fileOS, com.aspose.words.SaveFormat.HTML);
} catch (Exception e) {
excel最强教科书完全版("error:", e);
} finally {
try {
if(fileOS != null){
fileOS.close();
}
} catch (IOException e) {
<("error:", e);
}
}
}
public static void excel2Pdf(String resourceFilePath, String destFilePath){ InputStream in = null;
try {
in = new FileInputStream(new File(resourceFilePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
FileOutputStream fileOS = null;
/
/ 验证License
if (!getExcelLicense()) {
<("验证License失败!");
return;
}
try {
fileOS = new FileOutputStream(new File(destFilePath));
Workbook excel = new Workbook(in);
excel.save(destFilePath,lls.SaveFormat.PDF);
} catch (Exception e) {
<("error:", e);
} finally {
try {
if(fileOS != null){
fileOS.close();
System.out.println("转换已完成!");
}
} catch (IOException e) {
<("error:", e);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论