-
下载并导入项目【poi.3.17.jar】
-
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">1024</param><!-- 下载文件的大小 1048576=1M --> <param name="inputName">excelFile</param><!-- 这里需要和Action里的变量名一致 --> </result> </action>
-
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"; }
-
文件导出类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表格且解决文件名中文乱码/不显示
猜你喜欢
转载自www.cnblogs.com/HuangJie-sol/p/11001923.html
今日推荐
周排行