- 浏览: 7630 次
- 性别:
- 来自: 南通
文章分类
最新评论
-
jaw111:
非常感谢!!!JEXL1对中文支持不是很好,刚好需要用JXLS ...
jxls 升级 jexl 1.1 至 2.X 支持中文标签
修改net.sf.jxls.parser包下的
Expression
ExpressionCollectionParser
Property
package net.sf.jxls.parser;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import net.sf.jxls.transformer.Configuration;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.MapContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Represents a property in excel template
* @author Leonid Vysochyn
*/
public class Property {
protected final Log log = LogFactory.getLog(getClass());
List propertyTokens = new ArrayList();
private String beanName;
private Object bean;
private String collectionName;
private Collection collection;
private String property;
private Object propertyValue;
Configuration config;
public Property(String value) {
propertyValue = value;
}
public Property(String property, Map beans, Configuration config) {
this.property = property;
this.config = config;
propertyValue = getPropertyValue(beans);
}
public boolean isConstant(){
return property==null;
}
public Object getPropertyValue(Map beans) {
JexlContext jc = new MapContext();
jc.set("map", beans);
// JexlContext context = JexlHelper.createContext();
// context.setVars(beans);
ExpressionCollectionParser parser = new ExpressionCollectionParser(jc,this.property + ";", config.isJexlInnerCollectionsAccess());
if (parser.getCollection() == null) {
propertyValue = null;
} else {
collectionName = parser.getCollectionExpression();
collection = parser.getCollection();
beanName = null;
bean = null;
}
return propertyValue;
}
public boolean isCollection() {
return collectionName != null;
}
public boolean isNull() {
return getPropertyValue() == null;
}
public String getBeanName() {
return beanName;
}
public String getCollectionName() {
return collectionName;
}
public void setCollectionName(String collectionName) {
this.collectionName = collectionName;
}
public String getProperty() {
return property;
}
public Collection getCollection() {
return collection;
}
public void setCollection(Collection collection) {
this.collection = collection;
}
public String getFullCollectionName() {
if (beanName == null) {
return collectionName;
}
return beanName + "." + collectionName;
}
public String getPropertyNameAfterLastDot() {
String propertyName = null;
if (property != null) {
int dotIndex = property.lastIndexOf(".");
if (dotIndex >= 0) {
propertyName = property.substring(dotIndex + 1);
} else {
propertyName = property;
}
}
return propertyName;
}
public String getPropertyNameAfterFirstDot() {
String propertyName = null;
if (property != null) {
int dotIndex = property.indexOf(".");
if (dotIndex >= 0) {
propertyName = property.substring(dotIndex + 1);
} else {
propertyName = property;
}
}
return propertyName;
}
public String toString() {
return "Property{" +
"property='" + property + "'}";
}
public Object getPropertyValue() {
if( bean instanceof String){
return bean;
}
return propertyValue;
}
public void setPropertyValue(Object propertyValue) {
this.propertyValue = propertyValue;
}
public Object getBean() {
return bean;
}
public void setBean(Object bean) {
this.bean = bean;
}
}
Expression
package net.sf.jxls.parser; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sf.jxls.transformer.Configuration; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.jexl2.JexlEngine; import org.apache.commons.jexl2.MapContext; /** * Represents JEXL expression * @author Leonid Vysochyn */ public class Expression { public static final String aggregateSeparator = "[a-zA-Z()]+[0-9]*:"; String expression; String rawExpression; String aggregateFunction; String aggregateField; Map beans; List properties = new ArrayList(); org.apache.commons.jexl2.Expression jexlExpresssion; Configuration config; Property collectionProperty; public Property getCollectionProperty() { return collectionProperty; } public List getProperties() { return properties; } public String getExpression() { return expression; } public Expression(String expression, Configuration config) { this.expression = expression; this.config = config; } public Expression(String expression, Map beans, Configuration config) throws Exception { this.config = config; this.expression = expression; this.rawExpression = parseAggregate(expression); this.beans = beans; //修改表达式 创建方法 JexlEngine JEXL = new JexlEngine(); jexlExpresssion = JEXL.createExpression("map."+rawExpression); // jexlExpresssion = ExpressionFactory.createExpression( rawExpression ); parse(); } private void parse() { Property prop = new Property(rawExpression, beans, config); this.properties = new ArrayList(); this.properties.add(prop); if (prop.isCollection() && aggregateFunction == null && collectionProperty == null) { this.collectionProperty = prop; } } public Object evaluate() throws Exception { if (beans != null && !beans.isEmpty()){ /*-------------------开始修改------------------------------*/ org.apache.commons.jexl2.JexlContext jc = new MapContext(); jc.set("map", beans); //jexl 2 开始必须要名称 默认设置一个名称map Object ret = jexlExpresssion.evaluate(jc); JexlEngine JEXL1 = new JexlEngine(); org.apache.commons.jexl2.Expression e = JEXL1.createExpression("map."+rawExpression);//在表达式中增加 我在前面设置的名称 map Object ret1 = jexlExpresssion.evaluate(jc); /*-------------------结束修改------------------------------*/ if (aggregateFunction != null) { return calculateAggregate(aggregateFunction, aggregateField, ret); } return ret; } return expression; } private String parseAggregate(String expr) { String[] aggregateParts = expr.split(aggregateSeparator, 2); int i = expr.indexOf(":"); if (aggregateParts.length >= 2 && i >= 0) { String aggregate = expr.substring(0, i); if (aggregate.length() == 0) { aggregateFunction = null; aggregateField = null; } else { int f1 = aggregate.indexOf("("); int f2 = aggregate.indexOf(")"); if (f1 != -1 && f2 != -1 && f2 > f1) { aggregateFunction = aggregate.substring(0, f1); aggregateField = aggregate.substring(f1+1, f2); } else { aggregateFunction = aggregate; aggregateField = "c1"; } } return expr.substring(i+1); } aggregateFunction = null; aggregateField = null; return expr; } private Object calculateAggregate(String function, String field, Object list) { Aggregator agg = Aggregator.getInstance(function); if (agg != null) { if (list instanceof Collection) { Collection coll = (Collection) list; for (Iterator iterator = coll.iterator(); iterator.hasNext();) { Object o = iterator.next(); try { Object f = PropertyUtils.getProperty(o, field); agg.add(f); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } else { try { Object f = PropertyUtils.getProperty(list, field); agg.add(f); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return agg.getResult(); } return list; } public String toString() { return expression; } }
ExpressionCollectionParser
package net.sf.jxls.parser; import java.io.StringReader; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import org.apache.commons.jexl2.Expression; import org.apache.commons.jexl2.JexlContext; import org.apache.commons.jexl2.JexlEngine; import org.apache.commons.jexl2.parser.ASTIdentifier; import org.apache.commons.jexl2.parser.ASTReference; import org.apache.commons.jexl2.parser.Node; import org.apache.commons.jexl2.parser.Parser; import org.apache.commons.jexl2.parser.SimpleNode; public class ExpressionCollectionParser { public final static String COLLECTION_REFERENCE_SUFFIX = "_JxLsC_"; // This is set up as a ThreadLocal parser to avoid threading issues. private static ThreadLocal parser = new ThreadLocal() { protected synchronized Object initialValue() { return new Parser(new StringReader(";")); } }; private boolean jexlInnerCollectionsAccess; private String collectionExpression; private Collection collection; public ExpressionCollectionParser(org.apache.commons.jexl2.JexlContext jexlContext, String expr, boolean jexlInnerCollectionsAccess) { try { this.jexlInnerCollectionsAccess = jexlInnerCollectionsAccess; SimpleNode tree = ((Parser) parser.get()).parse(new StringReader(expr),null); ArrayList references = new ArrayList(); findReferences(references, tree); findCollection(jexlContext, references); } catch (Exception e) { e.printStackTrace(); } } public String getCollectionExpression() { return collectionExpression; } public Collection getCollection() { return collection; } private void findReferences(List references, Node node) { if (node instanceof ASTReference) { references.add(node); } int childCount = node.jjtGetNumChildren(); for (int i = 0; i < childCount; i++) { findReferences(references, node.jjtGetChild(i)); } } private void findCollection(JexlContext jexlContext, List references) { Node node; for (Iterator itr = references.iterator(); itr.hasNext();) { node = (Node) itr.next(); String newExpression = findCollectionProperties(jexlContext, node); if (newExpression != null) { if (!newExpression.endsWith(COLLECTION_REFERENCE_SUFFIX)) { this.collectionExpression = newExpression; } break; } } } private String findCollectionProperties(JexlContext jexlContext, Node node) { int childCount = node.jjtGetNumChildren(); Node child ; String subExpr = null; for (int i = 0; i < childCount; i++) { child = node.jjtGetChild(i); if (child instanceof ASTIdentifier) { ASTIdentifier ident = (ASTIdentifier) child; if (subExpr == null) { subExpr =ident.image; } else { subExpr = subExpr + "." + ident.image;; } if( jexlInnerCollectionsAccess ){ if (subExpr.endsWith(COLLECTION_REFERENCE_SUFFIX)) { return subExpr; } } try { JexlEngine JEXL = new JexlEngine(); Expression e = JEXL.createExpression("map."+subExpr); // Expression e = ExpressionFactory.createExpression(subExpr); Object obj = e.evaluate(jexlContext); if (obj instanceof Collection) { this.collection = (Collection) obj; return subExpr; } } catch (Exception e) { // TODO: insert proper logging here return null; } } } return null; } }
Property
package net.sf.jxls.parser;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import net.sf.jxls.transformer.Configuration;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.MapContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Represents a property in excel template
* @author Leonid Vysochyn
*/
public class Property {
protected final Log log = LogFactory.getLog(getClass());
List propertyTokens = new ArrayList();
private String beanName;
private Object bean;
private String collectionName;
private Collection collection;
private String property;
private Object propertyValue;
Configuration config;
public Property(String value) {
propertyValue = value;
}
public Property(String property, Map beans, Configuration config) {
this.property = property;
this.config = config;
propertyValue = getPropertyValue(beans);
}
public boolean isConstant(){
return property==null;
}
public Object getPropertyValue(Map beans) {
JexlContext jc = new MapContext();
jc.set("map", beans);
// JexlContext context = JexlHelper.createContext();
// context.setVars(beans);
ExpressionCollectionParser parser = new ExpressionCollectionParser(jc,this.property + ";", config.isJexlInnerCollectionsAccess());
if (parser.getCollection() == null) {
propertyValue = null;
} else {
collectionName = parser.getCollectionExpression();
collection = parser.getCollection();
beanName = null;
bean = null;
}
return propertyValue;
}
public boolean isCollection() {
return collectionName != null;
}
public boolean isNull() {
return getPropertyValue() == null;
}
public String getBeanName() {
return beanName;
}
public String getCollectionName() {
return collectionName;
}
public void setCollectionName(String collectionName) {
this.collectionName = collectionName;
}
public String getProperty() {
return property;
}
public Collection getCollection() {
return collection;
}
public void setCollection(Collection collection) {
this.collection = collection;
}
public String getFullCollectionName() {
if (beanName == null) {
return collectionName;
}
return beanName + "." + collectionName;
}
public String getPropertyNameAfterLastDot() {
String propertyName = null;
if (property != null) {
int dotIndex = property.lastIndexOf(".");
if (dotIndex >= 0) {
propertyName = property.substring(dotIndex + 1);
} else {
propertyName = property;
}
}
return propertyName;
}
public String getPropertyNameAfterFirstDot() {
String propertyName = null;
if (property != null) {
int dotIndex = property.indexOf(".");
if (dotIndex >= 0) {
propertyName = property.substring(dotIndex + 1);
} else {
propertyName = property;
}
}
return propertyName;
}
public String toString() {
return "Property{" +
"property='" + property + "'}";
}
public Object getPropertyValue() {
if( bean instanceof String){
return bean;
}
return propertyValue;
}
public void setPropertyValue(Object propertyValue) {
this.propertyValue = propertyValue;
}
public Object getBean() {
return bean;
}
public void setBean(Object bean) {
this.bean = bean;
}
}
相关推荐
jxls作为比较好的excel模版方案,比poi和easyExcel相对扩展性更高。 demo包含全部官方例子和模版, 2.6官方新增合并单元格标签,jx:mergecells 以及其他相关一切demo,很好的资源,分享给大家,供大家学习交流。 ...
该资源用于 根据Excel模板导出相应数据所需jar包。commons-jexl-2.1.1.jar 和 jxls-core1.0.3.jar
最近项目poi升级,导出excel有异常。通过修改源码,支持poi4.x。已经完美验证。
项目案例 http://blog.csdn.net/lnktoking/article/details/52932679
解决net.sf.jxls1.0.6中getCellType()和org.apache.poi4.1.2报错问题
jXLS是一个小而易用的Java库,它用于根据XLS模板文件生成Excel数据文件,或者根据XML配置文件从Excel文件中读出数据。 jXLS的功能是:只使用几行代码就可以建立极端复杂的Excel报表。你所需要实现的大部分工作是...
此版本是自己编译的,增加了实现java 在excel模版上导出图片的功能,并优化了模版导出数据功能
一.JXLS简介 ,安装,以及标签使用说明 Each数据循环、公式使用、if-else逻辑判断、加载图片、动态表格、数据分组、单元格超链接、SQL模板实现、自定义表达式解析引擎、自定义函数、单元格合并
jxls-core-1.0.7.jar包源码,支持jdkl8以及poi4.0以上的版本,原jar包作者的最高版本多年没有维护,通过修改原作者源码,为1.0.7版本支持高版本的poi库如poi4.0.1及以上。
兼容jdk1.7-兼容POM3.11及以上版本;如果需要源码,可以查看 https://download.csdn.net/download/iamcuilong/12066123
jxls2.3 官网最新下载 包含例子、文档 、jar
jxls-core-0.9.6.jar为的是更好的发扬优良的精要的代码!
jxls poi jxls-2.2.9.zip
jxls-core升级为poi4.1.2
jxls-jexcel-1.0.6.jar java读取excel文件的jarjxls-jexcel-1.0.6.jar
commons-jexl-2.1.1.jar commons-lang3-3.4.jar commons-logging-1.2.jar jxl-2.6.10.jar log4j-1.2.17.jar poi-3.12.jar poi-ooxml-3.12.jar poi-ooxml-3.12.jar slf4j-api-1.7.7.jar slf4j-log4j12-1.7.7.jar ...
jxls-core-1.0.jar java excel导出工具包
1. 数据导出至Excel.doc(包括当前导出方法小结,自己的示例,参考的网站) ...5. jxls-reader-0.9.6.jar,commons-jexl-1.1.jar,jxls-core-0.9.6.jar:jxls操作Excel的开发包,与poi共同构成当前java下Excel开发主流包
使用jxls技术,自己创建Excel模板导出数据
jxls-reader-0.9.6.jar 为的是更好的发展精良的代码!