package com.aspose.cells;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class TestForExcel2PDF {
public static void main(String[] args) throws Exception {
String sourceFilePath="e:/1.xlsx";
String desFilePath="e:/1.pdf";
// addWaterMark4Excel(sourceFilePath);
excel2pdf(sourceFilePath, desFilePath);
}
public static void addWaterMark4Excel(String path) throws Exception {
FileInputStream is = new FileInputStream(path);
com.aspose.cells.Workbook workbook = new com.aspose.cells.Workbook(is);
//Get the first default sheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Add Watermark
Shape wordart = sheet.getShapes().addTextEffect(
MsoPresetTextEffect.TEXT_EFFECT_11,"内部资料", "Arial Black", 50, false, true , 18, 8, 1, 1, 130, 800);
//Get the fill format of the word art
MsoFillFormat wordArtFormat = wordart.getFillFormat();
//Set the color
wordArtFormat.setForeColor(com.aspose.cells.Color.getGray());
//Set the transparency
wordArtFormat.setTransparency(0.5);
//Make the line invisible
MsoLineFormat lineFormat = wordart.getLineFormat();
lineFormat.setVisible(false);
//Save the file
workbook.save(path);
}
/**
* 获取license 去除水印
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = TestForExcel2PDF.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 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[] autoDrawSheets={3};
//当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
// autoDraw(wb,autoDrawSheets);
int[] showSheets={0};
//隐藏workbook中不需要的sheet页。
printSheetPage(wb,showSheets);
wb.save(fileOS, pdfSaveOptions);
fileOS.flush();
fileOS.close();
System.out.println("完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置打印的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);
}
}
}
}

love_shasha
- 粉丝: 0
- 资源: 14
最新资源
- Proteus 仿真MSP430实例之10 ADC12.zip
- Proteus 仿真MSP430实例之09 SPI.zip
- 基于两电平逆变器的SVPWM调制技术及空间矢量优化在Simulink仿真模型与Visio原理图中的实践与应用研究 ,基于两电平逆变器的SVPWM调制算法研究 - 文件包含Simulink仿真模型与Vi
- 洛谷P2089代码题解
- 基于事件触发机制的多智能体系统跟踪共识与共识策略研究,基于事件触发机制的多智能体系统跟踪共识研究:非理想一般线性系统的共识策略与算法设计,基于观测器的非理想一般线性多智能体系统的事件触发跟踪共识 关键
- MATLAB仿真:基于人工势场法的无人车路径规划算法Demo,含目标定位与障碍物避障,详细代码注释助力初学者快速掌握 ,MATLAB中基于人工势场法的无人车路径规划算法Demo:目标定位与障碍物规避的
- 三相并网仿真系统中的准PR控制策略及电容电流反馈技术应用研究,三相并网仿真技术:准PR控制与电容电流反馈策略的探讨与实践,三相并网仿真,准PR控制+电容电流反馈 ,三相并网仿真; 准PR控制; 电容电
- 基于改进麻雀算法的径向基神经网络优化与回归预测建模:Excel数据驱动的误差评估与可视化,基于改进麻雀算法的径向基神经网络优化与回归预测建模:Excel数据驱动的误差评估与可视化分析,改进麻雀算法优化
- 正齿轮减速机 - 变速箱 正齿轮减速器变速箱 3D 在 Creo 11 中设计 包括 STEP 组件
- 基于虚拟惯性控制与下垂控制的双馈风力发电机多机多节点一次调频模型:高风电渗透率下的储能与光伏系统整合仿真研究,基于虚拟惯性控制与下垂控制的双馈风力发电机多机多节点一次调频模型:仿真与接入混合能源系统研
- 基于ECMS的并联混合动力汽车能量管理策略研究:实现最小等效燃油消耗与多工况仿真分析,基于ECMS能量管理策略的并联混合动力汽车仿真研究:实现等效燃油消耗最小化与性能优化,ECMS,等效燃油消耗最小策
- 基于Matlab Simulink的双馈风机并网仿真模型:高效稳定、自调节与测频功能实现,基于Matlab Simulink的双馈风机并网模型:可测频自调波形稳定,仿真速度快捷高效,风机并网matla
- 标题:基于GWO-LSSVM灰狼算法优化最小二乘支持向量机回归预测的易用型软件工具-适应度函数优化参数与结果存储升级版 注:标题字数控制在40字以上,90字以内,根据您提供的文字提炼得出,力求简洁
- 分布式驱动电动汽车路面附着系数精确估计技术研究:基于无迹卡尔曼滤波(UKF)与容积卡尔曼滤波(CKF)的S-function估计算法及其在不同工况下的应用评估 ,基于分布式驱动电动汽车的动态路面附着系
- 基于蛇优化器(Snake Optimizer, SO)的无人机路径规划技术:探索算法在科学和工程应用中的高效性能与改进空间,**基于自然启发的蛇优化器在无人机路径规划的高效应用:提升算法质量与现实实践
- MDK5.41(含科学使用文件)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


