package com.example.client.service;
|
|
import org.springframework.stereotype.Service;
|
|
@Service
|
public class ExcelToPdfService {/*
|
public static boolean getLicense() {
|
boolean result = false;
|
try {
|
InputStream is = ExcelToPdfService.class.getClassLoader().getResourceAsStream("\\license.xml");
|
License aposeLic = new License();
|
aposeLic.setLicense(is);
|
result = true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
*//**
|
* excel 转为pdf 输出。
|
*
|
* @param sourceFilePath 原始excel路径
|
* @param desFilePathd 输出的PDF路径
|
*//*
|
public static void convertToPdf(String sourceFilePath, String desFilePathd) {
|
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
|
return;
|
}
|
try {
|
Workbook wb = new Workbook(sourceFilePath);
|
|
FileOutputStream fileOS = new FileOutputStream(desFilePathd);
|
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
|
// 设置为true,表示将整个sheet打印到pdf的一整夜,导致pdf不分页
|
// 设置为false,又会有格式问题,暂未解决
|
pdfSaveOptions.setOnePagePerSheet(true);
|
|
*//*int[] autoDrawSheets = {1300};
|
//当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
|
autoDraw(wb,autoDrawSheets);*//*
|
|
int[] showSheets = {0};
|
//隐藏workbook中不需要的sheet页。
|
printSheetPage(wb, showSheets);
|
wb.save(fileOS, pdfSaveOptions);
|
fileOS.flush();
|
fileOS.close();
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
*//**
|
* 隐藏workbook中不需要的sheet页。
|
*
|
* @param wb
|
* @param page 显示页的sheet数组
|
*//*
|
public static void printSheetPage(Workbook wb, int[] page) {
|
for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
|
wb.getWorksheets().get(i).setVisible(false);
|
}
|
if (null == page || page.length == 0) {
|
wb.getWorksheets().get(0).setVisible(true);
|
} else {
|
for (int i = 0; i < page.length; i++) {
|
wb.getWorksheets().get(i).setVisible(true);
|
}
|
}
|
}
|
|
public void ExcelToPdf (String filePath,String desFilePathd) {
|
convertToPdf(filePath, desFilePathd);
|
}
|
*/}
|