前台EXTJS后台JAVA导出EXCEL

前台 JSP:
<div style="display:none">
  <form name="exportExcel"
id="exportExcel" action="" target="export" method="post"
accept-charset="UTF-8"></form>
<iframe name="export" id="export"
></iframe>
</div>
EXTJS : function exportToXls(){
var cp = Ext.getCmp("cpType").getValue();
var pay = Ext.getCmp("qdBox").getValue();
var f = document.getElementById('exportExcel');
var actionUrl =
"geqfyj.do?method=export&date=" +
cellDate+"&cp="+cp+"&pay="+pay;
f.action = actionUrl;
f.submit();
} 后台: public class Column {
private int index;
private String metaName;
private String displayName;
private boolean isDouble = false;
private int size = 1;
private int columnWidth = 10;

public Column(int index, String meta, String display,boolean
isDouble,int size,int columnWidth) {
this.index = index;
this.metaName = meta;
this.displayName = display;
this.isDouble = isDouble;
this.size = size;
this.columnWidth = columnWidth;

public String getDisplayName() {
return displayName;

public int getIndex() {
return index;

public String getMetaName() {
return metaName;

public void setDisplayName(String displayName) {
this.displayName = displayName;

public void setIndex(int index) {
this.index = index;

public void setMetaName(String metaName) {
this.metaName = metaName;

public boolean isDouble() {
return isDouble;

public void setDouble(boolean isDouble) {
this.isDouble = isDouble;

public int getSize() {
return size;

public void setSize(int size) {
this.size = size;

public int getColumnWidth() {
return columnWidth;

public void setColumnWidth(int columnWidth) {
this.columnWidth = columnWidth;
} } public class Excel {

public static void export(OutputStream os, List list, Class c,
Map map, String title, String sheetName) throws Exception {

jxl.write.WritableWorkbook wbook =
Workbook.createWorkbook(os); // 建立excel文件
jxl.write.WritableSheet wsheet = wbook.createSheet(sheetName,
0); // sheet名称
jxl.write.WritableFont wfont = null; // 字体
jxl.write.WritableCellFormat wcfFC = null; // 字体格式
jxl.write.Label wlabel = null; // Excel表格的Cell
// 设置excel标题字体
wfont = new jxl.write.WritableFont(WritableFont.ARIAL,
16, WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
wcfFC = new jxl.write.WritableCellFormat(wfont);

// 添加excel标题
jxl.write.Label wlabel1 = new jxl.write.Label(2, 0, title,
wcfFC); wsheet.addCell(wlabel1);

// 设置列名字体
// 如果有标题的话,要设置一下偏移
int offset = 2;
if (title == null || title.trim().equals(""))
offset = 0;
else { wfont = new jxl.write.WritableFont(WritableFont.ARIAL,
14, WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
wcfFC = new jxl.write.WritableCellFormat(wfont);

// 根据原数据和map来创建Excel的列名
String[] fieldArr = ReflectUtil.getDeclaredFields(c);
for (int i=0;i<fieldArr.length;i++) {
String name = fieldArr[i];
if (map.containsKey(name)) {
wfont = new jxl.write.WritableFont(WritableFont.ARIAL,
10, WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
wcfFC = new jxl.write.WritableCellFormat(wfont);
Column col = (Column) map.get(name);
wlabel = new jxl.write.Label(col.getIndex(), offset, col
.getDisplayName(),wcfFC);
wsheet.setColumnView(col.getIndex(),
col.getColumnWidth());
wsheet.addCell(wlabel);
} } 
// 设置正文字体
wfont = new jxl.write.WritableFont(WritableFont.TIMES,
14, WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
wcfFC = new jxl.write.WritableCellFormat(wfont);

// 往Excel输出数据
int rowIndex = 1 + offset;
Collection array = map.values();
for (Object obj : list) {
Iterator it = array.iterator();
while (it.hasNext()) {
Column col = (Column) it.next();
String value = "";
if(ReflectUtil.getAttribute(obj, col.getMetaName()) !=
null){ if(col.isDouble()){
double temp = Double.parseDouble(ReflectUtil.getAttribute(obj,
col.getMetaName()).toString());
value = String.valueOf(temp/col.getSize());
}else{ value = ReflectUtil.getAttribute(obj,
col.getMetaName()).toString();
} } wlabel = new jxl.write.Label(col.getIndex(), rowIndex,
value); wsheet.addCell(wlabel);
} rowIndex++;
}  
wbook.write(); // 写入文件
wbook.close();
os.flush();
os.close();

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值