使⽤aspose将excel、word转pdf
1、导⼊jar包
aspose-words-15.8.0-jdk16.jar(word的jar包)、aspose-cells.jar(excel的jar包)
2、excel转pdf代码
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import lls.License;
import lls.PdfSaveOptions;
import lls.SaveFormat;
import lls.Workbook;
public class ExcelToPdf {
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = ClassLoader().getResourceAsStream("l"); // l应放在..\WebRoot\WEB-INF\classes路径下 License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void excel2pdf(String Address) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf⽂档会有⽔印产⽣
return;
}
try {
File pdfFile = new File("C:\\Users\\Desktop\\pdf4.pdf");// 输出路径
Workbook wb = new Workbook(Address);// 原始excel路径excel最强教科书完全版pdf
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
FileOutputStream fileOS = new FileOutputStream(pdfFile);
wb.save(fileOS, SaveFormat.PDF);
fileOS.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
excel2pdf("C:\\Users\\Desktop\\1.xlsx");
}
3、word转excel代码
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.words.Document;
import com.aspose.words.License;
public class Word2Pdf {
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = ClassLoader().getResourceAsStream("l"); // l应放在..\WebRoot\WEB-INF\classes路径下
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void word2Pdf(String Address) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf⽂档会有⽔印产⽣
return;
}
try {
File file = new File("C:\\Users\\Desktop\\pdf3.pdf"); //新建⼀个pdf⽂档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(Address); //Address是将要被转化的word⽂档
doc.save(os, com.aspose.words.SaveFormat.PDF);//全⾯⽀持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换 os.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
word2Pdf("C:\\Users\\Desktop\\1.docx");
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论