zoukankan      html  css  js  c++  java
  • poi导出excel表

    public void downloadAssementResult(EmpQuery query,HSSFWorkbook workbook, HttpServletRequest request,
    HttpServletResponse response){
    // SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
    response.reset();
    // 获得国际化语言
    RequestContext requestContext = new RequestContext(request);
    String CourseCompany = requestContext.getMessage("assessment-Result");
    response.setContentType("APPLICATION/vnd.ms-excel;charset=UTF-8");
    // 注意,如果去掉下面一行代码中的attachment; 那么也会使IE自动打开文件。
    response.setHeader(
    "Content-Disposition",
    "attachment; filename="
    + java.net.URLEncoder.encode(
    DateUtil.getExportDate() + ".xls", "UTF-8"));
    OutputStream os = response.getOutputStream();// new
    query.setEmployeeCode(CurrentUserUtil.getCurrentUserName());
    // List<EmployeeAssessmentModel> list = employeeService.fetchAssessPage(query);
    List<Map> list=employeeService.fetchAssessPage(query);
    // 产生Excel表头
    HSSFSheet sheet = workbook.createSheet(DateUtil.getExportDate()
    + CourseCompany);
    sheet.setDefaultColumnWidth((short) 17);
    // 创建属于上面Sheet的Row,参数0可以是0~65535之间的任何一个,
    HSSFRow header = sheet.createRow(0); // 第0行
    HSSFCellStyle style = workbook.createCellStyle();
    // 设置样式
    style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    String employeeCode = requestContext.getMessage("employeeCode");
    String employeeName = requestContext.getMessage("employeeName");
    String assessTime = requestContext.getMessage("assessTime");
    String state = requestContext.getMessage("state");
    String employed=requestContext.getMessage("employed");
    String unemployed=requestContext.getMessage("unemployed");
    String Unaudited=requestContext.getMessage("Unaudited");
    // 表头名称数组
    String[] headerArr = new String[] { employeeCode, employeeName,
    assessTime, state };
    // 产生标题列
    HSSFCell cell;
    for (int i = 0; i < headerArr.length; i++) {
    cell = header.createCell((short) i);
    cell.setCellStyle(style);
    cell.setCellValue(headerArr[i]);
    }
    // 迭代数据
    if (list != null && list.size() > 0) {
    int rowNum = 1;
    for (Map history : list) {
    HSSFRow row = sheet.createRow(rowNum++);
    row.createCell((short) 0).setCellValue(
    (String)history.get("employee_code"));

    row.createCell((short) 1).setCellValue(
    (String)history.get("name"));
    row.createCell((short) 2).setCellValue(
    (history.get("stage_time") != null ? history.get("stage_time").toString(): ""));

    if (history.get("state")!=null){
    String ss=history.get("state").toString();
    }
    if (history.get("state")!=null&&history.get("state").toString().equals("1")) {
    row.createCell((short) 3).setCellValue(employed);
    }else if(history.get("state")!=null&&history.get("state").toString().equals("2")) {
    row.createCell((short) 3).setCellValue(unemployed);
    }else{
    row.createCell((short) 3).setCellValue(Unaudited);
    }

    }
    }

    workbook.write(os);
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

  • 相关阅读:
    MySQL的安装问题
    初识二分法
    PK赛 lower_bound( )和upper_bound( )的应用
    记录
    "双指针"去重有序数组
    归并排序(循序渐进中......)
    [2021.4.20打卡]LeetCode781. 森林中的兔子
    杂记...(持续更新)
    [未完待续](c++实现)八数码Ⅱ
    [回忆向]快速排序(降序) 感悟
  • 原文地址:https://www.cnblogs.com/hm1990hpu/p/8193814.html
Copyright © 2011-2022 走看看