Excel转PDF工具类
Excel转PDF工具类
代码语言:java复制import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOpti;
import com.aspose.cells.Workbook;
import java.io.FileOutputStream;
import java.io.InputStream;
publi
Excel转PDF工具类
代码语言:java复制import com.License;
import com.PdfSaveOpti;
import com.Workbook;
import java.io.FileOutputStream;
import java.io.InputStream;
public class ExcelUtil {
/**
* 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 excelFilePath excel文件路径
* @param pdfFilePath pdf文件路径
* @param convertSheets 需要转换的sheet
*/
public static void excel2pdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {
try {
pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
// 验证 License
getLicense();
Workbook wb = new Workbook(excelFilePath);
FileOutputStream fileOS = new FileOutputStream(pdfFilePath);
PdfSaveOpti pdfSaveOpti = new PdfSaveOpti();
pdfSaveOpti.setOnePagePerSheet(true);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOS, pdfSaveOpti);
fileOS.flush();
();
println("convert success");
} catch (Exception e) {
println("convert failed");
e.printStackTrace();
}
}
/**
* 获取生成的 pdf 文件路径,默认与源文件同一目录
*
* @param excelFilePath excel文件路径
* @return 生成的 pdf 文件路径
*/
public static String getPdfFilePath(String excelFilePath) {
if (excelFilePath == null || excelFilePath.isEmpty()) {
throw new IllegalArgumentException("Excel file path cannot be null or empty");
}
int dotIndex = excelFilePath.lastIndexOf('.');
if (dotIndex > 0) {
return excelFilePath.substring(0, dotIndex) + ".pdf";
}
// 如果文件路径中没有扩展名,直接追加 .pdf
return excelFilePath + ".pdf";
}
/**
* 获取 license 去除水印
* 若不验证则转化出的pdf文档会有水印产生
*/
public static void getLicense() {
String licenseFilePath = "excel-license.xml";
try {
InputStream is = getClassLoader().getResourceAsStream(licenseFilePath);
License license = new License();
license.setLicense(is);
} catch (Exception e) {
println("license verify failed");
e.printStackTrace();
}
}
/**
* 隐藏workbook中不需要的sheet页。
*
* @param sheets 显示页的sheet数组
*/
public 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);
}
}
}
}
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2025-07-21 09:53:44
推荐阅读
留言与评论(共有 16 条评论) |
本站网友 蓬莱租房网 | 1分钟前 发表 |
int[] convertSheets) { try { pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) | |
本站网友 峨嵋酒家 | 28分钟前 发表 |
pdfFilePath; // 验证 License getLicense(); Workbook wb = new Workbook(excelFilePath); FileOutputStream fileOS = new FileOutputStream(pdfFilePath); PdfSaveOpti pdfSaveOpti = new PdfSaveOpti(); pdfSaveOpti.setOnePagePerSheet(true); if (null != convertSheets) { printSheetPage(wb | |
本站网友 佛影峡 | 17分钟前 发表 |
int[] convertSheets) { try { pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) | |
本站网友 白果功效 | 27分钟前 发表 |
默认与源文件同一目录 * * @param excelFilePath excel文件路径 * @return 生成的 pdf 文件路径 */ public static String getPdfFilePath(String excelFilePath) { if (excelFilePath == null || excelFilePath.isEmpty()) { throw new IllegalArgumentException("Excel file path cannot be null or empty"); } int dotIndex = excelFilePath.lastIndexOf('.'); if (dotIndex > 0) { return excelFilePath.substring(0 | |
本站网友 贸易壁垒 | 30分钟前 发表 |
默认与源文件同一目录 * * @param excelFilePath excel文件路径 * @return 生成的 pdf 文件路径 */ public static String getPdfFilePath(String excelFilePath) { if (excelFilePath == null || excelFilePath.isEmpty()) { throw new IllegalArgumentException("Excel file path cannot be null or empty"); } int dotIndex = excelFilePath.lastIndexOf('.'); if (dotIndex > 0) { return excelFilePath.substring(0 | |
本站网友 望京餐厅 | 28分钟前 发表 |
int[] convertSheets) { excel2pdf(excelFilePath | |
本站网友 4488b | 18分钟前 发表 |
int[] convertSheets) { excel2pdf(excelFilePath | |
本站网友 服务器cpu | 5分钟前 发表 |
null); } /** * excel 转 pdf * * @param excelFilePath excel文件路径 * @param convertSheets 需要转换的sheet */ public static void excel2pdf(String excelFilePath | |
本站网友 officedepot | 17分钟前 发表 |
convertSheets); } /** * excel 转 pdf * * @param excelFilePath excel文件路径 * @param pdfFilePath pdf文件路径 */ public static void excel2pdf(String excelFilePath | |
本站网友 bsuv | 16分钟前 发表 |
String pdfFilePath | |
本站网友 gt86 | 4分钟前 发表 |
null); } /** * excel 转 pdf * * @param excelFilePath excel文件路径 * @param convertSheets 需要转换的sheet */ public static void excel2pdf(String excelFilePath | |
本站网友 美联储利率 | 13分钟前 发表 |
String pdfFilePath | |
本站网友 上海工厂 | 18分钟前 发表 |
Excel转PDF工具类 代码语言:java复制import com.License; import com.PdfSaveOpti; import com.Workbook; import java.io.FileOutputStream; import java.io.InputStream; public class ExcelUtil { /** * excel 转 pdf * * @param excelFilePath excel文件路径 */ public static void excel2pdf(String excelFilePath) { excel2pdf(excelFilePath | |
本站网友 布希鞋 | 23分钟前 发表 |
dotIndex) + ".pdf"; } // 如果文件路径中没有扩展名 | |
本站网友 高考泄题 | 22分钟前 发表 |
int[] convertSheets) { excel2pdf(excelFilePath |