第一步代码放进新建的类
public static String word(String from,String to) throws Exception {
if (!getWordLicense()) throw new Exception("com.aspose.words lic ERROR!");
String suffixStr = to.substring(to.lastIndexOf(".")+1);
int suffix=com.aspose.words.SaveFormat.UNKNOWN;
if(suffixStr.equals("doc")) {
suffix= com.aspose.words.SaveFormat.DOC;
}else if(suffixStr.equals("docx")) {
suffix= com.aspose.words.SaveFormat.DOCX;
}else if(suffixStr.equals("pdf")) {
suffix= com.aspose.words.SaveFormat.PDF;
}else if(suffixStr.equals("html")) {
suffix= com.aspose.words.SaveFormat.HTML;
}else if(suffixStr.equals("txt")) {
suffix= com.aspose.words.SaveFormat.TEXT;
}
File file = new File(to); //新建一个空白pdf文档
try {
long old = System.currentTimeMillis();
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(from); //Address是将要被转化的word文档
doc.save(to, suffix);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
} catch (Exception e) {
e.printStackTrace();
}
//return file.getName();
return file.getPath();
}
public static String excel(String form,String to) throws Exception {
if (!getExcelLicense()) throw new Exception("com.aspose.cells lic ERROR!");
String suffixStr = to.substring(to.lastIndexOf(".")+1);
int suffix=com.aspose.cells.SaveFormat.UNKNOWN;
if(suffixStr.equals("xlsx")) {
suffix= com.aspose.cells.SaveFormat.XLSX;
}else if(suffixStr.equals("xls")) {
suffix= com.aspose.cells.SaveFormat.EXCEL_97_TO_2003;
}else if(suffixStr.equals("pdf")) {
suffix= com.aspose.cells.SaveFormat.PDF;
}else if(suffixStr.equals("html")) {
suffix= com.aspose.cells.SaveFormat.HTML;
}else if(suffixStr.equals("csv")) {
suffix= com.aspose.cells.SaveFormat.CSV;
}else if(suffixStr.equals("svg")) {
suffix= com.aspose.cells.SaveFormat.SVG;
}else {
suffix= com.aspose.cells.SaveFormat.AUTO;
}
File file = new File(to);// 输出路径
try {
long old = System.currentTimeMillis();
Workbook wb = new Workbook(form);// 原始excel路径
FileOutputStream fileOS = new FileOutputStream(file);
wb.save(fileOS,suffix);
fileOS.close();
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + file.getPath()); //转化过程耗时
} catch (Exception e) {
e.printStackTrace();
}
//return file.getName();
return file.getPath();
}
public static String ppt(String form,String to) throws Exception {
// 验证License
if (!getPPTLicense()) throw new Exception("com.aspose.slides lic ERROR!");
String suffixStr = to.substring(to.lastIndexOf(".")+1);
int suffix=0;
if(suffixStr.equals("ppt")) {
suffix= com.aspose.slides.SaveFormat.Ppt;
}else if(suffixStr.equals("pptx")) {
suffix= com.aspose.slides.SaveFormat.Pptx;
}else if(suffixStr.equals("pdf")) {
suffix= com.aspose.slides.SaveFormat.Pdf;
}else if(suffixStr.equals("html")) {
suffix= com.aspose.slides.SaveFormat.Html;
}else if(suffixStr.equals("swf")) {
suffix= com.aspose.slides.SaveFormat.Swf;
}
File file = new File(to);// 输出pdf路径
try {
long old = System.currentTimeMillis();
Presentation pres = new Presentation(form);//输入pdf路径
FileOutputStream fileOS = new FileOutputStream(file);
pres.save(fileOS, suffix);
fileOS.close();
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + file.getPath()); //转化过程耗时
} catch (Exception e) {
e.printStackTrace();
}
//return file.getName();
return file.getPath();
}
private static boolean getWordLicense(){
boolean result = false;
try {
InputStream is =AsposeOfficeUtil.class.getClassLoader().getResourceAsStream("license.xml");
com.aspose.words.License aposeLic = new com.aspose.words.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
}
return result;
}
private static boolean getExcelLicense(){
boolean result = false;
try {
InputStream is =AsposeOfficeUtil.class.getClassLoader().getResourceAsStream("license.xml");
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
}
return result;
}
private static boolean getPPTLicense(){
boolean result = false;
try {
InputStream is =AsposeOfficeUtil.class.getClassLoader().getResourceAsStream("license.xml");
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
}
return result;
}
第二步建个xml文件,把下面代码写进去
<xml version="1.0" encoding="UTF-8"?>
<License>
<Data>
<Products>
<Product>Aspose.Words for Java</Product>
<Product>Aspose.Slides for Java</Product>
<Product>Aspose.Cells for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>29991231</SubscriptionExpiry>
<LicenseExpiry>29991231</LicenseExpiry>
<SerialNumber>---</SerialNumber>
</Data>
<Signature>---</Signature>
</License>
最后jar 所需要3个jar包 在我的资源里面