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