利用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的值一定要設置的大一行,以防出現這種情況!

?

轉載于:https://www.cnblogs.com/lg-wxf/p/9474427.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279616.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279616.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279616.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Jmeter HTTP請求后響應數據顯示亂碼解決方法

Jmeter請求后結果樹里無論是text還是html響應數據顯示亂碼&#xff0c;這是因為jmeter 編碼格式配置文件默認不開啟導致的&#xff0c;解決方法如下&#xff1a; 1&#xff09;進入jmeter-***\bin目錄下&#xff0c;找到jmeter.properties文件&#xff0c;以文本文件形式打開 2…

禁用windows10更新_如何在Windows 10中禁用投影

禁用windows10更新The drop shadows on applications in the Windows 10 preview are really big and suspiciously similar to the ones in OS X, and if they aren’t your speed, you can easily remove them. We actually think they look good, but since somebody out th…

如何訪問 Service?- 每天5分鐘玩轉 Docker 容器技術(99)

前面我們已經學習了如何部署 service&#xff0c;也驗證了 swarm 的 failover 特性。不過截止到現在&#xff0c;有一個重要問題還沒有涉及&#xff1a;如何訪問 service&#xff1f;這就是本節要討論的問題。 為了便于分析&#xff0c;我們重新部署 web_server。 ① docker se…

sqlyog下載

sqlyog下載&#xff08;附注冊碼&#xff09;&#xff1a;http://www.onlinedown.net/soft/24926.htm轉載于:https://www.cnblogs.com/shujuxiong/p/9474496.html

Linux配置手冊(二)配置DHCP服務器

1.檢查是否安裝DHCP服務器軟件 2.掛在RHEL5系統光盤 3.安裝DHCP服務軟件 4.將模板配置文件復制并覆蓋現在的配置文件 5.配置修改dhcpd.conf文件 配置信息 默認租約時間 default-lease-time 最大租約時間 max-lease-time 局域網內所有主機的域名 option domain-name 客戶機所使用…

什么是Google Play保護以及如何確保Android安全?

Android is open, flexible, and all about choice. Unfortunately, that flexibility comes more potential security issues. The good news is that Google has a system in place named Play Protect that helps keep Android secure. Android開放&#xff0c;靈活且具有多…

如何使計算機為您讀取文檔

Since the beginning of the computer age, people have always enjoyed making computers talk to them. These days, that functionality is built right into Windows and you can easily use it to have your PC read documents to you. 自計算機時代開始以來&#xff0c;人…

面試中常問的List去重問題,你都答對了嗎?

2019獨角獸企業重金招聘Python工程師標準>>> 面試中經常被問到的list如何去重&#xff0c;用來考察你對list數據結構&#xff0c;以及相關方法的掌握&#xff0c;體現你的java基礎學的是否牢固。 我們大家都知道&#xff0c;set集合的特點就是沒有重復的元素。如果集…

Coolite Toolkit學習筆記五:常用控件Menu和MenuPanel

Coolite Toolkit里的Menu控件和其他的.NET Web控件不一樣&#xff0c;如果只是設計好了Menu或是通過程序初始化菜單項&#xff0c;菜單是不會呈現在界面上的&#xff0c;因為Coolite Toolkit規定Menu控件需要一個容器來做依托&#xff0c;而這個讓Menu依托的控件就是MenuPanel&…

剛接觸git,提交文件時,遇到no changes added to commit

第一次用git 在提交&#xff08;git commit -m add 文件名&#xff09;的時候&#xff0c;遇到了一個no changes added to commit&#xff0c;大體意思是沒有將改變的東西提交成功&#xff0c;查了很多博客&#xff0c;才解決這個問題&#xff0c;然后自己也做一下筆記&#…

CSS中!important的使用

本篇文章使用最新的IE10以及firefox與chrome測試&#xff08;截止2013年5月27日22:23:22&#xff09;http://www.cnblogs.com/yudy/archive/2013/05/27/3102825.html CSS的原理&#xff1a; 我們知道&#xff0c;CSS寫在不同的地方有不同的優先級&#xff0c; .css文件中的定義…

windows命令提示符_如何個性化Windows命令提示符

windows命令提示符Command line interfaces can be downright boring and always seem to miss out on the fresh coats of paint liberally applied to the rest of Windows. Here’s how to add a splash of color to Command Prompt and make it unique. 命令行界面可能非常…

android-api28轉換到api19-不能編譯

安裝出現錯誤- rootponkan:/ # pm install /mnt/usb/sda1/app-debug.apkpkg: /mnt/usb/sda1/app-debug.apk Failure [INSTALL_FAILED_OLDER_SDK]查看系統和api版本 rootponkan:/ # getprop ro.build.version.release 5.1.1 rootponkan:/ # getprop ro.build.version.sdk 22將ap…

Java多線程編程 — 鎖優化

2019獨角獸企業重金招聘Python工程師標準>>> 閱讀目錄 一、盡量不要鎖住方法 二、縮小同步代碼塊&#xff0c;只鎖數據 三、鎖中盡量不要再包含鎖 四、將鎖私有化&#xff0c;在內部管理鎖 五、進行適當的鎖分解 正文 并發環境下進行編程時&#xff0c;需要使用鎖機…

Android Ap 開發 設計模式第六篇:原型模式

Prototype Pattern 名稱由來 不是利用類來產生實例對象&#xff0c;而是從一個對象實例產生出另一個新的對象實例 &#xff0c;根據被視為原型的對象實例 &#xff0c;建立起的另一個新的對象實例就稱為原型模式&#xff08;Ptototype Pattern&#xff09;。 需求場景 種類過多…

netty實現客戶端服務端心跳重連

前言&#xff1a; 公司的加密機調度系統一直使用的是http請求調度的方式去調度&#xff0c;但是會出現網絡故障導致某個客戶端或者服務端斷線的情況&#xff0c;導致很多請求信息以及回執信息丟失的情況&#xff0c;接著我們拋棄了http的方式&#xff0c;改為Tcp的方式去建立客…

為什么您仍然不應該購買《星球大戰:前線II》

If you’ve been following video game news at all for the last couple of weeks, you’ve probably heard that EA’s Star Wars: Battlefront II is having some teething troubles. EA has backpedaled to avoid more controversy, but we’re here to say: don’t fall f…

web 后臺返回json格式數據的方式(status 406)

1.在類上使用注解 RestController public class HttpComentInterface {} 2.在方法是使用注解 ResponseBody RequestMapping(path "/interface/queryRemote", method RequestMethod.POST) //可以指定請求方式ResponseBody public RemoteCommentResultData queryCo…

OpenStack Juno系列之計算節點搭建

OpenStack Juno系列之計算節點搭建 nova-compute安裝配置 -------------------- apt-get install nova-compute sysfsutils 編輯配置文件 vi /etc/nova/nova.conf [DEFAULT] verbose True rpc_backend rabbit rabbit_host controller rabbit_password RABBIT_PASS auth_str…

quantum_如何從Firefox Quantum刪除Pocket

quantumFirefox Quantum has deep integration with the Pocket read-it-later service, which is now owned by Mozilla. You’ll see a Pocket page action in the address bar, a “View Pocket List” feature in the Library, and recommended articles from Pocket on th…