excel 表格
轉成功后的 pdf文件

話不多說直接上代碼
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
public class Excel2PdfUtil {
/**
* @param args
*/
public static void main(String[] args) {
excel2Pdf("D://cccc/sss.xlsx","D://cccc/sss.pdf");
}
/**
* 獲取license
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try {
InputStream license = Excel2PdfUtil.class.getClassLoader().getResourceAsStream("\\license.xml");// license路徑
License aposeLic = new License();
aposeLic.setLicense(license);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void excel2Pdf(String excelPath, String pdfPath) {
long old = System.currentTimeMillis();
// 驗證License
if (!getLicense()) {
return;
}
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
File excelFile = new File(excelPath);
if (excelFile.exists()) {
fileInputStream = new FileInputStream(excelFile);
Workbook workbook = new Workbook(fileInputStream);
File pdfFile = new File(pdfPath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);//把內容放在一張PDF 頁面上;
FileOutputStream fileOS = new FileOutputStream(pdfFile);
workbook.save(fileOS, pdfSaveOptions);// 只放一張紙;我的專為橫向了
long now = System.currentTimeMillis();
System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + pdfFile.getPath());
} else {
System.out.println("文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
需要依賴的maven jar包
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
-----------------------------------------------------------------------
個人筆記,僅供參考
