关于jxls2.6.0的学习以及遇到的问题(四)
访问jxls官网:http://jxls.sourceforge.net/
官网中也有例子:https://bitbucket.org/leonate/jxls-demo/src/master/。
笔者会在最后一篇总结中将笔者的项目和Excel文件打包上传。
if command
参照官网,和each 命令一样,没有出现什么问题。只有一点,如果出现不同的行使用if,数据在excel展示会乱掉。笔者由于是初学,还没有解决这个问题
笔者的后台代码
// 命令--if
public static void ifCommand() throws Exception {
BufferedInputStream is = new BufferedInputStream(
new FileInputStream("E:\\wrc\\jxls\\study\\ifTeplate.xlsx"));
OutputStream os = new FileOutputStream("E:\\wrc\\jxls\\study\\if.xlsx");
JxlsHelper jxlsHelper = JxlsHelper.getInstance();
Transformer transformer = jxlsHelper.createTransformer(is, os);
Context context = new Context();
context.putVar("x", 7);
context.putVar("y", 7);
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (int i = 1; i < 10; i++) {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("name", "name" + i);
list.add(hashMap);
}
List<Map<String, String>> list2 = new ArrayList<Map<String, String>>();
for (int i = 11; i < 20; i++) {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("name", "name" + i);
list2.add(hashMap);
}
context.putVar("employees", list);
context.putVar("employees2", list2);
jxlsHelper.setUseFastFormulaProcessor(false).processTemplate(context, transformer);
}
笔者的Excel,这个excel的意思是如果x<=y 则使用employees填充数据,否则使用employees2填充数据