欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-20 17:37:50
引言:        前段时间公司做的教育系统,系统需要实时记录用户学习课程的情况和时间,所以对一些除视频课程之外,对一些...

引言:

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

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

1:添加maven依赖

<!--excel word txt ppt转pdf依赖-->
    <dependency>
      <groupid>aspose</groupid>
      <artifactid>pdf</artifactid>
      <version>11.5.0</version>
    </dependency>
    <dependency>
      <groupid>aspose</groupid>
      <artifactid>words</artifactid>
      <version>16.4.0</version>
    </dependency>
    <dependency>
      <groupid>aspose</groupid>
      <artifactid>cell</artifactid>
      <version>8.9.2</version>
    </dependency>
    <dependency>
      <groupid>aspose</groupid>
      <artifactid>pdf</artifactid>
      <version>11.5.0</version>
    </dependency>

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

<license>
 <data>
  <products>
   <product>aspose.total for java</product>
   <product>aspose.words for java</product>
  </products>
  <editiontype>enterprise</editiontype>
  <subscriptionexpiry>20991231</subscriptionexpiry>
  <licenseexpiry>20991231</licenseexpiry>
  <serialnumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</serialnumber>
 </data>
 <signature>snllkgmudf0r8o1kkilwagdgfs2bvjb/2xp8p5iudvfzxmhppo+d0ran1p9tkdjv4abwagkxxj3jcqtqe/2irfqwnpf8itn8afzlv3tjpyed3ywe7it55gz6eijupc7akeoohtb4w2fpox58wwof3snp6sk6jdfiaugehyj9pju=</signature>
</license>

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:文本文件转码

/* 将txt 转换编码
  * @param file
  * @author zsqing
 */
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

/**
 * 将word txt转换成pdf
 * @param inpath
 * @param outpath
 * @author zsqing
*/
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,docx进行ooxml,rtf,html,opendocument,pdf,epub,xps,swf间转换
       */
      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

/**
 * 将docx转换成pdf
 * @param inpath
 * @param outpath
 * @author zsqing
 */
 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,docx进行ooxml,rtf,html,opendocument,pdf,epub,xps,swf间转换
       */
      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的方法,希望对大家有所帮助