本文需要aspose.cells.License
完整代码放在最后
第一步,导入Maven依赖和包
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>9.0.0</version>
</dependency>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
第二步,导入验证许可函数
public static boolean cellsgetLicense() {
boolean result = false;
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
license = loader.getResourceAsStream("cellslicense.xml"); // 导入许可
License aposeLic = new License();
aposeLic.setLicense(license);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
第三步,设置好输入输出的路径变量
private static InputStream license;
private static InputStream fileInput; // 输入路径
private static File outputFile; // 输出路径
第四步,转换函数
public static void main(String[] args) {
// 获取License
if (!cellsgetLicense()) {
return;
}
try {
fileInput = new FileInputStream("read_from_this.xlsx"); // 待处理的文件路径
outputFile = new File("E:\\导出文件\\test.pdf"); // 输出路径
Workbook wb = new Workbook(fileInput);
FileOutputStream fileOS = new FileOutputStream(outputFile);
wb.save(fileOS, SaveFormat.PDF);
System.out.println("文件保存在:" + outputFile.getPath());
} catch (Exception e) {
e.printStackTrace();
}
}
完整代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
public class test {
private static InputStream license;
private static InputStream fileInput; // 输入路径
private static File outputFile; // 输出路径
public static boolean cellsgetLicense() {
boolean result = false;
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
license = loader.getResourceAsStream("cellslicense.xml"); // 导入许可
License aposeLic = new License();
aposeLic.setLicense(license);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
// 获取License
if (!cellsgetLicense()) {
return;
}
try {
fileInput = new FileInputStream("read_from_this.xlsx"); // 待处理的文件路径
outputFile = new File("E:\\导出文件\\test.pdf"); // 输出路径
Workbook wb = new Workbook(fileInput);
FileOutputStream fileOS = new FileOutputStream(outputFile);
wb.save(fileOS, SaveFormat.PDF);
System.out.println("文件保存在:" + outputFile.getPath());
} catch (Exception e) {
e.printStackTrace();
}
}
}