<%--
  Created by IntelliJ IDEA.
  User: mc
  Date: 2021/7/7
  Time: 15:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import=" java.io.BufferedInputStream" %>
<%@ page import=" java.io.BufferedOutputStream" %>
<%@ page import=" java.io.File" %>
<%@ page import=" java.io.FileInputStream" %>
<%@ page import=" java.io.FileOutputStream" %>
<%@ page import=" java.io.IOException" %>
<%@ page import=" java.io.InputStream" %>
<%@ page import=" java.io.OutputStream" %>
<%@ page import=" java.text.SimpleDateFormat" %>
<%@ page import=" javax.servlet.http.HttpServletResponse" %>
<%@ page import="com.censoft.common.base.Pub" %>
<%@ page import="com.censoft.common.db.ConnectionFactory" %>
<%@ page import="com.censoft.db.Logger" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.poi.xssf.usermodel.*" %>
<%!
    private void download(String path, HttpServletResponse response) {
        try {
            // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
%>
<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    ConnectionFactory cf = new ConnectionFactory();
    Pub pub = new Pub();
    Connection conn = null;
    List list = new ArrayList();
    String[] field = request.getParameterValues("zc_ids");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String fileName = sdf.format(new Date()) + ".xlsx";
    // 文件路径
    String filePath = fileName;
    try {
        conn = com.censoft.db.DBUtil.getConnection();
        StringBuffer sb = new StringBuffer();
        sb.append("select t1.*,t2.xm_id,t2.ly_dept,t2.ly_user,t2.bg_time,t2.ly_time " +
                "from zc_register t1 left join zc_ly t2 on t1.zc_id=t2.zc_id where 1=1 ");
        sb.append(" and t1.zc_id in (");
        if(field!=null&&field.length>0){
            for(int i=0;i<field.length;i++){
                if(i==field.length-1){
                    sb.append("'" + field[i] + "'");
                }else{
                    sb.append("'" + field[i] + "',");
                }
            }
        }
        sb.append(" )");
        Vector v_list = cf.doQuery(conn, sb.toString(), null, new Object[]{});
        if (v_list == null) {
            System.out.println("查询出错"+cf.getMsg());
            return;
        }
        // 输出流
        OutputStream os = new FileOutputStream(filePath);
        // 工作区
        XSSFWorkbook wb = new XSSFWorkbook();
        // 工作区名
        XSSFSheet sheet = wb.createSheet("资产列表");
        XSSFCellStyle style=wb.createCellStyle();
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//水平垂直
        // 创建表头
        String values="所属项目,资产编号,资产名称,资产分类,国标大类,价值类型,价值,使用部门,使用人,资产状态,领用时间,入账日期";
        XSSFRow row = sheet.createRow(0);
        String[] value=values.split(",");
        for(int i=0;i<12;i++){
            XSSFCell row_0=row.createCell(i);
            row_0.setCellValue(value[i]);
            row_0.setCellStyle(style);
        }
        // 遍历数据装进table
        for (int i = 0; i < v_list.size(); i++) {
            // sheet.createRow(i+1) 因为表头是0
            Hashtable ht = (Hashtable) v_list.get(i);
            XSSFRow rows = sheet.createRow(i + 1);
            String bg_time = pub.trimNull((String) ht.get("bg_time"));
            String zc_status = pub.trimNull((String) ht.get("zc_status"));
            if("0".equals(zc_status)){
                zc_status="闲置";
            }else if("3".equals(zc_status)){
                zc_status="领用";
            }else if("1".equals(zc_status)){
                zc_status="已提交";
            }else if("2".equals(zc_status)){
                zc_status="审核中";
            }else if("4".equals(zc_status)){
                zc_status="盘点中";
            }else if("4_1".equals(zc_status)){
                zc_status="盘点结束";
            }else if("5".equals(zc_status)){
                zc_status="维修中";
            }else if("6".equals(zc_status)){
                zc_status="报废";
            }
            if(!"".equals(bg_time)){
                zc_status="变更";
            }
            rows.createCell(0).setCellValue((String)ht.get("xm_id"));
            rows.createCell(1).setCellValue((String)ht.get("zc_id"));
            rows.createCell(2).setCellValue((String)ht.get("zc_name"));
            rows.createCell(3).setCellValue((String)ht.get("zc_fl"));
            rows.createCell(4).setCellValue((String)ht.get("zc_gj_type"));
            rows.createCell(5).setCellValue((String)ht.get("jz_type"));
            rows.createCell(6).setCellValue((String)ht.get("jiazhi"));
            rows.createCell(7).setCellValue((String)ht.get("ly_dept"));
            rows.createCell(8).setCellValue((String)ht.get("ly_user"));
            rows.createCell(9).setCellValue(zc_status);
            rows.createCell(10).setCellValue((String)ht.get("ly_time"));
            rows.createCell(11).setCellValue((String)ht.get("cw_date"));
        }
        wb.write(os);
        // 关闭输出流
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
    download(filePath, response);
%>