分享

前台EXTJS后台JAVA导出EXCEL(转)

 第六感523 2011-09-16
前台EXTJS后台JAVA导出EXCEL(转)
2011年02月16日 星期三 10:19

前台

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.getElementByIdx('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();
}

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多