package com.lbc.excel.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lbc.entity.EquipmentLib;
import com.lbc.entity.OaUsers;
import com.lbc.service.AssetServiceDao;
import com.lbc.service.AssetServiceDaoImpl;
import youngPackage.db.YoungRepository;
/**
* Servlet implementation class ExportExcelServlet
*/
public class ExportExcelServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ExportExcelServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("---------------------------------ExportExcelServlet----------------------------------");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
YoungRepository youngdb = (YoungRepository)request.getSession().getAttribute("oa_db");
OaUsers user = (OaUsers)request.getSession().getAttribute("oa_user");
String eaPer = (String)request.getSession().getAttribute("currentUserEaPerGrade");
if(youngdb == null){
youngdb = new YoungRepository();
}
AssetServiceDao asd = new AssetServiceDaoImpl(youngdb);
List<EquipmentLib> list = new ArrayList<EquipmentLib>();
if("EA1004".equals(eaPer)){
list = asd.getElByUsernameOrDepartmentOrManager(user.getUsername(),"","ALL","1");
}else if("EA1002".equals(eaPer)){
list = asd.getElByUsernameOrDepartmentOrManager("",user.getDepartment(),"","1");
}
String fileName = "Excel-" + System.currentTimeMillis() + ".xls";//设置导出的文件名称
StringBuffer sb = new StringBuffer(this.gettable(list));//将表格信息放入内存
String contentType = "application/vnd.ms-excel";//定义导出文件的格式的字符串
String recommendedName = new String(fileName.getBytes(),"iso_8859_1");//设置文件名称的编码格式
response.setContentType(contentType);//设置导出文件格式
response.setHeader("Content-Disposition", "attachment; filename=" + recommendedName );//
response.resetBuffer();
//利用输出输入流导出文件
ServletOutputStream sos = response.getOutputStream();
sos.write(sb.toString().getBytes());
sos.flush();
sos.close();
}
private String gettable(List<EquipmentLib> list ){
StringBuffer table = new StringBuffer();
if(list != null && list.size() > 0){
table.append("<table border='1'><tbody><tr style='height: 35px;background-color:#fff1cc;'><th>序号</th><th>资产类别</th><th>资产名称</th><th>资产数量</th>");
table.append("<th>所在部门</th><th>使用人</th><th>状态</th><th>购置日期</th><th>开始使用日期</th>");
table.append("<th>使用年限</th><th>原值</th><th>品牌</th><th>规格型号</th><th>备注</th></tr>");
String temp = "";
for(int i = 0 ; i < list.size() ; i++){
EquipmentLib el = list.get(i);
if((i+1)%2 == 0){
table.append("<tr style='height: 35px;' bgcolor='#EDEDED'>");
}else{
table.append("<tr style='height: 35px;'>");
}
table.append("<td align='center'>"+(i+1)+"</td>");
table.append("<td align='center'>"+el.getFamily()+"</td>");
table.append("<td align='center'>"+el.getName()+"</td>");
table.append("<td align='center'>"+el.getAmount()+"</td>");
table.append("<td align='center' style='color:red;'>"+el.getDepartment()+"</td>");
table.append("<td align='center'>"+el.getCurrentUserName()+"</td>");
if("2".equals(el.getStatus())){
temp = "资产转移中";
}else if("4".equals(el.getStatus())){
temp = "报废审核中";
}else {
temp = "正常使用";
}
table.append("<td align='center'>"+temp+"</td>");
table.append("<td align='center'>"+el.getPurchaseDate()+"</td>");
table.append("<td align='center'>"+el.getStartUseDate()+"</td>");
table.append("<td align='center'>"+el.getUseLife()+"</td>");
table.append("<td align='center'>"+el.getValue()+"</td>");
if(el.getBrand() == null ){
table.append("<td align='center'></td>");
}else{
table.append("<td align='center'>"+el.getBrand()+"</td>");
}
if(el.getSpecifications() == null ){
table.append("<td align='center'></td>");
}else{
table.append("<td align='center'>"+el.getSpecifications()+"</td>");
}
if(el.getRemark() == null ){
table.append("<td align='center'></td>");
}else{
table.append("<td align='center'>"+el.getRemark()+"</td>");
}
}
table.append("</tr></tbody></table>");
}
return table.toString();
}
}