标签:cell cellStyle index exel hssfRow Strust2 headCell 乱码 createCell
-
下载并导入项目【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; } }
标签:cell,cellStyle,index,exel,hssfRow,Strust2,headCell,乱码,createCell 来源: https://www.cnblogs.com/HuangJie-sol/p/11001923.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。