本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
Java实现Word/Excel/TXT转PDF
luenmicro · 495浏览 · 发布于2020-01-15 +关注

引言:

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

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

1:添加maven依赖

按 Ctrl+C 复制代码

    asposepdf11.5.0asposewords16.4.0asposecell8.9.2asposepdf11.5.0

按 Ctrl+C 复制代码
2:添加license-excel.xml文件(Resource文件夹下)

Aspose.Total for JavaAspose.Words for JavaEnterprise20991231209912318bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/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:文本文件转码

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

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

相关推荐

PHP实现部分字符隐藏

沙雕mars · 1325浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 908浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 970浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 963浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 1086浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
本人有多年的互联网工作经验,专注技术研发,运维工作等!
分类专栏
小鸟云服务器
扫码进入手机网页