📄 outputgroupservlet.java
字号:
package cn.jx.ecjtu.oa.servlets.contract;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jxl.Workbook;
import jxl.write.Label;
import cn.jx.ecjtu.oa.common.Constant;
import cn.jx.ecjtu.oa.ps.pojo.UserCard;
import cn.jx.ecjtu.oa.services.ServiceFactory;
import cn.jx.ecjtu.oa.services.UserCardService;
import cn.jx.ecjtu.oa.services.UserInSession;
public class OutputGroupServlet extends HttpServlet {
/**
* 创建可写入的Excel
* @author 王一沛
*/
public void writeExcel(OutputStream os,int user_id,int groupId) throws IOException {
try {
int num = 0;
UserCardService service = (UserCardService) ServiceFactory.getService(UserCardService.class);
List<UserCard> list = new ArrayList<UserCard>();
list = service.findAllCardsById(user_id,groupId);
num = list.size();
// 创建Excel工作表
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
jxl.write.WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
// **************往工作表中添加数据*****************
// 添加Labe对象
ws.addCell(new Label(0, 0, "姓名"));
ws.addCell(new Label(1, 0, "性别"));
ws.addCell(new Label(2, 0, "出生日期"));
ws.addCell(new Label(3, 0, "职务"));
ws.addCell(new Label(4, 0, "联系人单位名称"));
ws.addCell(new Label(5, 0, "家庭地址"));
ws.addCell(new Label(6, 0, "移动电话号码"));
ws.addCell(new Label(7, 0, "电子邮件"));
ws.addCell(new Label(8, 0, "QQ号码"));
ws.addCell(new Label(9, 0, "MSN号码"));
int x = 0;
for (x=1; x<num + 1; x++) {
UserCard card = list.get(x-1);
if (card.getName()==null){
card.setName("");}
if(card.getGender()==true){
ws.addCell(new Label(1, x, "男"));
}
else {
ws.addCell(new Label(1, x, "女"));
}
if(card.getBirthday()==null){
Date date=new Date(0000-00-00);
card.setBirthday(date);
}
if(card.getDuty()==null){
card.setDuty("");
}
if(card.getCompany_name()==null){
card.setCompany_name("");
}
if(card.getHome_address()==null){
card.setHome_address("");
}
if(card.getPhone()==null){
card.setPhone("");
}
if(card.getEmail()==null){
card.setEmail("");
}
if(card.getQq()==null){
card.setQq("");
}
if(card.getMsn()==null){
card.setMsn("");
}
ws.addCell(new Label(0, x, card.getName()));
//ws.addCell(new Label(1, x, card.getGender().toString()));
ws.addCell(new Label(2, x, card.getBirthday().toString()));
ws.addCell(new Label(3, x, card.getDuty()));
ws.addCell(new Label(4, x, card.getCompany_name()));
ws.addCell(new Label(5, x, card.getHome_address()));
ws.addCell(new Label(6, x, card.getPhone()));
ws.addCell(new Label(7, x, card.getEmail()));
ws.addCell(new Label(8, x, card.getQq()));
ws.addCell(new Label(9, x, card.getMsn()));
}
// 写入工作表
wwb.write();
// 关闭Excel工作薄对象
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public OutputGroupServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
UserInSession user=(UserInSession)session.getAttribute(Constant.USER_IN_SESSION);
int group_id=Integer.parseInt(request.getParameter("group_id"));//获取groupId
response.setHeader("Content-disposition",
"attachment;filename=group.xls");//设定输出文件头文件
response.setContentType("application/vnd.ms-excel");//定义输出类型
writeExcel(response.getOutputStream(),user.getId(),group_id);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occure
*/
private UserCardService service;
public void init() throws ServletException {
this.service=(UserCardService)ServiceFactory.getService(UserCardService.class);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -