You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.7 KiB
53 lines
1.7 KiB
package com.xujie.sys.common.utils;
|
|
|
|
import com.aspose.cells.PageOrientationType;
|
|
import com.aspose.cells.PageSetup;
|
|
import com.aspose.cells.PaperSizeType;
|
|
import com.aspose.cells.SaveFormat;
|
|
import com.aspose.cells.Workbook;
|
|
import com.aspose.cells.Worksheet;
|
|
import com.aspose.words.Document;
|
|
|
|
import java.io.File;
|
|
|
|
/**
|
|
* Office文件转PDF工具类(使用Aspose)
|
|
*/
|
|
public class OfficeConverter {
|
|
|
|
public static String getFileExtension(File file) {
|
|
String fileName = file.getName();
|
|
int lastDot = fileName.lastIndexOf('.');
|
|
return (lastDot == -1) ? "" : fileName.substring(lastDot + 1);
|
|
}
|
|
|
|
public static void convertExcelToPdf(File file) throws Exception {
|
|
Workbook workbook;
|
|
String filePath = file.getAbsolutePath();
|
|
|
|
if (filePath.endsWith(".xlsx")) {
|
|
workbook = new Workbook(file.getAbsolutePath());
|
|
} else {
|
|
workbook = new Workbook(filePath);
|
|
}
|
|
|
|
Worksheet worksheet = workbook.getWorksheets().get(0);
|
|
PageSetup pageSetup = worksheet.getPageSetup();
|
|
pageSetup.setOrientation(PageOrientationType.PORTRAIT);
|
|
pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
|
|
pageSetup.setZoom(50);
|
|
pageSetup.setLeftMargin(0);
|
|
pageSetup.setRightMargin(0);
|
|
pageSetup.setTopMargin(0.5);
|
|
pageSetup.setBottomMargin(0.5);
|
|
|
|
String pdfPath = filePath.replaceAll("\\.xlsx|\\.xls", ".pdf");
|
|
workbook.save(pdfPath, SaveFormat.PDF);
|
|
}
|
|
|
|
public static void convertWordToPdf(File file) throws Exception {
|
|
Document document = new Document(file.getAbsolutePath());
|
|
String pdfPath = file.getAbsolutePath().replace(".docx", ".pdf").replace(".doc", ".pdf");
|
|
document.save(pdfPath);
|
|
}
|
|
}
|