前台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();
}
}
<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();
}
}
相关文章推荐
- [ExtJS5学习笔记]第三十四节 sencha extjs 5 grid表格之java后台导出excel
- [ExtJS5学习笔记]第三十四节 sencha extjs 5 grid表格之java后台导出excel
- extjs4 前台导出grid数据 生成excel,数据量大后台无法接收到数据
- [ExtJS5学习笔记]第三十四节 sencha extjs 5 grid表格之java后台导出excel
- [ExtJS5学习笔记]第三十四节 sencha extjs 5 grid表格之java后台导出excel
- java导出Excel之前台后台
- 前台extjs后台 java poi 读取excel数据到数据库中
- java后台导出excel,客户端下载
- 将后台数据读取到前台的EXCEL文件中去,用javascript实现,asp.net,javacript(发一个原创)
- java、Extjs导出数据库里的数据至Excel并下载至本地进行数据备份方法之二——使用Action
- Java 根据模板导出Excel时,如何根据后台数据设置复选框的勾选
- 数据源导出为Excel java后台代码整理
- 前台到后台的交互,至成功导出Excel
- java开发,前台用EXTJS展示,后台 swfuplaod批量图片上传组件的运用
- 纯前台js json导出到excel,不与后台交互
- Java后台list导出excel
- java后台poi方式导出excel和解析excel
- extjs4.2+java导出excel表格数据
- Extjs4---grid导出excel文件,前台导出
- ExtJS4 动态生成的grid导出为excel(纯前台)