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

Java实现Word/Excel/TXT转PDF的方法

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

Java实现Word/Excel/TXT转PDF的方法

引言:

       前段时间公司做的教育系统,系统需要实时记录用户学习课程的情况和时间,所以对一些除视频课程之外,对一些文本文档型课件同样如此,初次的方案是讲office相关类型的文件进行转换Html文件,然后展示对应的html文件,PC端差不多没问题了,但是个别文件再转换html之后,样式出现了错乱,即时做了编码转换处理,但是还是有个别乱码,最后改变方案,最后统一将文件转为pdf,然后通过流的方式在前端展示,其中包括Word Excel PPT TXT PDF等文件,代码如下:

   备注:本来是可以直接展示pdf的,但是Andior上pdf展示不了,最后统一就用IO流的方式进行读取展示了.

1:添加maven依赖


    
      aspose
      pdf
      11.5.0
    
    
      aspose
      words
      16.4.0
    
    
      aspose
      cell
      8.9.2
    
    
      aspose
      pdf
      11.5.0
    

2:添加license-excel.xml文件(Resource文件夹下)


 
  
   Aspose.Total for Java
   Aspose.Words for Java
  
  Enterprise
  20991231
  20991231
  8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7
 
 sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=

3:代码如下:

  3.1获取License文件

public static boolean getLicense(){
    boolean result = false;
    InputStream is = null;
    try{
      
      is =UploadFiles.class.getClassLoader().getResourceAsStream("license-excel.xml");
      License aposeLic = new License();
      aposeLic.setLicense(is);
      result = true;
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      try {
 is.close();
      } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
      }
    }
    return result;
  }

 3.2:文本文件转码


public File saveAsUTF8(File file){
    String code = "gbk";
    byte[] head = new byte[3];
    try {
      InputStream inputStream = new FileInputStream(file);
      inputStream.read(head);
      if (head[0] == -1 && head[1] == -2) {
 code = "UTF-16";
      } else if (head[0] == -2 && head[1] == -1) {
 code = "Unicode";
      } else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
 code = "UTF-8";
      }
      inputStream.close();
      System.out.println(code);
      if (code.equals("UTF-8")) {
 return file;
      }
      String str = FileUtils.readFileToString(file, code);
      FileUtils.writeStringToFile(file, str, "UTF-8");
      System.out.println("转码结束");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return file;
  }

3.3:word和txt转换pdf


public void wordAndTextToPdf(String inPath, String outPath ,String localIP,HttpServletRequest request)
  {
    String fileToPdfUrl="";
    boolean flag = false;
    File file = null;
    FileOutputStream os = null;
    try
    {
      //long old = System.currentTimeMillis();
      // 新建一个空白文档
      file = new File(outPath);
      file = saveAsUTF8(file);
      os = new FileOutputStream(file);
      // InPath是将要被转化的文档
      com.aspose.words.document doc = new com.aspose.words.document(inPath);
      
      doc.save(os, SaveFormat.PDF);
      flag = true;
      //long now = System.currentTimeMillis();
      //System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
      
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
 if (os != null)
 {
   os.close();
 }
      }
      catch (Exception e)
      {
 e.printStackTrace();
      }
      if (!flag)
      {
 file.deleteonExit();
      }
    }
  }

3.4:Excel转换pdf


 public void wordToPdf(String inPath, String outPath ,String localIP,HttpServletRequest request)
  {
    String fileToPdfUrl="";
    boolean flag = false;
    File file = null;
    FileOutputStream os = null;
    try
    {
      //long old = System.currentTimeMillis();
      // 新建一个空白文档
      file = new File(outPath);
      file = saveAsUTF8(file);
      os = new FileOutputStream(file);
      // InPath是将要被转化的文档
      com.aspose.words.document doc = new com.aspose.words.document(inPath);
      
      doc.save(os, SaveFormat.PDF);
      flag = true;
      //long now = System.currentTimeMillis();
      //System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
      
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
 if (os != null)
 {
   os.close();
 }
      }
      catch (Exception e)
      {
 e.printStackTrace();
      }
      if (!flag)
      {
 file.deleteonExit();
      }
    }
  }

总结

以上所述是小编给大家介绍的Java实现Word/Excel/TXT转PDF的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

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

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

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