不BB直接上代码:https://gitee.com/lanyanhua/excel-to-pdf.git
写了两种情况的应用:上传的文件转PDF。写代码生成的PDF,直接response流输出和本地文件输出
@PostMapping("/excelToPdf")
public void excelToPdf(MultipartFile excel, HttpServletResponse response) throws Exception{
ByteArrayInputStream is = new ByteArrayInputStream(excel.getBytes());
XSSFWorkbook wb = new XSSFWorkbook(is);
//修改excel
//设置输出格式
response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("statements.pdf", "utf-8"));
OutputStream outputStream = response.getOutputStream();
//转pdf输出
ExcelUtil.toPdf(wb,outputStream);
outputStream.flush();
outputStream.close();
}
@PostMapping("/excelToPdf1")
public void excelToPdf1(HttpServletResponse response) throws Exception{
XSSFWorkbook wb = new XSSFWorkbook();
//修改excel
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
row.setHeightInPoints(50);
//设置大小,
sheet.setColumnWidth(0, 19000);
XSSFCell cell = row.createCell(0);
cell.setCellValue("我是PDF");
XSSFCellStyle cellStyle = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 20);
font.setBold(true);
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
//设置输出格式
response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("statements.pdf", "utf-8"));
OutputStream outputStream = response.getOutputStream();
//转pdf输出
//本地改流就好了
//OutputStream outputStream = new FileOutputStream("/Users/lanyanhua/Desktop/本地文件.pdf");
ExcelUtil.toPdf(wb,outputStream);
outputStream.flush();
outputStream.close();
}
核心代码
/**
* 转pdf
*
* @param wb excel
* @param outputStream 输出流
*/
public static void toPdf(XSSFWorkbook wb, OutputStream outputStream) throws Exception {
// 最后输出
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
wb.write(os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
com.aspose.cells.Workbook we = new com.aspose.cells.Workbook(is);
//隐藏workbook中不需要的sheet页。
printSheetPage(we, new int[]{0});
//转pdf
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
// pdfSaveOptions.setOnePagePerSheet(true);
// pdfSaveOptions.isImageFitToPage(true);
pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
//写入 outputStream
we.save(outputStream, pdfSaveOptions);
is.close();
os.close();
wb.close();
}
/**
* 获取license 去除水印
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = ExcelUtil.class.getClassLoader().getResourceAsStream("\\license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void printSheetPage(com.aspose.cells.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);
}
}
}
看看效果
第二个接口效果
最后讲讲代码
这个包是重点aspose-cells-8.5.2.jar maven仓库可能没有方git上了
代码例子在ExcelToPdfApplication 里面
转PDF的在ExcelUtil 里面
生成PDF有啥问题可以试试 PdfSaveOptions 这个类里面的方法,一开始我用的注释的第一行代码就全在一页打印时就全在一页,后面在源码里翻到了现在这个,感觉其他有啥配置应该也是可以设置的