admin管理员组文章数量:1605139
需要用到的jar及文件地址:
都一样的文件,阿里的下载不用会员也会快一点
阿里网盘的:aspose
https://www.aliyundrive/s/ftPjVfh6378
提取码: ae28
百度网盘:aspose
链接: https://pan.baidu/s/1HR-UTu36mubQZ365O8fqhQ 密码: ut45
一、Exl转pdf
1、测试文件:
2、代码
private static final String PATH = "/Users/chenjx/Downloads/zipceshi/测试exl转pdf.xlsx";
private static final String FILE_PATH = "/Users/chenjx/Downloads/zipceshi/pdf/exl" + RandomCodeUtil.generateNumberString(8) + "exl.pdf";
//去除文件水印 在项目的 resources 下
private static final String FOP_EXL = "/file/excel-license.xml";
public static void main(String[] args) {
ExlWordToPdfUtils.excel2pdf(PATH, FILE_PATH);
// ExlWordToPdfUtils.word2Pdf(PATH1, FILE_PATH1);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
*/
public static void excel2pdf(String excelFilePath) {
excel2pdf(excelFilePath, null, null);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
* @param convertSheets 需要转换的sheet
*/
public static void excel2pdf(String excelFilePath, int[] convertSheets) {
excel2pdf(excelFilePath, null, convertSheets);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
* @param pdfFilePath pdf文件路径
*/
public static void excel2pdf(String excelFilePath, String pdfFilePath) {
excel2pdf(excelFilePath, pdfFilePath, null);
}
/**
* excel 转 pdf
*
* @param inputStream exl流
* @param pdfFilePath pdf文件路径
*/
public static void excel2pdf(InputStream inputStream, String pdfFilePath) {
excel2pdf(inputStream, pdfFilePath, null);
}
/**
* excel 转 pdf
*
* @param inputStream exl流
* @param convertSheets 需要的sheet
*/
public static InputStream excel2pdf(InputStream inputStream, int[] convertSheets) {
return excel2pdf1(inputStream, convertSheets);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
* @param pdfFilePath pdf文件路径
* @param convertSheets 需要转换的sheet
*/
public static void excel2pdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {
FileOutputStream fileOs = null;
try {
pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
// 验证 License
getLicenseExl();
Workbook wb = new Workbook(excelFilePath);
fileOs = new FileOutputStream(pdfFilePath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
int[] autoDrawSheets={3};
autoDraw(wb,autoDrawSheets);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOs, pdfSaveOptions);
fileOs.flush();
System.out.println("convert success");
} catch (Exception e) {
System.out.println("convert failed");
e.printStackTrace();
} finally {
try {
assert fileOs != null;
fileOs.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
/**
* excel 转 pdf
*
* @param inputStream exl流
* @param pdfFilePath pdf文件路径
* @param convertSheets 需要转换的sheet
*/
public static void excel2pdf(InputStream inputStream, String pdfFilePath, int[] convertSheets) {
FileOutputStream fileOs = null;
try {
// 验证 License
getLicenseExl();
Workbook wb = new Workbook(inputStream);
fileOs = new FileOutputStream(pdfFilePath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setMergeAreas(true);
//缩放到一个页面(如果列太多 太长)
pdfSaveOptions.setOnePagePerSheet(true);
// pdfSaveOptions.setCalculateFormula(true);
// pdfSaveOptions.setCheckFontCompatibility(true);
// pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
// pdfSaveOptions.setEmbedStandardWindowsFonts(true);
// pdfSaveOptions.setFontSubstitutionCharGranularity(true);
// pdfSaveOptions.setRefreshChartCache(true);
// pdfSaveOptions.setClearData(true);
// int[] autoDrawSheets={3};
// autoDraw(wb,autoDrawSheets);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOs, pdfSaveOptions);
fileOs.flush();
} catch (Exception e) {
System.out.println("convert failed");
e.printStackTrace();
} finally {
try {
inputStream.close();
assert fileOs != null;
fileOs.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
log.info("exl转pdf结束");
}
}
/**
* @param wb 设置打印的sheet自动拉伸比例
* @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();
}
}
}
/**
* excel 转 pdf 返回文件流
*
* @param inputStream exl文件流
* @param convertSheets 需要转换的 sheet
* @return 文件流
*/
public static InputStream excel2pdf1(InputStream inputStream, int[] convertSheets) {
ByteArrayOutputStream fileOs = null;
try {
// 验证 License
getLicenseExl();
Workbook wb = new Workbook(inputStream);
fileOs = new ByteArrayOutputStream();
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOs, pdfSaveOptions);
fileOs.flush();
return new ByteArrayInputStream(fileOs.toByteArray());
} catch (Exception e) {
System.out.println("convert failed");
e.printStackTrace();
} finally {
try {
inputStream.close();
if (fileOs != null) {
fileOs.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
log.info("exl转pdf结束");
}
return null;
}
/**
* 获取 生成的 pdf 文件路径,默认与源文件同一目录
*
* @param excelFilePath excel文件
* @return 生成的 pdf 文件
*/
private static String getPdfFilePath(String excelFilePath) {
return excelFilePath.split(".")[0] + ".pdf";
}
/**
* 获取 license 去除水印
* 若不验证则转化出的pdf文档会有水印产生
*/
private static void getLicenseExl() {
InputStream is = null;
try {
ClassPathResource classPathResource = new ClassPathResource(FOP_EXL);
is = classPathResource.getInputStream();
License license = new License();
license.setLicense(is);
} catch (Exception e) {
System.out.println("license verify failed");
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
/**
* 隐藏workbook中不需要的sheet页。
*
* @param sheets 显示页的sheet数组
*/
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);
}
}
}
3、执行结果:
4、可以看到是有水印的、格式和exl的格式一样,下来加上去水印的 方法调用,放在 excel2pdf方法中
/**
* 获取 license 去除水印
* 若不验证则转化出的pdf文档会有水印产生
*/
private static void getLicenseExl() {
InputStream is = null;
try {
ClassPathResource classPathResource = new ClassPathResource(FOP_EXL);
is = classPathResource.getInputStream();
License license = new License();
license.setLicense(is);
} catch (Exception e) {
System.out.println("license verify failed");
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
5、执行,看倒出结果,水印已被去除
二、word转pdf
word转pdf也需要加上去水印xml 和 以上同理
1、测试模版
2、代码
private static final String PATH1 = "/Users/chenjx/Downloads/zipceshi/测试word转pdf.docx";
private static final String FILE_PATH1 = "/Users/chenjx/Downloads/zipceshi/pdf/word" + RandomCodeUtil.generateNumberString(8) + "word.pdf";
//去水印模版。放在项目resources下
private static final String FOP_WORD = "/file/word-license.xml";
/**
* word 转 pdf
*
* @param wordFile word 文件路径
* @param pdfFile 生成的 pdf 文件路径
*/
public static void word2Pdf(String wordFile, String pdfFile) {
File file = new File(pdfFile);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
getLicenseWord();
FileOutputStream os = null;
try {
os = new FileOutputStream(pdfFile);
Document doc = new Document(wordFile);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
log.error("word转pdf失败", e);
e.printStackTrace();
} finally {
try {
assert os != null;
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
log.info("word转pdf结束");
}
}
/**
* word 转 pdf 生成文件到服务器
*
* @param inputStream word
* @param pdfFile 生成的 pdf 文件路径
*/
public static void word2Pdf(InputStream inputStream, String pdfFile) {
log.info("word转pdf[开始]");
File file = new File(pdfFile);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
getLicenseWord();
FileOutputStream os = null;
try {
os = new FileOutputStream(pdfFile);
Document doc = new Document(inputStream);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
log.error("word转pdf失败", e);
e.printStackTrace();
} finally {
assert os != null;
try {
inputStream.close();
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
log.info("word转pdf结束");
}
}
/**
* word 转 pdf 返回文件流
*
* @param inputStream word文件流
* @return 生成pdf文件流
*/
public static InputStream word2Pdf(InputStream inputStream) {
log.info("word转pdf[开始]");
//去水印
getLicenseWord();
ByteArrayOutputStream os = null;
try {
os = new ByteArrayOutputStream();
Document doc = new Document(inputStream);
doc.save(os, SaveFormat.PDF);
return new ByteArrayInputStream(os.toByteArray());
} catch (Exception e) {
log.error("word转pdf失败", e);
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
log.info("word转pdf[结束]");
}
return null;
}
/**
* 获取 license 去除水印
* 若不验证则转化出的pdf文档会有水印产生
*/
private static void getLicenseWord() {
ClassPathResource classPathResource = new ClassPathResource(FOP_WORD);
InputStream is = null;
try {
is = classPathResource.getInputStream();
com.aspose.words.License license = new com.aspose.words.License();
license.setLicense(Objects.requireNonNull(is));
} catch (Exception e) {
log.error("license verify failed");
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
3、导出结果
三、附上整个utils类:
package com.zjjw.jxtesttwo.util.util;
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.zjjw.platform.core.util.RandomCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.UUID;
/**
* @author: chenjiaxiang
* @create: 2022/11/23 11:08
**/
@Slf4j
public class ExlWordToPdfUtils {
private static final String PATH = "/Users/chenjx/Downloads/zipceshi/测试exl转pdf.xlsx";
private static final String PATH1 = "/Users/chenjx/Downloads/zipceshi/测试word转pdf.docx";
private static final String FILE_PATH = "/Users/chenjx/Downloads/zipceshi/pdf/exl" + RandomCodeUtil.generateNumberString(8) + "exl.pdf";
private static final String FILE_PATH1 = "/Users/chenjx/Downloads/zipceshi/pdf/word" + RandomCodeUtil.generateNumberString(8) + "word.pdf";
private static final String FOP_EXL = "/file/excel-license.xml";
private static final String FOP_WORD = "/file/word-license.xml";
public static void main(String[] args) {
ExlWordToPdfUtils.excel2pdf(PATH, FILE_PATH);
ExlWordToPdfUtils.word2Pdf(PATH1, FILE_PATH1);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
*/
public static void excel2pdf(String excelFilePath) {
excel2pdf(excelFilePath, null, null);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
* @param convertSheets 需要转换的sheet
*/
public static void excel2pdf(String excelFilePath, int[] convertSheets) {
excel2pdf(excelFilePath, null, convertSheets);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
* @param pdfFilePath pdf文件路径
*/
public static void excel2pdf(String excelFilePath, String pdfFilePath) {
excel2pdf(excelFilePath, pdfFilePath, null);
}
/**
* excel 转 pdf
*
* @param inputStream exl流
* @param pdfFilePath pdf文件路径
*/
public static void excel2pdf(InputStream inputStream, String pdfFilePath) {
excel2pdf(inputStream, pdfFilePath, null);
}
/**
* excel 转 pdf
*
* @param inputStream exl流
* @param convertSheets 需要的sheet
*/
public static InputStream excel2pdf(InputStream inputStream, int[] convertSheets) {
return excel2pdf1(inputStream, convertSheets);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
* @param pdfFilePath pdf文件路径
* @param convertSheets 需要转换的sheet
*/
public static void excel2pdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {
FileOutputStream fileOs = null;
try {
pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
// 验证 License
getLicenseExl();
Workbook wb = new Workbook(excelFilePath);
fileOs = new FileOutputStream(pdfFilePath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
int[] autoDrawSheets={3};
autoDraw(wb,autoDrawSheets);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOs, pdfSaveOptions);
fileOs.flush();
System.out.println("convert success");
} catch (Exception e) {
System.out.println("convert failed");
e.printStackTrace();
} finally {
try {
assert fileOs != null;
fileOs.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
/**
* excel 转 pdf
*
* @param inputStream exl流
* @param pdfFilePath pdf文件路径
* @param convertSheets 需要转换的sheet
*/
public static void excel2pdf(InputStream inputStream, String pdfFilePath, int[] convertSheets) {
FileOutputStream fileOs = null;
try {
// 验证 License
getLicenseExl();
Workbook wb = new Workbook(inputStream);
fileOs = new FileOutputStream(pdfFilePath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setMergeAreas(true);
//缩放到一个页面(如果列太多 太长)
pdfSaveOptions.setOnePagePerSheet(true);
// pdfSaveOptions.setCalculateFormula(true);
// pdfSaveOptions.setCheckFontCompatibility(true);
// pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
// pdfSaveOptions.setEmbedStandardWindowsFonts(true);
// pdfSaveOptions.setFontSubstitutionCharGranularity(true);
// pdfSaveOptions.setRefreshChartCache(true);
// pdfSaveOptions.setClearData(true);
// int[] autoDrawSheets={3};
// autoDraw(wb,autoDrawSheets);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOs, pdfSaveOptions);
fileOs.flush();
} catch (Exception e) {
System.out.println("convert failed");
e.printStackTrace();
} finally {
try {
inputStream.close();
assert fileOs != null;
fileOs.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
log.info("exl转pdf结束");
}
}
/**
* @param wb 设置打印的sheet自动拉伸比例
* @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();
}
}
}
/**
* excel 转 pdf 返回文件流
*
* @param inputStream exl文件流
* @param convertSheets 需要转换的 sheet
* @return 文件流
*/
public static InputStream excel2pdf1(InputStream inputStream, int[] convertSheets) {
ByteArrayOutputStream fileOs = null;
try {
// 验证 License
getLicenseExl();
Workbook wb = new Workbook(inputStream);
fileOs = new ByteArrayOutputStream();
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOs, pdfSaveOptions);
fileOs.flush();
return new ByteArrayInputStream(fileOs.toByteArray());
} catch (Exception e) {
System.out.println("convert failed");
e.printStackTrace();
} finally {
try {
inputStream.close();
if (fileOs != null) {
fileOs.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
log.info("exl转pdf结束");
}
return null;
}
/**
* 获取 生成的 pdf 文件路径,默认与源文件同一目录
*
* @param excelFilePath excel文件
* @return 生成的 pdf 文件
*/
private static String getPdfFilePath(String excelFilePath) {
return excelFilePath.split(".")[0] + ".pdf";
}
/**
* 获取 license 去除水印
* 若不验证则转化出的pdf文档会有水印产生
*/
private static void getLicenseExl() {
InputStream is = null;
try {
ClassPathResource classPathResource = new ClassPathResource(FOP_EXL);
is = classPathResource.getInputStream();
License license = new License();
license.setLicense(is);
} catch (Exception e) {
System.out.println("license verify failed");
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
/**
* 获取 license 去除水印
* 若不验证则转化出的pdf文档会有水印产生
*/
private static void getLicenseWord() {
ClassPathResource classPathResource = new ClassPathResource(FOP_WORD);
InputStream is = null;
try {
is = classPathResource.getInputStream();
com.aspose.words.License license = new com.aspose.words.License();
license.setLicense(Objects.requireNonNull(is));
} catch (Exception e) {
log.error("license verify failed");
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
/**
* 隐藏workbook中不需要的sheet页。
*
* @param sheets 显示页的sheet数组
*/
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);
}
}
}
/**
* word 转 pdf
*
* @param wordFile word 文件路径
* @param pdfFile 生成的 pdf 文件路径
*/
public static void word2Pdf(String wordFile, String pdfFile) {
File file = new File(pdfFile);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
getLicenseWord();
FileOutputStream os = null;
try {
os = new FileOutputStream(pdfFile);
Document doc = new Document(wordFile);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
log.error("word转pdf失败", e);
e.printStackTrace();
} finally {
try {
assert os != null;
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
log.info("word转pdf结束");
}
}
/**
* word 转 pdf 生成文件到服务器
*
* @param inputStream word
* @param pdfFile 生成的 pdf 文件路径
*/
public static void word2Pdf(InputStream inputStream, String pdfFile) {
log.info("word转pdf[开始]");
File file = new File(pdfFile);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
getLicenseWord();
FileOutputStream os = null;
try {
os = new FileOutputStream(pdfFile);
Document doc = new Document(inputStream);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
log.error("word转pdf失败", e);
e.printStackTrace();
} finally {
assert os != null;
try {
inputStream.close();
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
log.info("word转pdf结束");
}
}
/**
* word 转 pdf 返回文件流
*
* @param inputStream word文件流
* @return 生成pdf文件流
*/
public static InputStream word2Pdf(InputStream inputStream) {
log.info("word转pdf[开始]");
//去水印
getLicenseWord();
ByteArrayOutputStream os = null;
try {
os = new ByteArrayOutputStream();
Document doc = new Document(inputStream);
doc.save(os, SaveFormat.PDF);
return new ByteArrayInputStream(os.toByteArray());
} catch (Exception e) {
log.error("word转pdf失败", e);
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
log.info("word转pdf[结束]");
}
return null;
}
}
版权声明:本文标题:java实现Exl、word转pdf 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728479760a1159985.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论