栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java excel转pdf

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java excel转pdf

需要的依赖和仓库地址

		
            AsposeJavaAPI
            Aspose Java API
            https://repository.aspose.com/repo/
        

		 
		     com.aspose
		     aspose-cells
		     21.8
		 
		
		     org.javassist
		     javassist
		     3.20.0-GA
		 	

工具类

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.aspose.cells.IndividualFontConfigs;
import com.aspose.cells.License;
import com.aspose.cells.LoadOptions;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;


public class ExcelToPdf {
	public static void main(String[] args) {
		excel2pdf("C:\Users\admin\Desktop\1.xlsx","E:\a\excel转pdf\3.pdf");
	}
   
   public static boolean getLicense() {
       boolean result = false;
       try {
    	   String license = "rn" + 
    	   		"  rn" + 
    	   		"    rn" + 
    	   		"      Aspose.Total for Javarn" + 
    	   		"      Aspose.Words for Javarn" + 
    	   		"    rn" + 
    	   		"    Enterprisern" + 
    	   		"    20991231rn" + 
    	   		"    20991231rn" + 
    	   		"    8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7rn" + 
    	   		"  rn" + 
    	   		"  sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=rn" + 
    	   		"";
    	   
           InputStream is = new  ByteArrayInputStream(license.getBytes("UTF-8"));
           License aposeLic = new License();
           aposeLic.setLicense(is);
           result = true;
       } catch (Exception e) {
           e.printStackTrace();
       }
       return result;
   }

   
   public static void excel2pdf(String sourceFilePath, String desFilePathd){
       if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
           return;
       }
       try {
    	   if(isLinux()) {
    		   IndividualFontConfigs configs = new IndividualFontConfigs();
    		   configs.setFontFolder("/usr/share/fonts/chinese", true);
    		   LoadOptions loadOptions  = new LoadOptions();
             	loadOptions.setFontConfigs(configs);
    	   }

    
           
           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("转换PDF完毕!");
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   
   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();
           }
       }
   }


   
   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);
           }
       }
   }
   
   
   public static boolean isLinux() {
       return System.getProperty("os.name").toLowerCase().contains("linux");
   }

   public static boolean isWindows() {
       return System.getProperty("os.name").toLowerCase().contains("windows");
   }

   public String JudgeSystem() {
       if (isLinux()) {
           return "linux";
       } else if (isWindows()) {
           return "windows";
       } else {
           return "other system";
       }
   }
}

controller测试类

	@PostMapping("/excel2pdf")
	public String excel2pdf(MultipartFile file ) throws IllegalStateException, IOException{
		String fileName = file.getOriginalFilename();

		String localFilePath = StrUtil.appendIfMissing("/data/apps/bms/test", "/") + fileName;
		
		
		file.transferTo(new File(localFilePath));
		ExcelToPdf.excel2pdf(localFilePath, "/data/apps/bms/test/" + fileName.split("\.")[0] + ".pdf");

		return "success可喜可贺";
	}

备注:

在linux上转换的时候会有乱码问题,需要在指定位置(如代码所示:/usr/local/fonts/chinese)放windows的字体包C:WindowsFonts

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988292.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号