您的位置:首页 > 产品设计 > UI/UE

easyUI导出excel文件 提供下载功能

2015-10-27 10:45 363 查看
实现思路:

1.把数据导入到excel表中,存放在项目upload文件夹下,

2.利用Struts2提供的下载功能把excel表从项目的upload文件夹中下载下来.

3.删除项目中的excel文件.

----------java代码:

private InputStream ist1;

private String fileName;

//导出excel

public String getExcelData(){

OnlineApply onlineApply=new OnlineApply();

onlineApply.setName(getRequest().getParameter("name"));//筛选条件

onlineApply.setStatus(getRequest().getParameter("status"));

onlineApply.setCourseId(getRequest().getParameter("courseId"));

String[] headers={"报名时间","姓名","手机号码"," QQ","报选课程","留言"};//表头

WritableWorkbook wwb;

FileOutputStream fos;

String realPath = ServletActionContext.getServletContext().getRealPath("/upload");

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");//格式化时间输出

String fileName1 = sdf.format(new Date());// 取得当前时间

String fileName2=realPath+"\\"+fileName1+".xls";

fileName=fileName1+".xls";//获取保存的文件名

try {

//获取需要导出的数据 list

List<OnlineApply> list=onlineApplyService.queryOnlineApplyInfo1(onlineApply);

fos = new FileOutputStream(fileName2);

wwb = Workbook.createWorkbook(fos);//创建可写入的Excel工作薄

//创建Excel工作表 (名称,位置)

WritableSheet ws = wwb.createSheet("在线报名表", 0);

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

WritableFont wf = new WritableFont(WritableFont.ARIAL, 12,

WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,

Colour.BLUE);

WritableCellFormat wcf = new WritableCellFormat(wf); //Format

wcf.setVerticalAlignment(VerticalAlignment.CENTRE);

wcf.setAlignment(Alignment.CENTRE);

ws.setRowView(0, 500);//设置表头行高

//填充表头

for(int i=0;i<headers.length;i++){

ws.addCell(new Label(i+1, 0, headers[i], wcf));

}

// 填充数据的内容

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

OnlineApply onlineApply1 = (OnlineApply) list.get(i);

ws.addCell(new Label(1, i+1, onlineApply1.getApplyTime().toString()));

ws.addCell(new Label(2, i+1, onlineApply1.getName()));

ws.addCell(new Label(3, i+1, onlineApply1.getPhone()));

ws.addCell(new Label(4, i+1, onlineApply1.getQqnumber()));

ws.addCell(new Label(5, i+1, onlineApply1.getApplyCourse()));

ws.addCell(new Label(6, i+1, onlineApply1.getMemberLeave()));

ws.setColumnView(i+1, 14);//设置每列列宽

ws.setRowView(i+1, 300);//设置内容行高

}

ws.setColumnView(1, 20);//设置第一列列宽

ws.setColumnView(6, 60);//设置第六列列宽

wwb.write();//写入excel

wwb.close();//关闭excel工作簿

fos.close();//关闭输出流

//下载

ist1=ServletActionContext.getServletContext().getResourceAsStream("upload/"+fileName);

if (fileName!=null) {//删除服务器上的文件

String realPath1 = ServletActionContext.getServletContext().getRealPath("");

File f = new File(realPath1);

File deleteFile1 = new File(f,fileName);

System.out.println(deleteFile1);

if (deleteFile1 != null) {

deleteFile1.delete();// 删除原有文件

}

}

} catch (Exception e) {

e.printStackTrace();

}

return SUCCESS;

}

public InputStream getIst1() {

return ist1;

}

public void setIst1(InputStream ist1) {

this.ist1 = ist1;

}

public String getFileName() {

return fileName;

}

public void setFileName(String fileName) {

this.fileName = fileName;

}

---------Struts2配置

<action name="OnlineApplyAction_getExcelData" class="com.zte.wm.action.OnlineApplyAction" method="getExcelData">

<result type="stream">

<param name="contentType">application/vnd.ms-excel</param>

<param name="contentDisposition">attachment;fileName="${fileName}"</param>

<param name="inputName">ist1</param>

<param name="bufferSize">1024</param>

</result>

</action>

注意:提交方式不能用ajax提交,如果有中文筛选条件不能用get提交,只能用post提交,ajax提交无法下载,get提交中文会乱码,或者url被防火墙拦截报500错误。

页面:分页的第二页还有2行数据






下载的excel文件






筛选数据:






excel下载文件




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