- 环境搭建
jxls-1.2
依赖jars,请使用mvn构建
提供一个下载链接吧(依赖jars)
http://pan.baidu.com/share/link?shareid=729611509&uk=1125362416
- 效果展示:
- 模板展示
- java源码
package org.wcf.eg;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
public class Test {
private static String templateFileName = "templates/test.xls";
private static String destFileName = "output/test.xls";
/**
* @param args
* @throws IOException
* @throws InvalidFormatException
* @throws ParsePropertyException
*/
public static void main(String[] args) throws ParsePropertyException, InvalidFormatException, IOException {
// TODO Auto-generated method stub
List t = new ArrayList();
t.add(new Tab("东方一号周刊", 5,6));
t.add(new Tab("神马周刊", 1,5));
t.add(new Tab("土豪周刊",18,6));
Map beans = new HashMap();
beans.put("tab", t);
beans.put("index", 0);
XLSTransformer transformer = new XLSTransformer();
transformer.transformXLS(templateFileName, beans, destFileName);
}
}
package org.wcf.eg;
public class Tab {
String name;
int January;
int February;
public Tab(String nameString, int january, int february) {
this.name = nameString;
this.January = january;
this.February = february;
}
public String getName() {
return name;
}
public void setName(String nameString) {
this.name = nameString;
}
public int getJanuary() {
return January;
}
public void setJanuary(int january) {
January = january;
}
public int getFebruary() {
return February;
}
public void setFebruary(int february) {
February = february;
}
}
- 目录结构
/output/
/templates/test.excel
java 包自己修改下吧
- 使用的模板
liver writer怎么加excel附件?
放个百度网盘的链接吧
- chart部分需要自定义名称(关键部分啊)
在模板里面定义:
xv(这个可以随便选择数据源,程序中会自动修改)
=表1!$A$4:$A$6
yv1
=OFFSET(test.xls!xv,0,1)
yv2
=OFFSET(test.xls!xv,0,2)
说明:offset定义一个数据范围(range)
xv,yv:横轴数据和纵轴数据,用excel中工具自行定义吧,这个java程序不好或者说不能控制
offset
接受参数可以是5个,可以是3个(这个看excel的说明吧)
这里第一个参数是一个自定义名称,代表一个数据范围,
后两个代表相对于第一个参数的偏移行数和列数
- 参考文献
- jxls: doc
(自动弹出了一个表情,哈哈)
- http://www.programming-free.com/2012/12/create-charts-in-excel-using-java.html
- http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/ss/examples/LoanCalculator.java
三个文档够用了吧