<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:宋体; mso-ascii-font-family:"Times New Roman"; mso-hansi-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->
simple Bean
介绍
这一章描述了访问对象属性的语法,满足了规定的语法才能使 jXLS 引擎正确的处理 XLS 模板。
下面的代码示例了如何将参数传递个 XLSTransformer 。
Department department;
... //initialization
Map beans = new HashMap();
beans.put("department", department);
XLSTransformer transformer = new XLSTransformer();
transformer.transformXLS(xlsTemplateFileName, beans, outputFileName);
Department 对象
public class Department {
private String name;
private Employee chief;
private List staff = new ArrayList();
public Department(String name) {
this.name = name;
}
public Department(String name, Employee chief, List staff) {
this.name = name;
this.chief = chief;
this.staff = staff;
}
public void addEmployee(Employee employee) {
staff.add(employee);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Employee getChief() {
return chief;
}
public void setChief(Employee chief) {
this.chief = chief;
}
public List getStaff() {
return staff;
}
public void setStaff(List staff) {
this.staff = staff;
}
}
基本属性访问
在 Excel 文件中使用注释来访问对象的属性:
${department.name}
jXLS 引擎将会查找 Map 的 key 值是否一个叫“ department ”的,如果有将尝试取出 name 的属性并填入相应的 Excel 的单元格中。
同样也可以访问更复杂的对象。例如输出 department 中 chief 的 name 。
${department.chief.name}
可以达到对象属性的任意深度。例如:
${bean.bean1.bean2.bean3.bean4.bean5.bean6.bean7.bean8.bean9.beanX.property1}
单个单元格使用多个属性
我们也可以在一个单元格中访问多个属性,例如:
Employee: ${employee.name} - ${employee.age} years
它的输出结果如下:
Employee: John - 35 years