word与excel转pdf的实现
用aspose实现word与excel转成pdf的功能,附件是myeclipse工程,导入即可运行,生成的pdf文件在与源word或excel文件相同目录下,无水印,无文件大小限制。
资源下载地址: http://download.csdn.net/detail/lihong22/9700174
package pdf;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Locale;
import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
public class ExeclAndWordToPdf
{
/**
* 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转<br>
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String fileName = "";
/*String filePath = "C:\\Users\\Ai\\Desktop\\临时文件\\外包任务推送操作手册.docx";
String filePathWord = wordToPdf(filePath,fileName);
System.out.println("word生成pdf文件的路径:"+filePathWord);*/
String filePath = "C:\\Users\\Ai\\Desktop\\临时文件\\工作簿1.xlsx";
String filePathExcel = excelToPdf(filePath,fileName);
System.out.println("excel生成pdf文件的路径:"+filePathExcel);
}
/**
* Excel生成PDF文件
*
* @param filePath
* @throws Exception
*/
public static String excelToPdf(String filePath,String fileName) throws Exception {
// 验证License
if (!getExcelLicense()) {
return "Excel license fault!";
}
String filePathBorf = filePath.substring(0, filePath.lastIndexOf("\\")+1);
if ("".equals(fileName)) {
fileName = filePath.substring(filePath.lastIndexOf("\\")+1, filePath.indexOf("."));
}
long old = System.currentTimeMillis();
Workbook wb = new Workbook(filePath);// 原始excel路径
String filePathTemp = filePathBorf + fileName+ ".pdf";
File pdfFile = new File(filePathTemp);// 输出路径
FileOutputStream fileOS = new FileOutputStream(pdfFile);
wb.save(fileOS, SaveFormat.PDF);
long now = System.currentTimeMillis();
System.out.println("共耗时" + ((now - old) / 1000.0) + "秒");
return filePathTemp;
}
/**
* word生成PDF文件
*
* @param filePath
* @throws Exception
*/
public static String wordToPdf(String filePath,String fileName) throws Exception {
// 验证License
if (!getWordLicense()) {
return "Word license fault!";
}
long old = System.currentTimeMillis();
// 打开文档实例
Document doc = new Document(filePath);
String filePathBorf = filePath.substring(0, filePath.lastIndexOf("\\")+1);
if ("".equals(fileName)) {
fileName = filePath.substring(filePath.lastIndexOf("\\")+1, filePath.indexOf("."));
}
String filePathTemp = filePathBorf + fileName+ ".pdf";
doc.save(filePathTemp, com.aspose.words.SaveFormat.PDF);
long now = System.currentTimeMillis();
System.out.println("共耗时" + ((now - old) / 1000.0) + "秒");
return filePathTemp;
}
/**
* 获取license
*
* @return
*/
public static boolean getExcelLicense() {
boolean result = false;
try {
InputStream is = ExeclAndWordToPdf.class.getClassLoader().getResourceAsStream("\\license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 获取license
*
* @return
*/
public static boolean getWordLicense() {
boolean result = false;
try {
InputStream is = ExeclAndWordToPdf.class.getClassLoader().getResourceAsStream("\\license.xml");
com.aspose.words.License aposeLic = new com.aspose.words.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
推荐阅读
-
Python实现的矩阵转置与矩阵相乘运算示例
-
在用Word制作表格过程中如何将表的行与列转置
-
Java实现Word/Pdf/TXT转html
-
Word Excel 实现表格的行列转置将表的行与列互转
-
Excel实现数值与文本的相互转换即文本变数值与数值变文本
-
Word利用书签与超链接实现文档中段落之间的交互跳转
-
Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
-
php用windows COM组件调用openoffice接口实现word转pdf文件时报错的解决办法
-
jsp实现针对excel及word文档的打印方法
-
php导出word文档与excel电子表格的简单示例代码