package com.ichangeman.cms.company;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import share.DAO;
import share.Dispatcher;
import share.Req;
import com.ichangeman.cms.Lib;
import com.ichangeman.cms.PageList;
import com.ichangeman.cms.person.GetDateFormat;
public class ExportCompany extends Dispatcher {
private ServletConfig config;
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}
final public ServletConfig getServletConfig() {
return config;
}
public void dispatcher(HttpServletRequest request,
HttpServletResponse response) {
Lib.init(request);
DAO dao = new DAO();
String action = request.getParameter("action");
if (action != null && "exportCompany".equals(action)) {
String companyId = request.getParameter("companyId");
String filePath = "\\export\\company\\"
+ GetDateFormat.getTime(new Date()) + ".xls";
File file = new File(request.getRealPath(filePath));
try {
file.createNewFile();
String url = request.getRequestURL().toString();
ArrayList list = getExportCompany(dao, request, companyId);
// 创建可写入的Excel工作薄
WritableWorkbook wwb;
wwb = Workbook.createWorkbook(file);
// 创建Excel工作表
WritableSheet ws = wwb.createSheet("公司信息列表", 0);
Label data = null;
jxl.write.Number labelN = null;
// 添加标题
String type = Req.getPara(request, "dicttypeid");
if (type == null || "".equals(type)) {
return;
}
ArrayList typeList = dao
.conList("select field,fieldname from t_reportsel where tablename = 't_company' and type='"
+ type + "' order by seq");
if (typeList == null) {
return;
}
for (int x = 0; typeList != null && x < typeList.size(); x++) {
Map map1 = (Map) typeList.get(x);
String fieldname = map1.get("fieldname").toString();
Label column1 = new Label(x, 0, fieldname);
ws.addCell(column1);
}
int column = 0;
int row = 1;
// 添加数据
for (int i = 0; list != null && i < list.size(); i++) {
Map map2 = (Map) list.get(i);
for (int j = 0; typeList != null && j < typeList.size(); j++) {
Map map3 = (Map) typeList.get(j);
String field = map3.get("field").toString();
if ("vocationid".equals(field)) {
String vocationid = dao
.conValue("select d.dataname from t_dictdata d,t_dicttype t where d.dicttypeid = t.dicttypeid and t.typecode='vocationid' and d.datacode='"
+ map2.get(field).toString() + "'");
data = new Label(column, row, vocationid);
ws.addCell(data);
column++;
} else if ("customertype".equals(field)) {
String customertype = dao
.conValue("select d.dataname from t_dictdata d,t_dicttype t where d.dicttypeid = t.dicttypeid and t.typecode='customertype' and d.datacode='"
+ map2.get(field).toString() + "'");
data = new Label(column, row, customertype);
ws.addCell(data);
column++;
} else if ("capiscale".equals(field)) {
String capiscale = dao
.conValue("select d.dataname from t_dictdata d,t_dicttype t where d.dicttypeid = t.dicttypeid and t.typecode='capiscale' and d.datacode='"
+ map2.get(field).toString() + "'");
data = new Label(column, row, capiscale);
ws.addCell(data);
column++;
} else if ("persscale".equals(field)) {
String persscale = dao
.conValue("select d.dataname from t_dictdata d,t_dicttype t where d.dicttypeid = t.dicttypeid and t.typecode='persscale' and d.datacode='"
+ map2.get(field).toString() + "'");
data = new Label(column, row, persscale);
ws.addCell(data);
column++;
} else if ("infosource".equals(field)) {
String infosource = dao
.conValue("select d.dataname from t_dictdata d,t_dicttype t where d.dicttypeid = t.dicttypeid and t.typecode='infosource' and d.datacode='"
+ map2.get(field).toString() + "'");
data = new Label(column, row, infosource);
ws.addCell(data);
column++;
} else if ("customerlevel".equals(field)) {
String customerlevel = dao
.conValue("select d.dataname from t_dictdata d,t_dicttype t where d.dicttypeid = t.dicttypeid and t.typecode='customerlevel' and d.datacode='"
+ map2.get(field).toString() + "'");
data = new Label(column, row, customerlevel);
ws.addCell(data);
column++;
} else {
data = new Label(column, row, map2.get(field)
.toString());
ws.addCell(data);
column++;
}
}
column = 0;
row++;
}
// 写入Exel工作表
wwb.write();
// 关闭Excel工作薄对象
wwb.close();
request.setAttribute("filePath", filePath);
} 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();
}
forward(request, response,"/person/export_personsucceed.jsp");
} else {
ArrayList list = companyquery(dao, request, response);
request.setAttribute("list", list);
// 转到显示层
forward(request, response, "/company/export_companyquery.jsp");
}
}
private ArrayList getExportCompany(DAO dao, HttpServletRequest request, String companyId) {
// 设置参数
String deforder = " order by corpid desc "; // 缺省的排序条件
String table = "t_company"; // 要查询的表名
String sfield = "*"; // 要在select 语句中查询的字段,空或*表示所有
String whereCond = " where corpid in ("+companyId+")";// 附加的查询条件
String url = "";
// 查询数据库
PageList lst = new PageList(request, url, deforder, "1000000",
"");
ArrayList list = lst.initQry(dao, table, whereCond, sfield);
return list;
}
public ArrayList companyquery(DAO dao, HttpServletRequest request,
HttpServletResponse response) {
// 查询功能
String corpname = Req.getPara(request, "corpname").trim();
String vocationid = Req.getPara(request, "vocationid");
String province = Req.getPara(request, "province");
String city = Req.getPara(request, "city");
String address = Req.getPara(request, "address").trim();
String businessscope = Req.getPara(request, "businessscope").trim();
String customertype = Req.getPara(request, "customertype");
String infosource = Req.getPara(request, "infosource");
String isbusiness = Req.getPara(request, "isbusiness");
String islatency = Req.getPara(request, "islatency");
String ispurchase = Req.getPara(request, "ispurchase");
String iscase = Req.getPara(request, "iscase");
String action = Req.getPara(request, "action");
request.setAttribute("corpname", corpname);
request.setAttribute("vocationid", vocationid);
request.setAttribute("province", province);
request.setAttribute("city", city);
request.setAttribute("address", address);
request.setAttribute("businessscope", businessscope);
request.setAttribute("customertype", customertype);
request.setAttribute("infosource", infosource);
request.setAttribute("isbusiness", isbusiness);
request.setAttribute("islatency", islatency);
request.setAttribute("ispurchase", ispurchase);
request.setAttribute("iscase", iscase);
// 设置参数
String deforder = "