JAVA:Excel文档转无水印PDF文档
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JAVA:Excel文档转无水印PDF文档,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2743字,纯文字阅读大概需要4分钟。
内容图文
背景
新年初始,万物复苏,正月未过,给大家拜个晚年了!
一个小功能,分享一下。
准备
首先下载文件
链接: https://pan.baidu.com/s/1xvZYc13QVl1OtvFZ13gyDQ .
提取码: gkrc
我这里用的是Aspose的jar包,因为它是收费的所以不交钱会有水印,需要自己破解一下。破解用到的文件就是下载中的xml文件。
实现
将文件放到下图所示文件。
package com.demo.demo.controller;
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* @ClassName ExcelToPdfUtil
* 功能详细描述:
* @Author GuoZiYue
* @Date 2021/2/18 11:08
* @Version 1.0
*/
public class ExcelToPdfUtil {
/**
* excel 转为pdf 输出。
*
* @param sourceFilePath excel文件
* @param desFilePathd pad 输出文件目录
*/
public static void excel2pdf(String sourceFilePath, String desFilePathd ){
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
try {
Workbook wb = new Workbook(sourceFilePath);// 原始excel路径
FileOutputStream fileOS = new FileOutputStream(desFilePathd);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
int sheetCount = wb.getWorksheets().getCount();
int[] showSheets=new int[sheetCount];
for (int i = 0; i < sheetCount; i++) {
showSheets[i]=i;
}
//隐藏workbook中不需要的sheet页。
autoDraw(wb,showSheets);
printSheetPage(wb,showSheets);
wb.save(fileOS, pdfSaveOptions);
fileOS.flush();
fileOS.close();
System.out.println("完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = FileController.class.getClassLoader().getResourceAsStream("\\templates\\license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 设置打印的sheet 自动拉伸比例
* @param wb
* @param page 自动拉伸的页的sheet数组
*/
public static void autoDraw(Workbook wb,int[] page){
if(null!=page&&page.length>0){
for (int i = 0; i < page.length; i++) {
wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
}
}
}
/**
* 隐藏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);
}
}
}
}
它支持多sheet转pdf,每个sheet分为一页。
调用
public static void main(String[] args) {
String sourceFilePath="C:/excel2pdf/aa.xlsx";
String desFilePath="d:/rest222.pdf";
ExcelToPdfUtil.excel2pdf(sourceFilePath,desFilePath);
}
直接调方法就行。
内容总结
以上是互联网集市为您收集整理的JAVA:Excel文档转无水印PDF文档全部内容,希望文章能够帮你解决JAVA:Excel文档转无水印PDF文档所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。