您的位置:首页 > 编程语言 > Java开发

前台EXTJS后台JAVA导出EXCEL

2014-12-30 13:22 399 查看
前台 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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: