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

  2. strust.xml

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

    1. /**
    2. * 导出
    3. * @return
    4. */
    5. public String download() throws Exception {
    6.  
    7. List<ReturnLateEntity> returnLateEntityList = new ArrayList<>();
    8. fileName = fileName+ DateTool.dateFormat(new Date());
    9. fileName=new String(fileName.getBytes("gb2312"), "iso8859-1");//防止中文乱码或不显示
    10. //TODO 第一步:声明excel的文档对象
    11. HSSFWorkbook returnLateExcel;
    12. try {
    13. //查询的结果,插入Excel填充数据
    14. String[] ids = returnLateIds.split(",");
    15. returnLateEntityList = returnLateService.selectAllReturnLate(ids);
    16.  
    17. //TODO 第二步:获取excel的文档对象
    18. returnLateExcel = CreateExcel.createReturnLateExcel(returnLateEntityList);
    19.  
    20. ByteArrayOutputStream output = new ByteArrayOutputStream();//字节数组输出流
    21.  
    22. //TODO 第三步:写入字节数组输出流
    23. returnLateExcel.write(output);
    24. byte[] ba = output.toByteArray();
    25.  
    26. //TODO 第四步:为输入流对象赋值
    27. excelFile = new ByteArrayInputStream(ba);
    28.  
    29. output.flush();
    30. output.close();//关闭
    31. } catch (SQLException e) {
    32. System.out.println("晚归记录文件内容写入或者创建失败失败!");
    33. e.printStackTrace();
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37.  
    38. return "download";
    39. }
  4. 文件导出类CreateExcel.class

    1. package com.stureturnlate.common;
    2.  
    3. import com.stureturnlate.moudels.vo.*;
    4. import org.apache.poi.hssf.usermodel.*;
    5. import org.apache.poi.ss.usermodel.HorizontalAlignment;
    6.  
    7. import java.sql.SQLException;
    8. import java.util.List;
    9.  
    10. /**
    11. * @author soldier
    12. * @title: CreateExcel
    13. * @projectName stu_return_late
    14. * @description: 文件导出Excel
    15. * @date 19-6-10下午9:56
    16. */
    17. public class CreateExcel {
    18.  
    19. public static HSSFWorkbook createReturnLateExcel(List<ReturnLateEntity> returnLateEntityList) throws SQLException {
    20. // 创建一个Excel文件
    21. HSSFWorkbook workbook = new HSSFWorkbook();//excel的文档对象
    22. // 创建一个工作表
    23. HSSFSheet sheet = workbook.createSheet("学生晚归记录");
    24. // 添加表头行
    25. HSSFRow hssfRow = sheet.createRow(0);
    26. // 设置单元格格式居中
    27. HSSFCellStyle cellStyle = workbook.createCellStyle();
    28. cellStyle.setAlignment(HorizontalAlignment.CENTER);
    29.  
    30. int index = 0;
    31. // 添加表头内容
    32. HSSFCell headCell = hssfRow.createCell(index++);
    33. headCell.setCellValue("晚归编号");
    34. headCell.setCellStyle(cellStyle);
    35.  
    36. headCell = hssfRow.createCell(index++);
    37. headCell.setCellValue("晚归学生");
    38. headCell.setCellStyle(cellStyle);
    39.  
    40. headCell = hssfRow.createCell(index++);
    41. headCell.setCellValue("所属学院");
    42. headCell.setCellStyle(cellStyle);
    43.  
    44. headCell = hssfRow.createCell(index++);
    45. headCell.setCellValue("学生所在宿舍");
    46. headCell.setCellStyle(cellStyle);
    47.  
    48. headCell = hssfRow.createCell(index++);
    49. headCell.setCellValue("晚归时间");
    50. headCell.setCellStyle(cellStyle);
    51.  
    52. headCell = hssfRow.createCell(index++);
    53. headCell.setCellValue("晚归原因");
    54. headCell.setCellStyle(cellStyle);
    55.  
    56. headCell = hssfRow.createCell(index++);
    57. headCell.setCellValue("记录者工号");
    58. headCell.setCellStyle(cellStyle);
    59.  
    60. headCell = hssfRow.createCell(index);
    61. headCell.setCellValue("记录者姓名");
    62. headCell.setCellStyle(cellStyle);
    63.  
    64. // 添加数据内容
    65. for (int i = 0; i < returnLateEntityList.size(); i++) {
    66. hssfRow = sheet.createRow((int) i + 1);
    67. ReturnLateEntity returnLateEntity = returnLateEntityList.get(i);
    68.  
    69. index = 0;
    70. // 创建单元格,并设置值
    71. HSSFCell cell = hssfRow.createCell(index++);
    72. cell.setCellValue(returnLateEntity.getReturnLateId());
    73. cell.setCellStyle(cellStyle);
    74.  
    75. cell = hssfRow.createCell(index++);
    76. StudentEntity studentEntity = new StudentEntity();//获取学生名称
    77. cell.setCellValue(studentEntity.getStudentName());
    78. cell.setCellStyle(cellStyle);
    79.  
    80. cell = hssfRow.createCell(index++);
    81. CollegeEntity collegeEntity = new CollegeEntity();//获取学院
    82. cell.setCellValue(collegeEntity.getCollegeName());
    83. cell.setCellStyle(cellStyle);
    84.  
    85. cell = hssfRow.createCell(index++);
    86. HostelEntity hostelEntity = new HostelEntity();//获取宿舍名称
    87. cell.setCellValue(hostelEntity.getHostelName());
    88. cell.setCellStyle(cellStyle);
    89.  
    90. cell = hssfRow.createCell(index++);
    91. cell.setCellValue(DateTool.dateTimeFormat(returnLateEntity.getReturnLateTime()));
    92. cell.setCellStyle(cellStyle);
    93.  
    94. cell = hssfRow.createCell(index++);
    95. cell.setCellValue(returnLateEntity.getReturnLateCause());
    96. cell.setCellStyle(cellStyle);
    97.  
    98. cell = hssfRow.createCell(index++);
    99. DormMasterEntity dormMasterEntity = new DormMasterEntity();
    100. cell.setCellValue(dormMasterEntity.getDormMasterId());
    101. cell.setCellStyle(cellStyle);
    102.  
    103. cell = hssfRow.createCell(index);
    104. cell.setCellValue(dormMasterEntity.getDormMasterName());
    105. cell.setCellStyle(cellStyle);
    106. }
    107.  
    108. try {
    109. return workbook;
    110.  
    111. } catch (Exception e) {
    112. e.printStackTrace();
    113. }
    114. return null;
    115. }
    116. }

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. C#任务调度——LimitedConcurrencyLevelTaskScheduler

    这是参考大佬分享的代码写的有问题请提出指正,谢谢. using Serilog; using System; using System.Collections.Generic; using Syste ...

  2. 12. ClustrixDB 为容错和可用性分配磁盘空间

    集群必须包含足够的空闲磁盘空间,以便从节点或区域故障中自动恢复.要计算在发生故障后仍然允许ClustrixDB完全重新保护数据的情况下可以使用的最大磁盘空间量,可以使用以下公式: 最大磁盘利用率% = ...

  3. 面向对象this关键字的内存图解

    this:哪个对象调用方法,this就代表哪个对象 案例1: //定义老师类 class Teacher { private String name; private int age; public ...

  4. 【gym102394B】Binary Numbers(DP)

    题意:From https://blog.csdn.net/m0_37809890/article/details/102886956 思路: 可以发现转移就是右上角的一个区间前缀和 std只要开1倍 ...

  5. Json和XML的一些差别

    XML: 扩展标记语言,可以用来标记数据.定义数据类型, 优缺点: 1.格式统一,符合标准: 2.容易与其他系统进行远程交互,数据共享比较方便 3.XML文件庞大,文件格式复杂,传输占带宽,较复杂 J ...

  6. Jprofiler远程监控JVM

    一.下载并安装 本地和远程服务器分别安装Jprofiler,下载地址 二.Windows远程连接JVM配置 1.打开Windows客户端Jprofiler 2.点Cancel 3.创建远程会话 4.添 ...

  7. Comparable接口与Comparator接口的比较————Comparable接口详解

    Comparable接口位于:java.lang包中. Comparable接口: 1. 实现了这个接口的类,会被强制进行自然排序. 问题又来了:那什么是自然排序呢? 自然排序:就是字典序排序,不分大 ...

  8. tp32-layuicms项目介绍

    项目结构:  项目截图: 登录页 文章列表 码云仓库:https://gitee.com/lim2018/tp32-layuicms

  9. tihuantupian

  10. iOS SDK开发之 .framework静态库

    查看.a静态库的生成及使用单击此处 注:这篇教程将只使用一小部分Objective-C代码,本文主要讲解从开始到应用的详细步骤.环境:xcode 9.2下面我们开始操作: 第一步:创建一个静态库工程 ...