JXL导出到Excel

 

package action;


import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import model.UserModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class ExportAction extends ActionSupport{
	    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	    public String execute(){  
	    	   HttpServletResponse response = ServletActionContext.getResponse();  
	    	    String fileName = "强大.xls";  
	    	    try {  
	    	        fileName = new String(fileName.getBytes(), "iso8859-1");//解决中文 文件名问题  
	    	    } catch (UnsupportedEncodingException e1) {  
	    	        e1.printStackTrace();  
	    	    }  
	    	    response.setHeader("content-disposition", "attachment; filename="  
	    	            + fileName);// 设定输出文件头  
	    	    response.setContentType("application/msexcel");// 定义输出类型  
	    	    getInputStream(response);  
	    	    return null;  
	    }  
	    
	    public void getInputStream(HttpServletResponse response) {  
	        try {  
	            OutputStream os = response.getOutputStream();  
	            WritableWorkbook wwb = Workbook.createWorkbook(os);  
	            WritableSheet ws = wwb.createSheet("困难生认定结果", 0);  
	            WritableFont wfc = new WritableFont(WritableFont.ARIAL, 15,  
	                    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,  
	                    Colour.GREEN);  
	            WritableCellFormat wcfFC = new WritableCellFormat(wfc);  
	            Label label1 = new Label(0, 0, "学号", wcfFC);  
	            ws.addCell(label1);  
	            Label label2 = new Label(1, 0, "姓名", wcfFC);  
	            ws.addCell(label2);  
	            
	            List<UserModel> list = getList();  
	            int i = 1;  
	            for (UserModel user : list) {  
	                Label id = new Label(0, i, user.getUserId());  
	                ws.addCell(id);  
	                Label name = new Label(1, i, user.getName());  
	                ws.addCell(name);  
	                i++;  
	            }  
	            wwb.write();  
	            wwb.close();  
	            os.close();  
	        } catch (IOException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        } catch (RowsExceededException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        } catch (WriteException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        }  
	    }  
	    
	    public List<UserModel> getList(){
	    	List<UserModel> list = new ArrayList<UserModel>();
	    	list.add(new UserModel("200826630522","张三"));
	    	list.add(new UserModel("200826630523","李四"));
	    	list.add(new UserModel("200826630524","王五"));
	    	list.add(new UserModel("200826630525","赵六"));
	    	return list;
	    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值