SpringBoot 項(xiàng)目 基于aspose相關(guān)jar包 將excel 轉(zhuǎn)換成pdf 導(dǎo)出
1、依賴的jar包 , jar獲取鏈接 aspose相關(guān)三方j(luò)ar ,下載解壓后,在項(xiàng)目路徑下建一個(gè)libs包,然后將下圖兩個(gè)jar 拷貝至剛新建的libs目錄中
文章來源地址http://www.zghlxwxcb.cn/news/detail-845140.html
2、pom.xml中加入maven引入
<dependency>
<groupId>com.aspose.cells</groupId>
<artifactId>cells-8.5.2 </artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/libs/aspose-cells-8.5.2.jar</systemPath>
<version>8.5.2</version>
</dependency>
<dependency>
<groupId>com.aspose.words</groupId>
<artifactId>words-15.8.0 </artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/libs/aspose-words-15.8.0.jar</systemPath>
<version>15.8.0</version>
</dependency>
2.1 使用SpringBoot打包插件生成jar包的時(shí)候,你會(huì)發(fā)現(xiàn)這個(gè)jar包不會(huì)被打進(jìn)去,進(jìn)而出現(xiàn)錯(cuò)誤。解決這個(gè)問題就需要在maven打包插件中配置一個(gè)includeSystemScope屬性
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--設(shè)置為true,以便把本地的system的jar也包括進(jìn)來-->
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
3、編寫轉(zhuǎn)換工具類 如下
package com.by.excelToPdf;
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class PdfUtil {
/**
* excel 轉(zhuǎn) pdf
* @param is 輸入流
* @return 輸出流
*/
public static ByteArrayOutputStream excel2pdf(ByteArrayInputStream is) {
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
// 驗(yàn)證 License
getLicense();
Workbook wb = new Workbook(is);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
wb.save(bos, pdfSaveOptions);
bos.flush();
bos.close();
} catch (Exception e) {
System.out.println("convert failed");
e.printStackTrace();
}
return bos;
}
/**
* excel 轉(zhuǎn) pdf
*
* @param excelFilePath excel文件路徑
*/
public static void excel2pdf(String excelFilePath) {
excel2pdf(excelFilePath, null, null);
}
/**
* excel 轉(zhuǎn) pdf
*
* @param excelFilePath excel文件路徑
* @param convertSheets 需要轉(zhuǎn)換的sheet
*/
public static void excel2pdf(String excelFilePath, int[] convertSheets) {
excel2pdf(excelFilePath, null, convertSheets);
}
/**
* excel 轉(zhuǎn) pdf
*
* @param excelFilePath excel文件路徑
* @param pdfFilePath pdf文件路徑
*/
public static void excel2pdf(String excelFilePath, String pdfFilePath) {
excel2pdf(excelFilePath, pdfFilePath, null);
}
/**
* excel 轉(zhuǎn) pdf
*
* @param excelFilePath excel文件路徑
* @param pdfFilePath pdf文件路徑
* @param convertSheets 需要轉(zhuǎn)換的sheet
*/
public static void excel2pdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {
try {
pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
// 驗(yàn)證 License
getLicense();
Workbook wb = new Workbook(excelFilePath);
FileOutputStream fileOS = new FileOutputStream(pdfFilePath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOS, pdfSaveOptions);
fileOS.flush();
fileOS.close();
System.out.println("convert success");
} catch (Exception e) {
System.out.println("convert failed");
e.printStackTrace();
}
}
/**
* 獲取 生成的 pdf 文件路徑,默認(rèn)與源文件同一目錄
*
* @param excelFilePath excel文件
* @return 生成的 pdf 文件
*/
private static String getPdfFilePath(String excelFilePath) {
return excelFilePath.split("\\.")[0] + ".pdf";
}
/**
* 獲取 license 去除水印
* 若不驗(yàn)證則轉(zhuǎn)化出的pdf文檔會(huì)有水印產(chǎn)生
*/
private static void getLicense() {
String licenseFilePath = "excel-license.xml";
try {
InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream(licenseFilePath);
License license = new License();
license.setLicense(is);
} catch (Exception e) {
System.out.println("license verify failed");
e.printStackTrace();
}
}
/**
* 隱藏workbook中不需要的sheet頁(yè)。
*
* @param sheets 顯示頁(yè)的sheet數(shù)組
*/
private static void printSheetPage(Workbook wb, int[] sheets) {
for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
wb.getWorksheets().get(i).setVisible(false);
}
if (null == sheets || sheets.length == 0) {
wb.getWorksheets().get(0).setVisible(true);
} else {
for (int i = 0; i < sheets.length; i++) {
wb.getWorksheets().get(i).setVisible(true);
}
}
}
}
4、調(diào)用 工具類中有基于流的方式入?yún)?、文件地址方式入?yún)⒌?,大家可根?jù)自行需要選擇合適的轉(zhuǎn)換方法
public static void main(String[] args) {
String inputFile = "D:/testPdf/222.xlsx";
String outputFile = "D:/testPdf/222.pdf";
PdfUtil.excel2pdf(inputFile, outputFile);
}
5、注意問題,以上轉(zhuǎn)換在windows環(huán)境運(yùn)行一切正常,可能部署到linux環(huán)境會(huì)存在中文亂碼,引起亂碼的原因可能是因?yàn)閘inux環(huán)境無中文相關(guān)字體
- linux環(huán)境查看字段方法 字體路徑/usr/share/fonts
# 刷新字體緩存
fc-cache
# 查看所有字體
fc-list
# 查看所有中文字體
fc-list :lang=zh
- 如果無中文字體 我們可能把windows環(huán)境中的字段上傳至linux字段目錄下,windows環(huán)境字段路徑C:\Windows\Fonts,上傳后安裝字段
yum -y install mkfontscale mkfontdir fontconfig
# mkfontscale:字體擴(kuò)展、mkfontdir:新增字體目錄、fc-cache:刷新緩存
mkfontscale && mkfontdir && fc-cache
- 如果使用docker 容器啟動(dòng)的應(yīng)用服務(wù),則還需要使用掛載卷的方式,將宿主體的字體和容器共享,具體方式即啟動(dòng)容器時(shí) 加上 “-v /usr/share/fonts/:/usr/share/fonts”
文章來源:http://www.zghlxwxcb.cn/news/detail-845140.html
到了這里,關(guān)于使用aspose相關(guān)包將excel轉(zhuǎn)成pdf 并導(dǎo)出的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!