您的位置:首页 > 其它

导出到excel表格

2015-11-13 09:02 423 查看
jsp:

function outExcel(){

var url = "action.do?method=operMethod"

+"¶m1=111"

+"¶m2=222"

+"¶m3=333";

window.location.href = url;

}

}

________________________________________________________________________________________

java

List<?> excelA = null;

excelA = this.getOutExcel(controller, command);

ArrayList excelL = new ArrayList();

if (excelA.size() > 0) {

for (int i = 0; i < excelA.size(); i++) {

String excelS = excelA.get(i).toString();

String[] temp = excelS.substring(0,excelS.length()-1).split(",");

ArrayList tempAL = new ArrayList();

for(int j=0;j<temp.length;j++)

{

tempAL.add(temp[j].split("=")[1]);

}

excelL.add(tempAL);

}

}

request.setAttribute("excelL", excelL);

request.setAttribute("title", "title");

return new ModelAndView("/path/Output_Excel");

_________________________________________________________________________________________

Output_Excel.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%@ page import="java.io.*,jxl.*,jxl.write.*"%>

<%@ page import="jxl.format.UnderlineStyle"%>

<%@ page import="java.util.ArrayList"%>

<%@ page import="com.hsoft.flow.service.IFlowNewManager,java.net.URLDecoder" %>

<%

ArrayList excelA = (ArrayList)request.getAttribute("excelL");

String batchNo = (String)request.getAttribute("title");

String filename = batchNo+".xls";

response.setHeader("Content-Disposition" ,"attachment;filename="+filename);

String tableName = "明细表";

String tableHead = "";

tableHead = "办事处,姓名,登记编号,领取人签字";

ArrayList temp = new ArrayList();

if(tableHead.indexOf(",")!=-1) {

String[] headL = tableHead.split(",");

for(int j=0;j<headL.length;j++) {

temp.add(headL[j]);

}

} else {

temp.add(tableHead);

}

excelA.add(0,temp);

OutputStream os = response.getOutputStream(); // 将WritableWorkbook 写入到输出流

WritableWorkbook wwb = Workbook.createWorkbook(os);

try {

WritableSheet ws = wwb.createSheet(tableName, 10); // 创建一个工作表

// 设置单元格的文字格式

WritableFont wf = new WritableFont(WritableFont.ARIAL, 12,WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);

WritableCellFormat wcf = new WritableCellFormat(wf);

wcf.setVerticalAlignment(VerticalAlignment.CENTRE);

wcf.setAlignment(Alignment.CENTRE);

WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 20,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);

WritableCellFormat wcf1 = new WritableCellFormat(wf1);

wcf1.setVerticalAlignment(VerticalAlignment.CENTRE);

wcf1.setAlignment(Alignment.CENTRE);

CellView cellView = new CellView();

cellView.setAutosize(true); //设置自动大小

ws.mergeCells(0,0,4,0);

ws.addCell(new Label(0,0,"《通知书》明细表",wcf1));

ws.setRowView(1, 500);

// 填充数据的内容

if (excelA.size() > 0) {

for (int i = 0; i < excelA.size(); i++) {

ws.setColumnView(0,30);

if(i==0){

ws.addCell(new Label(0, (i+1) , "序号", wcf));

}else{

ws.addCell(new Label(0, (i+1) , i+"", wcf));

}

temp = (ArrayList) excelA.get(i);

int l = temp.size();

for (int j = 0; j < l; j++) {

ws.setColumnView(j+1,30);

ws.addCell(new Label((j+1), (i+1) , temp.get(j).toString(), wcf));

}

}

}

os.flush();

wwb.write();

wwb.close();

os.close();

out.clear();

out = pageContext.pushBody();

} catch (Exception e) {

wwb.close();

os.close();

}

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