第一章——快速入门
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
1? ? 介紹
jXLS是用于生成Excel報(bào)表的小型Java類庫(kù)。jXLS使用特殊標(biāo)記在Excel模板中定義輸出格式和數(shù)據(jù)布局。
Java有優(yōu)秀的創(chuàng)建Excel文件的開源和社區(qū)類庫(kù)。值得關(guān)注的開源類庫(kù)有Apache POI和Java Excel API。在某種意義上,這些類庫(kù)非常低級(jí),需要編寫許多Java代碼才能創(chuàng)建一個(gè)簡(jiǎn)單的Excel文件。通常需要手動(dòng)設(shè)置每個(gè)單元格的格式和數(shù)據(jù)。報(bào)表布局和數(shù)據(jù)格式越復(fù)雜會(huì)導(dǎo)致Java代碼非常復(fù)雜和難以調(diào)試和維護(hù)。此外,不是所有的Excel特性都支持和使用API操作(例如,宏或圖表)。
在使用jXLS時(shí),你所需要做的就是在一個(gè)Excel模板中定義所有報(bào)表格式和數(shù)據(jù)布局,并運(yùn)行jXLS引擎,為它提供數(shù)據(jù)以填充模板。在大多數(shù)情況下,你需要編寫的唯一代碼是使用適當(dāng)配置的jXLS引擎的簡(jiǎn)單調(diào)用。
2????主要概念
2.1????Area(區(qū)域)
Area代表Excel文件中的矩形區(qū)域??梢允褂脝卧穹秶蚴褂瞄_始單元格和區(qū)域大小(行和列數(shù))定義矩形區(qū)域。Area包括特定范圍的所有Excel單元格。
每個(gè)Area可以關(guān)聯(lián)一組命令,jXLS引擎處理區(qū)域時(shí)執(zhí)行和區(qū)域相關(guān)的命令。Area可以嵌套子區(qū)域。每個(gè)子區(qū)域也是一個(gè)Area,也可以包含自己的命令和自己的子區(qū)域。
Area可以使用以下方式定義:
- 在Excel模板文件中使用特定標(biāo)記語(yǔ)法。
- 使用XML配置。
- 使用jXLS Java API。
2.2????Command(命令)
命令代表一個(gè)或多個(gè)Area的轉(zhuǎn)換動(dòng)作。Command接口如下所示:?
public interface Command {String getName();List<Area> getAreaList();Command addArea(Area area);Size applyAt(CellRef cellRef, Context context);void reset(); }Command的主要方法是Size applyAt(CellRef cellRef, Context context)。該方法在單元格cellRef執(zhí)行命令動(dòng)作。context類似于一個(gè)map,用于為命令傳遞數(shù)據(jù)。該方法返回轉(zhuǎn)換后區(qū)域的大小作為Size對(duì)象。
當(dāng)前,jXLS提供以下內(nèi)置命令:
- Each
- If
- Image
2.3????Transformer
Transformer接口允許Area與特定Excel實(shí)現(xiàn)無(wú)關(guān)。這意味著通過(guò)提供不同的Transformer接口實(shí)現(xiàn),我們可以使用不同的底層Java->Excel庫(kù)。
Transformer接口如下所示:
public interface Transformer {void transform(CellRef srcCellRef, CellRef targetCellRef, Context context, boolean updateRowHeight);void setFormula(CellRef cellRef, String formulaString);Set<CellData> getFormulaCells();CellData getCellData(CellRef cellRef);List<CellRef> getTargetCellRef(CellRef cellRef);void resetTargetCellRefs();void resetArea(AreaRef areaRef);void clearCell(CellRef cellRef);List<CellData> getCommentedCells();void addImage(AreaRef areaRef, byte[] imageBytes, ImageType imageType);void write() throws IOException;TransformationConfig getTransformationConfig();void setTransformationConfig(TransformationConfig transformationConfig);boolean deleteSheet(String sheetName);void setHidden(String sheetName, boolean hidden);void updateRowHeight(String srcSheetName, int srcRowNum, String targetSheetName, int targetRowNum); }盡管Transformer接口看起來(lái)有很多方法,但大多數(shù)方法已經(jīng)在AbstractTransformer基礎(chǔ)抽象類中實(shí)現(xiàn),如果需要提供新Java->Excel實(shí)現(xiàn),只需繼承抽象類即可。
當(dāng)前,jXLS提供兩種Transformer接口實(shí)現(xiàn):
- PoiTransformer
- JexcelTransformer
PoiTransformer使用Apache POI類庫(kù)生成Excel文件。JexcelTransformer基于較老的Java Excel API類庫(kù)。
3? ? 例子:使用jXLS基于模板輸出員工列表到Excel中
3.1? ? 引入依賴
jXLS對(duì)Apache POI和Java Excel API進(jìn)行高級(jí)封裝,因此,要使用jXLS必須引入底層Apache POI或Java Excel API,毋庸置疑,Apache POI是Java開源社區(qū)最強(qiáng)Excel解決方案,因此,我們選擇使用Apache POI作為jXLS的底層API。下面是引入Apache POI的依賴配置:
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.15</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.15</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.15</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.15</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-excelant</artifactId><version>3.15</version> </dependency>接下來(lái)我們需要引入jXLS的核心類庫(kù):
<dependency><groupId>org.jxls</groupId><artifactId>jxls</artifactId><version>2.4.5</version> </dependency>jXLS核心類庫(kù),并沒有直接實(shí)現(xiàn)對(duì)Excel的操作,而是定義了兩個(gè)適配器,分別使用Apache POI和Java Excel API對(duì)Excel進(jìn)行操作。因?yàn)槲覀冞x擇使用Apache POI作為底層API操作Excel,因此,我們需要導(dǎo)入Apache POI適配器依賴:
<dependency><groupId>org.jxls</groupId><artifactId>jxls-poi</artifactId><version>1.0.15</version> </dependency>一個(gè)完整的Maven依賴配置如下所示:
<!-- jXLS核心庫(kù) --> <dependency><groupId>org.jxls</groupId><artifactId>jxls</artifactId><version>2.4.5</version> </dependency> <!-- jXLS-POI適配器 --> <dependency><groupId>org.jxls</groupId><artifactId>jxls-poi</artifactId><version>1.0.15</version> </dependency> <!-- Apache POI依賴包 --> <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.15</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.15</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.15</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.15</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-excelant</artifactId><version>3.15</version> </dependency>3.1? ? 定義員工實(shí)體類
/*** 員工實(shí)體類* * @since 2018年7月11日* @author 趙凡* @version 1.0**/ public class Employee {private String name;// 員工名稱private String sex;// 性別private String telephone;// 手機(jī)號(hào)碼private Date birthday;// 生日private BigDecimal payment;// 工資// ... 構(gòu)造函數(shù)// ... getters/setters}3.2? ??創(chuàng)建Excel模板
模板是使用特定標(biāo)記規(guī)定jXLS應(yīng)該如何輸出數(shù)據(jù)的Excel文件。jXLS提供一些內(nèi)置標(biāo)記處理器解析Excel模板和提取控制命令。如有需要可以自定義標(biāo)記處理器。因此,可以為Excel模板定義自己的標(biāo)記,并以適當(dāng)?shù)姆绞竭M(jìn)行解析,以創(chuàng)建jXLS命令結(jié)構(gòu)。
默認(rèn),jXLS以Apache JEXL作為表達(dá)式語(yǔ)言在Excel模板中引用Java對(duì)象屬性和方法。對(duì)象必須在jXLS上下文中某一鍵上可用。為了在單元格中輸出員工名稱,可以在單元格中放入${employee.name}。使用${和}環(huán)繞JEXL表達(dá)式。假設(shè)在上下文中employee鍵下有一個(gè)Employee對(duì)象。
輸出員工列表信息的最終模板如下所示:
模板中第三行的單元格使用JEXL表達(dá)式引用employee對(duì)象的屬性。單元格A1包含的Excel注釋jx:area(lastCell="E4")定義模板根區(qū)域?yàn)锳1:E4。單元格A3包含的Excel注釋jx:each(items="employees" var="employee" lastCell="E4")定義jXLS Each命令。Each命令迭代employees(由items屬性定義)集合的條目到上下文的employee(由var屬性定義)鍵。執(zhí)行Each命令的區(qū)域是A3:E4(有l(wèi)astCell屬性定義),該區(qū)域?qū)?huì)被克隆,并使用上下文中的每個(gè)新的Employee對(duì)象處理。
3.3????使用jXLS API處理模板
List<Employee> employees = generateSampleEmployeeData(); try(InputStream is = ObjectCollectionDemo.class.getResourceAsStream("object_collection_template.xls")) {try (OutputStream os = new FileOutputStream("target/object_collection_output.xls")) {Context context = new Context();context.putVar("employees", employees);JxlsHelper.getInstance().processTemplate(is, os, context);} }運(yùn)行程序生成如下所示的Excel:
?
?
?
轉(zhuǎn)載于:https://my.oschina.net/leeck/blog/1844426
總結(jié)
- 上一篇: Vue+axios配置踩坑记录
- 下一篇: nginx配置多个server