利用java實現excel轉pdf文件


在有些需求當中我們需要抓取字段並且填充到excel表格里面,最后將excel表格轉換成pdf格式進行輸出,我第一次接觸這個需求時,碰到幾個比較棘手的問題,現在一一列出並且提供解決方案。

1:excel轉pdf出現亂碼:

    第一次excel轉pdf是成功的,第二次開始后面皆是亂碼,是因為我的pdf轉excel方法出現的問題,解決辦法是采用java自身底層的方法(詳見下方代碼)。

 public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("license.xml"); //  license.xml應放在..\WebRoot\WEB-INF\classes路徑下
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {               
            e.printStackTrace();
        }
        return result;
    }
    
    
    public static void excelTransferPdf(String excelPath,String pdfPath) {
        if (!getLicense()) {
            System.out.println("license faile");
            return;
        }
        
        try {     
            Workbook wb = new Workbook(excelPath);
            FileOutputStream fileOS = new FileOutputStream(new File(pdfPath));
            wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
            fileOS.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2:excel轉pdf出現折行。

  excel轉pdf出現折行的情況非常常見,因為在程序運行過程中很多字段是抓取的,你無法判斷你的excel轉成pdf會有幾頁,所以這個時候你就不要隨意設置excel的預覽格式,將excel的單元格式設置自動換行。

3:抓取字段顯示結果不完整:。

  當你未設置單元格大小而又沒有設置單元格自動換行,比如你的A18單元格里面的字段超過了單元格的長度你還沒有設置單元格大小而又沒有設置單元格自動換行,就將抓取的字段填充在B18單元格里面,那么打印出來的pdf文件A18單元格超出單元格外的內容是不予顯示的,此時你要么將抓取字段填充在C18單元格內要么將更改A18單元格格式

4:excel轉PDF字段內容無故中間部分換行:

  這是我碰到的最坑的一個地方,這個時候你只需要在excel單元格里面設置自動換行即可,無需代碼強行自動換行(強行換行有可能只出現多行數據只顯示一行)。同時你需要如下代碼:

/**
     * 得到一個字符串的長度,顯示的長度,一個漢字或日韓文長度為1,英文字符長度為0.5
     *
     * @param String
     *            s 需要得到長度的字符串
     * @return int 得到的字符串長度
     */
    public static double getLength(String s) {
        double valueLength = 0;
        if (s == null) {
            return 0;
        }
        String chinese = "[\u4e00-\u9fa5]";
        // 獲取字段值的長度,如果含中文字符,則每個中文字符長度為2,否則為1
        for (int i = 0; i < s.length(); i++) {
            // 獲取一個字符
            String temp = s.substring(i, i + 1);
            // 判斷是否為中文字符
            if (temp.matches(chinese)) {
                // 中文字符長度為2
                valueLength += 2;
            } else {
                // 其他字符長度為1
                valueLength += 1;
            }
        }
        // 進位取整
        return Math.ceil(valueLength);
    }

    /**
     * 根據字符串長度獲取行高
     *
     * @param str
     * @return
     */
    public static Float getRowHeight(String str) {

        Integer lineCount = (int) (getLength(str) / 64) + 1;
        if (str.contains("\n")) {
            Integer tempLineCount = 1;
            String[] lines = str.split("\n");
            for (String line : lines) {
                Integer everyLineCount = (int) (getLength(line) / 64) + 1;
                tempLineCount += everyLineCount;
            }
            lineCount = lineCount >= tempLineCount ? lineCount : tempLineCount;
        }
        Float rowHeight = (float) (lineCount * 20);
        return rowHeight;
    }

你需要先獲取抓取的字符串的長度,然后通過這個方法計算行高,再將excel需要填充的該行用Java代碼設置行高(行高單位是像素),但是如果出現我上面說的字段內容無故中間部分換行,那么你獲取的行高就會不足,這個時候你需要改動這個地方----->>>>Float rowHeight = (float) (lineCount * X);  x的值一定要設置的大一行,以防出現這種情況!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM