@Slf4j
public class OfficeUtil {
/**
* Excel文件转PDF文件
*
* @param inputStream
* @param pdfPath
*/
@SuppressWarnings("unchecked")
public static File excel2pdf(InputStream inputStream, String pdfPath) {
FileOutputStream fileOS = null;
if (!ExcelUtil.getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return null;
}
try {
long old = System.currentTimeMillis();
Workbook wb = new Workbook(inputStream);
File file = new File(pdfPath);
file.setWritable(true, false);
fileOS = new FileOutputStream(file);
Style style = new Style();
style.getFont().setName("宋体");
//设置单元格的边框颜色
style.setBorder(4, 1, Color.fromArgb(0, 0, 0));
style.setBorder(8, 1, Color.fromArgb(0, 0, 0));
style.setBorder(2, 1, Color.fromArgb(0, 0, 0));
style.setBorder(1, 1, Color.fromArgb(0, 0, 0));
style.setShrinkToFit(true);
wb.setDefaultStyle(style);
StyleFlag sf = new StyleFlag();
sf.setBorders(true);
sf.setShrinkToFit(true);
sf.setWrapText(true);
wb.getWorksheets().forEach(new Consumer() {
@Override
public void accept(Worksheet worksheet) {
try {
worksheet.autoFitRows();
worksheet.autoFitColumns();
worksheet.getCells().applyStyle(style, sf);
} catch (Exception e) {
e.printStackTrace();
}
}
});
PdfSaveOptions pdfSaveOption = new PdfSaveOptions(com.aspose.cells.SaveFormat.PDF);
pdfSaveOption.setOnePagePerSheet(true);
pdfSaveOption.setAllColumnsInOnePagePerSheet(true);
wb.save(fileOS, pdfSaveOption);
long now = System.currentTimeMillis();
log.info("Excel文件转PDF文件共耗时{}秒", ((now - old) / 1000.0)); // 转化用时
return file;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (fileOS != null) {
fileOS.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* CSV转PDF
*/
@SuppressWarnings("unchecked")
public static File csv2pdf(InputStream inputStream, String pdfPath) {
if (!ExcelUtil.getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return null;
}
ByteArrayOutputStream baos = null;
ByteArrayInputStream bais = null;
FileOutputStream fileOS = null;
try {
long old = System.currentTimeMillis();
TxtLoadOptions lo = new TxtLoadOptions();
baos = new ByteArrayOutputStream();
IoUtil.copy(inputStream, baos);
byte[] b = baos.toByteArray();
if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
lo.setEncoding(Encoding.getUTF8());
} else {
lo.setEncoding(Encoding.getEncoding("GB2312"));
}
bais = new ByteArrayInputStream(baos.toByteArray());
Workbook wb = new Workbook(bais, lo);
File file = new File(pdfPath);
file.setWritable(true, false);
fileOS = new FileOutputStream(file);
Style style = wb.getDefaultStyle();
style.setShrinkToFit(true);
style.setTextWrapped(true);
style.getFont().setName("宋体");
//设置单元格的边框颜色
style.setBorder(4, 1, Color.fromArgb(0, 0, 0));
style.setBorder(8, 1, Color.fromArgb(0, 0, 0));
style.setBorder(2, 1, Color.fromArgb(0, 0, 0));
style.setBorder(1, 1, Color.fromArgb(0, 0, 0));
wb.setDefaultStyle(style);
wb.getWorksheets().forEach(new Consumer() {
@Override
public void accept(Worksheet worksheet) {
try {
worksheet.autoFitRows();
worksheet.autoFitColumns();
} catch (Exception e) {
e.printStackTrace();
}
}
});
PdfSaveOptions pdfSaveOption = new PdfSaveOptions(com.aspose.cells.SaveFormat.PDF);
pdfSaveOption.setAllColumnsInOnePagePerSheet(true);
wb.save(fileOS, pdfSaveOption);
fileOS.close();
long now = System.currentTimeMillis();
log.info("Csv文件转PDF文件共耗时{}秒", ((now - old) / 1000.0)); // 转化用时
return file;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (fileOS != null) {
fileOS.close();
}
if (baos != null) {
baos.close();
}
if (bais != null) {
bais.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* Word文件转PDF文件
*
* @param inputStream
* @param pdfPath
*/
public static File doc2pdf(InputStream inputStream, String pdfPath) {
if (!WordUtil.getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return null;
}
try {
long old = System.currentTimeMillis();
File file = new File(pdfPath);
file.setWritable(true, false);
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(inputStream);
String path = pdfPath.substring(0, pdfPath.lastIndexOf(File.separator));
String fontPath = path + File.separator + "font" + File.separator;
File fontDic = new File(fontPath);
FontSettings fontSettings = new FontSettings();
if (fontDic.exists() && fontDic.listFiles().length > 0) {
FontSettings.getDefaultInstance().setFontsFolder(fontPath, true);
fontSettings.setFontsFolder(fontPath, true);
} else {
FontSettings.getDefaultInstance().setDefaultFontName("宋体");
fontSettings.setDefaultFontName("宋体");
}
doc.setFontSettings(fontSettings);
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
os.close();
log.info("Word文件转PDF文件共耗时{}秒", ((now - old) / 1000.0)); // 转化用时
return file;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* @method ppt2pdf PPT转PDF
*/
public static File ppt2pdf(InputStream inputStream, String pdfPath) {
if (!WordUtil.getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return null;
}
try {
long old = System.currentTimeMillis();
File file = new File(pdfPath);
file.setWritable(true, false);
FileOutputStream os = new FileOutputStream(file);
Presentation pres = new Presentation(inputStream);
PdfOptions pdfOpts = new PdfOptions();
pdfOpts.setDefaultRegularFont("宋体");
pres.save(os, com.aspose.slides.SaveFormat.Pdf, pdfOpts);
long now = System.currentTimeMillis();
os.close();
log.info("ppt文件转PDF文件共耗时{}秒", ((now - old) / 1000.0)); // 转化用时
return file;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void main(String[] args) {
File file = new File("d:/利润表测试2018.03.xls");
try {
OfficeUtil.excel2pdf(new FileInputStream(file), "d:/5.pdf");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public class WordUtil {
public static boolean getLicense() {
boolean result = false;
try {
String str = "";//证书字符串,有破解版。支持购买
InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
需要服务器上安装有对应的字体,或者字体文件。不然会出现中文乱码的情况。