excel文件导出_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > excel文件导出

excel文件导出

 2013/8/21 12:57:01  清心明目  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.capinfo.supervise.web.count;importjava.io.FileInputStream;importjava.io.InputStream;importjava.util.List;importorg.apache.struts2.convention.annotation.Namespace;importorg.apache.struts2.convention.annotation.Result;importorg.apache
  • 标签:excel 文件
class="java" name="code">package com.capinfo.supervise.web.count;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.capinfo.supervise.entity.NationSupCount;
import com.capinfo.supervise.manager.NationSupCountManager;
import com.capinfo.supervise.vo.NationSupCountVo;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/supervise/count")
@Results({
	@Result(name="export",type="stream",params={
			"contentType","application/xls",
			"contentDisposition","attachment;filename=\"${downloadFileName}\"",
			"inputName","inputStream",
			"bufferSize","4096"
	})
})
public class SupCountAction extends ActionSupport{

	private static final long serialVersionUID = 7315697641372551936L;
	
	//注入
	@Autowired
	NationSupCountManager supCountManager;
	/**
	 * 导出Excel使用
	 */
	private List<NationSupCount> supCountList;
	private String zipFileName;
	
	public String expExcel(){
		
		NationSupCountVo qvo = new NationSupCountVo();
		//添加查询条件(略)
		//根据条件获取list列表
		supCountList = supCountManager.getCountListByQvo(qvo);
		//获取下载的文件的路径
		zipFileName = supCountManager.expExcel(supCountList);
		
		return "export";
	}
	
    public InputStream getInputStream() throws Exception {
		/**
		 * 下载用的Action应该返回一个InputStream实例
		 */
        InputStream fis = null ;
		try {
			fis = new FileInputStream(zipFileName);
			//fis.close(); 仍在使用,不能关闭
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return fis;
	}
	    
    // 处理下载文件的名字
	public String getDownloadFileName() {
		String downloadFileName = null;
		downloadFileName =  "superviseCount.xls";
//		try {
//			downloadFileName = java.net.URLEncoder.encode(downloadFileName,"GBK");
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}
		
		return downloadFileName;
	}
	
	//-------------------getter/setter-------------------------

	public List<NationSupCount> getSupCountList() {
		return supCountList;
	}

	public void setSupCountList(List<NationSupCount> supCountList) {
		this.supCountList = supCountList;
	}

	public String getZipFileName() {
		return zipFileName;
	}

	public void setZipFileName(String zipFileName) {
		this.zipFileName = zipFileName;
	}
	
}

?导出Excel分两步:

?

1.Excel文件的生成

2.Excel文件的下载

下载的示例代码如下:?

?

发表评论
用户名: 匿名