第三章——jXLS Excel标记
為什么80%的碼農都做不了架構師?>>> ??
jXLS的Excel標記分為3種:
- Bean屬性標記
- 區域標記
- 命令標記
jXLS提供XlsCommentAreaBuilder類從Excel單元格注釋讀取標記。XlsCommentAreaBuilder實現通用AreaBuilder接口。AreaBuilder接口如下所示:
public interface AreaBuilder {List<Area> build(); }這是一個單方法簡單接口,返回一個Area對象列表。因此,如果你想要定義自己的標記,可以創建自己的AreaBuilder實現,解釋輸入Excel模板或任意其它輸入。
1? ? Bean屬性標記
jXLS使用Apache JEXL表達式語言處理Bean屬性標記。在未來版本中,可以配置表達式語言引擎,以便在需要時可以用任何其他表達式引擎替換JEXL。默認,jXLS期望JEXL表達式放在模板文件中的${和}之間。例如,以下單元格內容${department.chief.age} years告訴jXLS,假設,在Context中有一個department對象有一個chief對象有一個age屬性,使用JEXL計算department.chief.age。如果表達式department.getChief().getAge()計算等于35,jXLS將放置35 years在單元格中。
2? ? 區域標記
jXLS區域標記用于定義jXLS引擎處理的根XlsArea。XlsCommentAreaBuilder支持的Excel單元格注釋區域定義語法如下所示:
jx:area(lastCell="<LAST_CELL>")<LAST_CELL>定義矩形區域右下角單元格。第一個單元格是定義Excel注釋的單元格。因此,假設我們有一個注釋jx:area(lastCell="G12")在單元格A1中,根區域將讀取A1:G12。
XlsCommentAreaBuilder應該用于從模板文件讀取所有區域。例如,以下代碼讀取所有區域到xlsAreaList,然后保存第一個區域到xlsArea變量:
AreaBuilder areaBuilder = new XlsCommentAreaBuilder(transformer); List<Area> xlsAreaList = areaBuilder.build(); Area xlsArea = xlsAreaList.get(0);大多數情況下,定義一個根區域就足夠了。
3? ? 命令標記
命令應該定義在XlsArea中。XlsCommentAreaBuilder接收以下命令符創建Excel單元格注釋:
jx:<command_name>(attr1='val1' attr2='val2' ... attrN='valN' lastCell=<last_cell> areas=["<command_area1>", "<command_area2", ... "<command_areaN>"])<command_name>是在XlsCommentAreaBuilder中預注冊或手動注冊的命令名稱。當前,預注冊了以下命令:
- each
- if
- image
使用XlsCommentAreaBuilder的static void addCommandMapping(String commandName, Class clazz)方法注冊自定義命令。
attr1,?attr2,…,?attrN是命令屬性。
例如,if命令有condition屬性設置條件表達式。
<last_cell>定義命令區域的右下角單元格。右上角由注釋所在單元格決定。
<command_area1>,?<command_area2>, …?<command_areaN>——區域作為參數傳遞給命令。
例如,if命令期望以下區域被定義:
- ifArea:當if條件計算為true時,輸出區域的引用。
- elseArea(可選):當if條件計算為false時,輸出區域的引用。
if命令的區域屬性定義如下所示:
areas=["A8:F8","A13:F13"]一個單元格注釋可以定義多個命令。例如,Each和If命令:
jx:each(items="department.staff", var="employee", lastCell="F8") jx:if(condition="employee.payment <= 2000", lastCell="F8", areas=["A8:F8","A13:F13"])4? ? 表達式語言
默認,jXLS使用Apache JEXL表達式語言計算Excel模板文件中的屬性表達式。
4.1????自定義JEXL處理
如果你需要自定義JEXL處理,可以從Transformer獲取JexlEngine引用并設置必須的配置。
例如,以下代碼在demo命名空間注冊一個自定義JEXL函數:
Transformer transformer = TransformerFactory.createTransformer(is, os); // ... JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator) transformer.getTransformationConfig().getExpressionEvaluator(); Map<String, Object> functionMap = new HashMap<>(); functionMap.put("demo", new JexlCustomFunctionDemo()); evaluator.getJexlEngine().setFunctions(functionMap);下面是JexlCustomerFunctionDemo類:
public Integer mySum(Integer x, Integer y){return x + y; }因此,模板中可以這樣使用函數:
${demo:mySum(x,y)}x和y是Context中的參數。
4.2????改變表達式引擎
你可能不喜歡使用Apache JEXL,而想要使用其它表達式處理引擎,例如,SpEL。jXLS允許你使用喜歡的計算引擎替代默認的計算引擎。
你只需要實現ExpressionEvaluator接口的一個方法委托表達式計算處理給你想要的引擎:
public interface ExpressionEvaluator {Object evaluate(String expression, Map<String,Object> context); }然后,傳入你的ExpressionEvaluator實現給TransformationConfig:
ExpressionEvaluator evaluator = new MyCustomEvaluator(); // 基于SpEL的自定義實現 transformer.getTransformationConfig().setExpressionEvaluator(evaluator);?
轉載于:https://my.oschina.net/leeck/blog/1845844
總結
以上是生活随笔為你收集整理的第三章——jXLS Excel标记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在centos 7 下安装图形界面
- 下一篇: 『TensorFlow』SSD源码学习_