当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现Word/Excel/TXT转PDF

Java实现Word/Excel/TXT转PDF

2020年01月15日  | 移动技术网IT编程  | 我要评论

引言:

       前段时间公司做的教育系统,系统需要实时记录用户学习课程的情况和时间,所以对一些除视频课程之外,对一些文本文档型课件同样如此,初次的方案是讲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文件

 1 public static boolean getlicense(){
 2         boolean result = false;
 3         inputstream is =  null;
 4         try{
 5             
 6             is =uploadfiles.class.getclassloader().getresourceasstream("license-excel.xml");
 7             license aposelic = new license();
 8             aposelic.setlicense(is);
 9             result = true;
10         }catch(exception e){
11             e.printstacktrace();
12         }finally{
13             try {
14                 is.close();
15             } catch (ioexception e) {
16                 // todo auto-generated catch block
17                 e.printstacktrace();
18             }
19         }
20         return result;
21     }

 3.2:文本文件转码

 1   /* 将txt 转换编码
 2    * @param file
 3    * @author zsqing
 4   */
 5 public file saveasutf8(file file){
 6         string code = "gbk";
 7         byte[] head = new byte[3];
 8         try {
 9             inputstream inputstream = new fileinputstream(file);
10             inputstream.read(head);
11             if (head[0] == -1 && head[1] == -2) {
12                 code = "utf-16";
13             } else if (head[0] == -2 && head[1] == -1) {
14                 code = "unicode";
15             } else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
16                 code = "utf-8";
17             }
18             inputstream.close();
19 
20             system.out.println(code);
21             if (code.equals("utf-8")) {
22                 return file;
23             }
24             string str = fileutils.readfiletostring(file, code);
25             fileutils.writestringtofile(file, str, "utf-8");
26             system.out.println("转码结束");
27         } catch (filenotfoundexception e) {
28             e.printstacktrace();
29         } catch (ioexception e) {
30             e.printstacktrace();
31         }
32 
33         return file;
34     }

3.3:word和txt转换pdf

 1 /**
 2  * 将word txt转换成pdf
 3  * @param inpath
 4  * @param outpath
 5  * @author zsqing
 6 */
 7 public  void wordandtexttopdf(string inpath, string outpath ,string localip,httpservletrequest request)
 8     {
 9         string filetopdfurl="";
10         boolean flag = false;
11         file file = null;
12         fileoutputstream os = null;
13         try
14         {
15             //long old = system.currenttimemillis();
16             // 新建一个空白文档
17             file = new file(outpath);
18             file = saveasutf8(file);
19             os = new fileoutputstream(file);
20             // inpath是将要被转化的文档
21             com.aspose.words.document doc = new com.aspose.words.document(inpath);
22             /*
23              * 全面支持doc,docx进行ooxml,rtf,html,opendocument,pdf,epub,xps,swf间转换
24              */
25             doc.save(os, saveformat.pdf);
26             flag = true;
27             //long now = system.currenttimemillis();
28             //system.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
29             
30         }
31         catch (exception e)
32         {
33             e.printstacktrace();
34         }
35         finally
36         {
37             try
38             {
39                 if (os != null)
40                 {
41                     os.close();
42                 }
43             }
44             catch (exception e)
45             {
46                 e.printstacktrace();
47             }
48             if (!flag)
49             {
50                 file.deleteonexit();
51             }
52         }
53     }

3.4:excel转换pdf

 1 /**
 2  * 将docx转换成pdf
 3  * @param inpath
 4  * @param outpath
 5  * @author zsqing
 6  */
 7  public  void wordtopdf(string inpath, string outpath ,string localip,httpservletrequest request)
 8     {
 9         string filetopdfurl="";
10         boolean flag = false;
11         file file = null;
12         fileoutputstream os = null;
13         try
14         {
15             //long old = system.currenttimemillis();
16             // 新建一个空白文档
17             file = new file(outpath);
18             file = saveasutf8(file);
19             os = new fileoutputstream(file);
20             // inpath是将要被转化的文档
21             com.aspose.words.document doc = new com.aspose.words.document(inpath);
22             /*
23              * 全面支持doc,docx进行ooxml,rtf,html,opendocument,pdf,epub,xps,swf间转换
24              */
25             doc.save(os, saveformat.pdf);
26             flag = true;
27             //long now = system.currenttimemillis();
28             //system.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
29             
30         }
31         catch (exception e)
32         {
33             e.printstacktrace();
34         }
35         finally
36         {
37             try
38             {
39                 if (os != null)
40                 {
41                     os.close();
42                 }
43             }
44             catch (exception e)
45             {
46                 e.printstacktrace();
47             }
48             if (!flag)
49             {
50                 file.deleteonexit();
51             }
52         }
53     }

最近工作有些忙写的就少了,2020年第一篇与大家分享,一起学习,共同成长!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网