excel文件导出

 

 导出Excel分两步:

 

1.Excel文件的生成

2.Excel文件的下载

下载的示例代码如下: 

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.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;
	}
	
}

 

 

 

你可能感兴趣的:(java,excel导出)