使用jxls自定義命令設(shè)置動(dòng)態(tài)行高
前言
之前的博客中都簡(jiǎn)單說(shuō)了數(shù)據(jù)的渲染和導(dǎo)出excel文件。包括固定的表頭結(jié)構(gòu),以及動(dòng)態(tài)表頭和表數(shù)據(jù)等方式。
本篇博客主要說(shuō)明自定義命令的方式,控制輸出excel文件每行記錄的行高。
依賴(lài)引入
主要依賴(lài)以及版本如下所示:
<dependency> <groupId>org.jxls</groupId> <artifactId>jxls</artifactId> <version>2.4.5</version> </dependency> <dependency> <!-- 可以使用poi的實(shí)現(xiàn)也可以用jexcelapi的 --> <groupId>org.jxls</groupId> <artifactId>jxls-poi</artifactId> <version>1.0.15</version> </dependency> <dependency> <groupId>org.jxls</groupId> <artifactId>jxls-jexcel</artifactId> <version>1.0.7</version> </dependency> <dependency> <groupId>net.sf.jxls</groupId> <artifactId>jxls-core</artifactId> <version>1.0.6</version> </dependency>
繪制 jxls 批注的 excel 模板
其中兩個(gè)批注分別如下:
- 整體數(shù)據(jù)范圍:
Administrator: jx:area(lastCell=”H3”)
- 列表數(shù)據(jù)渲染范圍:
Administrator: jx:each(items=“bDatas” var=“vo” lastCell=“H3” varIndex=“ojbIndex” )
測(cè)試類(lèi)編寫(xiě)
編寫(xiě)一個(gè)簡(jiǎn)單的數(shù)據(jù)填充邏輯,并生成對(duì)應(yīng)的excel文件。
代碼如下所示:
import cn.xj.test.UserPo; import com.google.common.collect.Lists; import org.jxls.builder.xls.XlsCommentAreaBuilder; import org.jxls.common.Context; import org.jxls.util.JxlsHelper; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import java.io.*; import java.util.*; public class Test1 { public static void main(String[] args) throws IOException { Context context = new Context(); // 數(shù)據(jù)集合 List<UserPo> dataList = Lists.newArrayList(); for (int i = 0; i < 10; i++) { UserPo userPo = new UserPo(); userPo.setNum("1_"+i); userPo.setName("xj_"+i); userPo.setAge(i+1); userPo.setMail("專(zhuān)注寫(xiě)bug測(cè)試中文11111"); dataList.add(userPo); } // ${item.num} context.putVar("bDatas",dataList); // 模板文件再resources 目錄下 Resource resource = new ClassPathResource("/report/test_user1.xlsx"); InputStream is = resource.getInputStream(); String outFile = System.getProperty("user.dir")+ File.separator+"springboot-poi"+File.separator+"pdf"+File.separator+System.currentTimeMillis()+ ".xlsx"; OutputStream outputStream = new FileOutputStream(outFile); JxlsHelper jxlsHelper = JxlsHelper.getInstance(); jxlsHelper.getAreaBuilder().getTransformer(); jxlsHelper.processTemplate(is, outputStream, context); // JxlsHelper.getInstance().processTemplate(is, outputStream, context); } }
執(zhí)行后,生成excel文件中內(nèi)容的效果如下所示:
每行的行高太大,畢竟再模板中就是配置的這么大,顯得很散亂。
此時(shí)則可以使用自定義命令的方式,動(dòng)態(tài)地修改行高。
自定義命令
jxls中自定義命令,可以采取繼承 AbstractCommand 類(lèi)實(shí)現(xiàn)。自定義命令需要定義命令名稱(chēng)和命令邏輯。
如下所示:
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.jxls.area.Area; import org.jxls.command.AbstractCommand; import org.jxls.common.CellRef; import org.jxls.common.Context; import org.jxls.common.Size; import org.jxls.transform.poi.PoiTransformer; /** * 自定義列高指令 * 如: * jx:autoRowHeight(lastCell ="C3") * * 還需要在對(duì)應(yīng)的主程序中調(diào)用 */ public class AutoRowHeightCommand extends AbstractCommand { /** * 批注中的自定義指令 * @return */ @Override public String getName() { return "autoRowHeight"; } /** * 列高邏輯 * @param cellRef * @param context * @return */ @Override public Size applyAt(CellRef cellRef, Context context) { Area area=getAreaList().get(0); Size size = area.applyAt(cellRef, context); PoiTransformer transformer = (PoiTransformer) area.getTransformer(); Sheet sheet = transformer.getWorkbook().getSheet(cellRef.getSheetName()); // List bDatas = (List) context.getVar("bDatas"); // int firstDefaultCol = cellRef.getCol(); // 最開(kāi)始的第一列 // if(!CollectionUtils.isEmpty(bDatas)){ // for (int i = 0; i < bDatas.size(); i++) { // // 計(jì)算中文、字符的長(zhǎng)度 設(shè)定列寬 // Object data = bDatas.get(i); // if(!StringUtils.isEmpty(data) && (data.getBytes().length+4)>sheet.getColumnWidth(i)){ // sheet.setColumnWidth(i+firstDefaultCol,data.getBytes().length+4); // }else{ // sheet.setColumnWidth(i+firstDefaultCol,30); // 默認(rèn) // } // // } // } //sheet.setColumnWidth(cellRef.getCol(),50); Row row = sheet.getRow(cellRef.getRow()); row.setHeight((short) -1); return size; } }
自定義命令后,需要再模板中增加命令的標(biāo)識(shí),否則不會(huì)生效。
jx:autoRowHeight(lastCell =“H3”)
其次,還需要再調(diào)用jxls做填充渲染之前,補(bǔ)充命令和邏輯的調(diào)用。
import cn.xj.jxls.AutoRowHeightCommand; import cn.xj.test.UserPo; import com.google.common.collect.Lists; import org.jxls.builder.xls.XlsCommentAreaBuilder; import org.jxls.common.Context; import org.jxls.util.JxlsHelper; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import java.io.*; import java.util.*; public class Test1 { public static void main(String[] args) throws IOException { Context context = new Context(); // 數(shù)據(jù)集合 List<UserPo> dataList = Lists.newArrayList(); for (int i = 0; i < 10; i++) { UserPo userPo = new UserPo(); userPo.setNum("1_"+i); userPo.setName("xj_"+i); userPo.setAge(i+1); userPo.setMail("專(zhuān)注寫(xiě)bug測(cè)試中文11111"); dataList.add(userPo); } // ${item.num} context.putVar("bDatas",dataList); // 模板文件再resources 目錄下 Resource resource = new ClassPathResource("/report/test_user1.xlsx"); InputStream is = resource.getInputStream(); String outFile = System.getProperty("user.dir")+ File.separator+"springboot-poi"+File.separator+"pdf"+File.separator+System.currentTimeMillis()+ ".xlsx"; OutputStream outputStream = new FileOutputStream(outFile); JxlsHelper jxlsHelper = JxlsHelper.getInstance(); jxlsHelper.getAreaBuilder().getTransformer(); // 渲染前 載入 自定義 命令 XlsCommentAreaBuilder.addCommandMapping("autoRowHeight", AutoRowHeightCommand.class); jxlsHelper.processTemplate(is, outputStream, context); } }
執(zhí)行后的效果如下所示:
關(guān)于自動(dòng)換行
jxls沒(méi)有對(duì)應(yīng)的自動(dòng)換行操作,但是jxls可以在模板中定義對(duì)應(yīng)的單元格樣式。只需要在模板中對(duì)需要做自動(dòng)換行的列增加如下配置。
再次執(zhí)行上述的代碼邏輯,查看顯示效果。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于IDEA 的遠(yuǎn)程調(diào)試 Weblogic的操作過(guò)程
這篇文章主要介紹了基于IDEA 的遠(yuǎn)程調(diào)試 Weblogic的操作過(guò)程,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09java:try...catch跳過(guò)異常繼續(xù)處理循環(huán)問(wèn)題
這篇文章主要介紹了java:try...catch跳過(guò)異常繼續(xù)處理循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Spring?MVC中@Controller和@RequestMapping注解使用
這篇文章主要介紹了Spring?MVC中@Controller和@RequestMapping注解使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02java 實(shí)現(xiàn)切割文件和合并文件的功能
這篇文章主要介紹了java 實(shí)現(xiàn)切割文件和合并文件的功能的相關(guān)資料,這里實(shí)現(xiàn)文件的切割的實(shí)現(xiàn)代碼和文件合并的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-07-07必須掌握的十個(gè)Lambda表達(dá)式簡(jiǎn)化代碼提高生產(chǎn)力
這篇文章主要為大家介紹了必須掌握的十個(gè)Lambda表達(dá)式來(lái)簡(jiǎn)化代碼提高生產(chǎn)力,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04詳解SpringMVC的url-pattern配置及原理剖析
這篇文章主要介紹了SpringMVC的url-pattern配置及原理剖析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Java多線(xiàn)程中wait?notify等待喚醒機(jī)制詳解
這篇文章主要介紹了Java多線(xiàn)程中wait?notify等待喚醒機(jī)制,由于線(xiàn)程之間是搶占式執(zhí)行的,因此線(xiàn)程的執(zhí)行順序難以預(yù)知,但是實(shí)際開(kāi)發(fā)中有時(shí)候我們希望合理的協(xié)調(diào)多個(gè)線(xiàn)程之間的執(zhí)行先后順序,所以這里我們來(lái)介紹下等待喚醒機(jī)制,需要的朋友可以參考下2024-10-10