欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF的方法

 更新時(shí)間:2020年01月15日 10:18:16   作者:zsq_fengchen  
這篇文章主要介紹了Java實(shí)現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

引言:

       前段時(shí)間公司做的教育系統(tǒng),系統(tǒng)需要實(shí)時(shí)記錄用戶學(xué)習(xí)課程的情況和時(shí)間,所以對一些除視頻課程之外,對一些文本文檔型課件同樣如此,初次的方案是講office相關(guān)類型的文件進(jìn)行轉(zhuǎn)換Html文件,然后展示對應(yīng)的html文件,PC端差不多沒問題了,但是個(gè)別文件再轉(zhuǎn)換html之后,樣式出現(xiàn)了錯(cuò)亂,即時(shí)做了編碼轉(zhuǎn)換處理,但是還是有個(gè)別亂碼,最后改變方案,最后統(tǒng)一將文件轉(zhuǎn)為pdf,然后通過流的方式在前端展示,其中包括Word Excel PPT TXT PDF等文件,代碼如下:

   備注:本來是可以直接展示pdf的,但是Andior上pdf展示不了,最后統(tǒng)一就用IO流的方式進(jìn)行讀取展示了.

1:添加maven依賴

<!--excel word txt ppt轉(zhuǎn)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:文本文件轉(zhuǎn)碼

/* 將txt 轉(zhuǎn)換編碼
  * @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("轉(zhuǎn)碼結(jié)束");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return file;
  }

3.3:word和txt轉(zhuǎn)換pdf

/**
 * 將word txt轉(zhuǎn)換成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();
      // 新建一個(gè)空白文檔
      file = new File(outPath);
      file = saveAsUTF8(file);
      os = new FileOutputStream(file);
      // InPath是將要被轉(zhuǎn)化的文檔
      com.aspose.words.Document doc = new com.aspose.words.Document(inPath);
      /*
       * 全面支持DOC,DOCX進(jìn)行OOXML,RTF,HTML,OpenDocument,PDF,EPUB,XPS,SWF間轉(zhuǎn)換
       */
      doc.save(os, SaveFormat.PDF);
      flag = true;
      //long now = System.currentTimeMillis();
      //System.out.println("共耗時(shí):" + ((now - old) / 1000.0) + "秒"); // 轉(zhuǎn)化用時(shí)
      
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (os != null)
        {
          os.close();
        }
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
      if (!flag)
      {
        file.deleteOnExit();
      }
    }
  }

3.4:Excel轉(zhuǎn)換pdf

/**
 * 將docx轉(zhuǎn)換成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();
      // 新建一個(gè)空白文檔
      file = new File(outPath);
      file = saveAsUTF8(file);
      os = new FileOutputStream(file);
      // InPath是將要被轉(zhuǎn)化的文檔
      com.aspose.words.Document doc = new com.aspose.words.Document(inPath);
      /*
       * 全面支持DOC,DOCX進(jìn)行OOXML,RTF,HTML,OpenDocument,PDF,EPUB,XPS,SWF間轉(zhuǎn)換
       */
      doc.save(os, SaveFormat.PDF);
      flag = true;
      //long now = System.currentTimeMillis();
      //System.out.println("共耗時(shí):" + ((now - old) / 1000.0) + "秒"); // 轉(zhuǎn)化用時(shí)
      
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (os != null)
        {
          os.close();
        }
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
      if (!flag)
      {
        file.deleteOnExit();
      }
    }
  }

總結(jié)

以上所述是小編給大家介紹的Java實(shí)現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • Java探索之Feign入門使用詳解

    Java探索之Feign入門使用詳解

    這篇文章主要介紹了Java探索之Feign入門使用詳解,關(guān)于feign,我也是第一次遇到,于是在網(wǎng)上搜集了相關(guān)文章,這篇比較詳細(xì),介紹了其簡介,選擇feign的原因以及其他相關(guān)內(nèi)容,需要的朋友可以參考下。
    2017-10-10
  • JVM回收跨代垃圾的方式詳解

    JVM回收跨代垃圾的方式詳解

    在Java堆內(nèi)存中,年輕代和老年代之間存在的對象相互引用,假設(shè)現(xiàn)在要進(jìn)行一次新生代的YGC,但新生代中的對象可能被老年代所引用的,為了找到新生代中的存活對象,不得不遍歷整個(gè)老年代,這樣明顯效率很低下,那么如何快速識(shí)別并回收這種引用對象呢
    2024-02-02
  • java繪制國際象棋與中國象棋棋盤

    java繪制國際象棋與中國象棋棋盤

    這篇文章主要為大家詳細(xì)介紹了java繪制國際象棋與中國象棋棋盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • jmeter壓力測試工具簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    jmeter壓力測試工具簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了jmeter壓力測試工具相關(guān)介紹資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Spring Cloud Alibaba之Sentinel實(shí)現(xiàn)熔斷限流功能

    Spring Cloud Alibaba之Sentinel實(shí)現(xiàn)熔斷限流功能

    這篇文章主要介紹了Spring Cloud Alibaba之Sentinel,這里使用阿里的sentinel來實(shí)現(xiàn)熔斷限流功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • nacos注冊中心單節(jié)點(diǎn)ap架構(gòu)源碼解析(最新推薦)

    nacos注冊中心單節(jié)點(diǎn)ap架構(gòu)源碼解析(最新推薦)

    這篇文章主要介紹了nacos注冊中心單節(jié)點(diǎn)ap架構(gòu)源碼解析,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01
  • java編程實(shí)現(xiàn)國際象棋棋盤

    java編程實(shí)現(xiàn)國際象棋棋盤

    這篇文章主要為大家詳細(xì)介紹了java編程實(shí)現(xiàn)國際象棋棋盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Springboot項(xiàng)目升級2.2.x升至2.7.x的示例代碼

    Springboot項(xiàng)目升級2.2.x升至2.7.x的示例代碼

    本文主要介紹了Springboot項(xiàng)目升級2.2.x升至2.7.x的示例代碼,會(huì)有很多的坑,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • Spring?Cloud?Stream消息驅(qū)動(dòng)組件使用方法介紹

    Spring?Cloud?Stream消息驅(qū)動(dòng)組件使用方法介紹

    Spring?Cloud?Stream?消息驅(qū)動(dòng)組件幫助我們更快速,更方便,更友好的去構(gòu)建消息驅(qū)動(dòng)微服務(wù)的。當(dāng)時(shí)定時(shí)任務(wù)和消息驅(qū)動(dòng)的?個(gè)對比。消息驅(qū)動(dòng):基于消息機(jī)制做一些事情
    2022-09-09
  • Java中的cglib原理解析

    Java中的cglib原理解析

    這篇文章主要介紹了Java中的cglib原理解析,由于代理類繼承了被代理類,所以調(diào)用sayHello()方法時(shí)會(huì)直接調(diào)用代理類的sayHello()方法,而在代理類的方法中,調(diào)用了Callback的邏輯,需要的朋友可以參考下
    2023-10-10

最新評論