1. 下载并导入项目【poi.3.17.jar

  2. strust.xml

    <action name="returnLate_*" class="com.stureturnlate.moudels.biz.action.return_late.ReturnLateAction" method="{1}">
    <result name="list">/page/moudels/biz/return_late/returnLateList.jsp</result>
    <result name="openAdd">/page/moudels/biz/return_late/returnLateAdd.jsp</result>
    <!--<result name="openEdit">/page/biz/student/studentUpdate.jsp</result>-->
    <result name="openDetails">/page/moudels/biz/return_late/returnLateDetails.jsp</result> <!-- 导出Excel -->
    <result name="download" type="stream">
    <param name="contentType">application/vnd.ms-excel</param><!-- 注意这里的ContentType -->
    <param name="contentDisposition">attachment;filename="${fileName}.xls"</param><!-- 下载文件的名字 -->
    <param name="bufferSize"></param><!-- 下载文件的大小 =1M -->
    <param name="inputName">excelFile</param><!-- 这里需要和Action里的变量名一致 -->
    </result>
    </action>
  3. ReturnLateAction.class

        /**
    * 导出
    * @return
    */
    public String download() throws Exception { List<ReturnLateEntity> returnLateEntityList = new ArrayList<>();
    fileName = fileName+ DateTool.dateFormat(new Date());
    fileName=new String(fileName.getBytes("gb2312"), "iso8859-1");//防止中文乱码或不显示
    //TODO 第一步:声明excel的文档对象
    HSSFWorkbook returnLateExcel;
    try {
    //查询的结果,插入Excel填充数据
    String[] ids = returnLateIds.split(",");
    returnLateEntityList = returnLateService.selectAllReturnLate(ids); //TODO 第二步:获取excel的文档对象
    returnLateExcel = CreateExcel.createReturnLateExcel(returnLateEntityList); ByteArrayOutputStream output = new ByteArrayOutputStream();//字节数组输出流 //TODO 第三步:写入字节数组输出流
    returnLateExcel.write(output);
    byte[] ba = output.toByteArray(); //TODO 第四步:为输入流对象赋值
    excelFile = new ByteArrayInputStream(ba); output.flush();
    output.close();//关闭
    } catch (SQLException e) {
    System.out.println("晚归记录文件内容写入或者创建失败失败!");
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } return "download";
    }
  4. 文件导出类CreateExcel.class

    package com.stureturnlate.common;
    
    import com.stureturnlate.moudels.vo.*;
    import org.apache.poi.hssf.usermodel.*;
    import org.apache.poi.ss.usermodel.HorizontalAlignment; import java.sql.SQLException;
    import java.util.List; /**
    * @author soldier
    * @title: CreateExcel
    * @projectName stu_return_late
    * @description: 文件导出Excel
    * @date 19-6-10下午9:56
    */
    public class CreateExcel { public static HSSFWorkbook createReturnLateExcel(List<ReturnLateEntity> returnLateEntityList) throws SQLException {
    // 创建一个Excel文件
    HSSFWorkbook workbook = new HSSFWorkbook();//excel的文档对象
    // 创建一个工作表
    HSSFSheet sheet = workbook.createSheet("学生晚归记录");
    // 添加表头行
    HSSFRow hssfRow = sheet.createRow(0);
    // 设置单元格格式居中
    HSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setAlignment(HorizontalAlignment.CENTER); int index = 0;
    // 添加表头内容
    HSSFCell headCell = hssfRow.createCell(index++);
    headCell.setCellValue("晚归编号");
    headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(index++);
    headCell.setCellValue("晚归学生");
    headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(index++);
    headCell.setCellValue("所属学院");
    headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(index++);
    headCell.setCellValue("学生所在宿舍");
    headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(index++);
    headCell.setCellValue("晚归时间");
    headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(index++);
    headCell.setCellValue("晚归原因");
    headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(index++);
    headCell.setCellValue("记录者工号");
    headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(index);
    headCell.setCellValue("记录者姓名");
    headCell.setCellStyle(cellStyle); // 添加数据内容
    for (int i = 0; i < returnLateEntityList.size(); i++) {
    hssfRow = sheet.createRow((int) i + 1);
    ReturnLateEntity returnLateEntity = returnLateEntityList.get(i); index = 0;
    // 创建单元格,并设置值
    HSSFCell cell = hssfRow.createCell(index++);
    cell.setCellValue(returnLateEntity.getReturnLateId());
    cell.setCellStyle(cellStyle); cell = hssfRow.createCell(index++);
    StudentEntity studentEntity = new StudentEntity();//获取学生名称
    cell.setCellValue(studentEntity.getStudentName());
    cell.setCellStyle(cellStyle); cell = hssfRow.createCell(index++);
    CollegeEntity collegeEntity = new CollegeEntity();//获取学院
    cell.setCellValue(collegeEntity.getCollegeName());
    cell.setCellStyle(cellStyle); cell = hssfRow.createCell(index++);
    HostelEntity hostelEntity = new HostelEntity();//获取宿舍名称
    cell.setCellValue(hostelEntity.getHostelName());
    cell.setCellStyle(cellStyle); cell = hssfRow.createCell(index++);
    cell.setCellValue(DateTool.dateTimeFormat(returnLateEntity.getReturnLateTime()));
    cell.setCellStyle(cellStyle); cell = hssfRow.createCell(index++);
    cell.setCellValue(returnLateEntity.getReturnLateCause());
    cell.setCellStyle(cellStyle); cell = hssfRow.createCell(index++);
    DormMasterEntity dormMasterEntity = new DormMasterEntity();
    cell.setCellValue(dormMasterEntity.getDormMasterId());
    cell.setCellStyle(cellStyle); cell = hssfRow.createCell(index);
    cell.setCellValue(dormMasterEntity.getDormMasterName());
    cell.setCellStyle(cellStyle);
    } try {
    return workbook; } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }
    }

Strust2+POI导出exel表格且解决文件名中文乱码/不显示的更多相关文章

  1. windows10下面部署nginx(解决文件名中文乱码问题)

    由于开发需要,我们总是需要先在windows环境下面部署项目进行测试,通过之后才会移植到linux系统进行测试部署. 本篇文章会介绍一下windows终端下面部署nginx WEB服务的一些步骤流程, ...

  2. 解决swfupload上传控件文件名中文乱码问题 三种方法 flash及最新版本11.8.800.168

    目前比较流行的是使用SWFUpload控件,这个控件的详细介绍可以参见官网http://demo.swfupload.org/v220/index.htm 在使用这个控件批量上传文件时发现中文文件名都 ...

  3. poi导出word表格详解 超详细了

    转:非常感谢原作者 poi导出word表格详解 2018年07月20日 10:41:33 Z丶royAl 阅读数:36138   一.效果如下 二.js代码 function export_word( ...

  4. Maven项目结合POI导出Excl表格Demo-亲测可用

    Maven项目结合POI导出Excl表格 一.POM文件添加依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> ...

  5. PHP Header下载文件在IE文件名中文乱码问题

    解决PHP Header下载文件在IE文件名中文乱码有两种常见的,一种是是把页面编码改成utf8,另一种是对中文url进入urlencode编码,根据UA检测,区别下载,就可以解决了 $filenam ...

  6. Linux->Windows主机目录和文件名中文乱码恢复

    目录 Linux->Windows主机目录和文件名中文乱码恢复 声明 一. 乱码问题 二. 调试环境 三. 目录和文件名乱码恢复 3.1 可选方案 3.1.1 通过合适的编解码转换 3.1.2 ...

  7. 彻底解决matplotlib中文乱码问题(转)

    彻底解决matplotlib中文乱码问题 1.环境查看a.系统版本查看[hadoop@p168 ~]$ cat /etc/redhat-releaseCentOS Linux release 7.2. ...

  8. Centos7 unzip文件名中文乱码

    Centos7 unzip文件名中文乱码 前言 今天在批量处理windos文件时为了方便操作,将windos下面的文件夹打成zip包上传至centos7中解压处理,发现解压后中文文件名变成了乱码,如下 ...

  9. 解决Eclipse中文乱码 - 技术博客 - 51CTO技术博客 http://hsj69106.blog.51cto.com/1017401/595598/

    解决Eclipse中文乱码 - 技术博客 - 51CTO技术博客  http://hsj69106.blog.51cto.com/1017401/595598/

随机推荐

  1. 【JZOJ2156】【2017.7.10普及】复仇者vsX战警之训练

    题目 月球上反凤凰装甲在凤凰之力附身霍普之前,将凤凰之力打成五份,分别附身在X战警五大战力上面辐射眼.白皇后.钢力士.秘客和纳摩上(好尴尬,汗). 在凤凰五使徒的至高的力量的威胁下,复仇者被迫逃到昆仑 ...

  2. Java+超大文件上传

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  3. Python黑科技:FuckIt.py

    说起 Python 强大的地方,你可能想到是它的优雅.简洁.开发速度快,社区活跃度高.但真正使得这门语言经久不衰的一个重要原因是它的无所不能,因为社区有各种各样的第三库,使得我们用Python实现一个 ...

  4. Android图片上传(头像裁切+原图原样)

    下面简单铺一下代码: (一)头像裁切.上传服务器(代码) 这里上边的按钮是头像的点击事件,弹出底部的头像选择框,下边的按钮跳到下个页面,进行原图上传. ? 1 2 3 4 5 6 7 8 9 10 1 ...

  5. android 3.0 ationbar使用总结

    1,ationbar的基本讲解 http://www.apkbus.com/forum.php?mod=viewthread&tid=125536 仅仅需要根据需求写出一个menu资源文件 2 ...

  6. 错误1919,配置ODBC数据源MS Access Database时发生错误ODEC错误

    WIN7 64位旗舰版安装OFFICE2003 提示:“错误1919,配置ODBC数据源MS Access Database时发生错误ODEC错误” 在64位系统上,32位软件的注册表的信息不是直接在 ...

  7. Nodejs搭建音视频通信-信令服务器 总结

    1.安装nodejs  node-v10.16.3-x64.msi 2.安装配置环境变量 这里的环境配置主要配置的是npm安装的全局模块所在的路径,以及缓存cache的路径,之所以要配置,是因为以后在 ...

  8. spring boot: Whitelabel Error Page的解决方案

    http://blog.csdn.net/u014788227/article/details/53670112

  9. C++ STL unordered_map

    容器unordered_map<key type,value tyep>m; 迭代器unordered_map<key type,value tyep>::iterator i ...

  10. 安装 Windows 系统在 NVMe 规范的 M.2 接口的固态硬盘(SSD)上

    作为一个程序员很重要的一项技能就是装系统 @_@,以前我都是随便用网上的工具做个系统盘,每次要用直接随手就搞好了,节省大家时间. 但最近同事装了个贼小的固态,然后我启动盘里的系统果断识别不出来他的固态 ...