package com.fuxin.app.action.system;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import com.fuxin.base.action.BaseAction;
import com.fuxin.web.bean.CustomerBean;
import com.fuxin.web.bean.DepartmentBean;
import com.fuxin.web.bean.TaskBean;
import com.fuxin.web.bean.UserBean;
import com.fuxin.web.bean.UserDailyBean;
import com.fuxin.web.util.DateUtil;
public class BackupAction extends BaseAction {
private static final long serialVersionUID = 1L;
private static final Log LOG = LogFactory.getLog(BackupAction.class);
private Integer[] type;
private UserBean user;
public String backup() throws Exception {
user = super.getLoginUser();
if (null == user) {
return LOGIN;
}
this.xlsDownload(type);
return NONE;
}
private WritableCellFormat wcf_title;
private WritableCellFormat wcf_value;
private void xlsDownload(Integer[] types) throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
OutputStream os = null;
WritableWorkbook wbook = null;
try {
os = response.getOutputStream();// 取得输出流
wbook = Workbook.createWorkbook(os);
response.reset();// 清空输出流
response.setHeader("Content-disposition",
"attachment; filename=backup_" + DateUtil.getDisplayYMD2() + ".xls");// 设定输出文件头
response.setContentType("application/msexcel;charset=UTF_8");// 定义输出类型
// excel 字体样式
WritableFont wf_title = new WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.BOLD);
WritableFont wf_value = new WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.NO_BOLD);
wcf_title = new WritableCellFormat(wf_title);
wcf_value = new WritableCellFormat(wf_value);
// 位置居中
wcf_title.setAlignment(Alignment.CENTRE);
wcf_value.setAlignment(Alignment.CENTRE);
for (Integer t : types) {
switch(t) {
case 1 : wbook = this.userXls(wbook);break;
//case 2 : wbook = this.attendanceXls(wbook); break;
//case 3 : wbook = this.taskXls(wbook); break;
//case 4 : wbook = this.customerXls(wbook); break;
//case 5 : wbook = this.departmentXls(wbook); break;
}
}
// 主体内容生成结束
wbook.write(); // 写入文件
} catch (Exception e) {
LOG.error(this, e);
} finally {
wbook.close();
os.close(); // 关闭流
}
}
/**
* 用户备份
*
* @param wbook
* @param users
* @return
* @throws Exception
*/
private WritableWorkbook userXls(WritableWorkbook wbook) throws Exception {
Sheet[] sheets = wbook.getSheets();
int length = 0;
if (null != sheets && sheets.length > 0) {
length = sheets.length;
}
WritableSheet wsheet = wbook.createSheet("用户表", length);// sheet名称
// 开始生成主体内容
wsheet.addCell(new Label(0, 0, "用户ID", wcf_title));
wsheet.addCell(new Label(1, 0, "用户名称", wcf_title));
wsheet.addCell(new Label(2, 0, "标题", wcf_title));
wsheet.addCell(new Label(3, 0, "登录名", wcf_title));
wsheet.addCell(new Label(4, 0, "密码", wcf_title));
wsheet.addCell(new Label(5, 0, "地址", wcf_title));
wsheet.addCell(new Label(6, 0, "联系方式", wcf_title));
wsheet.addCell(new Label(7, 0, "部门ID", wcf_title));
wsheet.addCell(new Label(8, 0, "机器码", wcf_title));
List<UserBean> users = super.getUserService().getUserList(
user.getCompanyId(), 0L,
super.getUserService().getUserListCount(user.getCompanyId()));
for (int i = 0; i < users.size(); i++) {
wsheet.addCell(new Label(0, i + 1, users.get(i).getUserId() + "", wcf_value));
wsheet.addCell(new Label(1, i + 1, users.get(i).getUserName(), wcf_value));
wsheet.addCell(new Label(2, i + 1, users.get(i).getTitle(), wcf_value));
wsheet.addCell(new Label(3, i + 1, users.get(i).getLoginName(), wcf_value));
wsheet.addCell(new Label(4, i + 1, users.get(i).getPassword(), wcf_value));
wsheet.addCell(new Label(5, i + 1, users.get(i).getAddress(), wcf_value));
wsheet.addCell(new Label(6, i + 1, users.get(i).getTel(), wcf_value));
wsheet.addCell(new Label(7, i + 1, users.get(i).getDepartId() + "", wcf_value));
wsheet.addCell(new Label(8, i + 1, users.get(i).getMachineId(), wcf_value));
}
return wbook;
}
}