您的位置:首页 > 其它

poi导出exce表实例

2017-12-07 12:54 176 查看
/**
* 导出签到人员列表
* @param requestMsg
*/
@Override
public String getMeetingVotedExport( MeetingSignDTO meetingSignDTO, RequestMsg requestMsg ) {

if ( null == meetingSignDTO ) {
throw new DctException( ErrorCodeCore.DATA_NOT_NULL );//抛出自定义异常
}
Integer meetingId = meetingSignDTO.getMeetingId();
if ( null == meetingId ) {
throw new DctException( ErrorCodeCore.DATA_NOT_NULL );//抛出自定义异常
}
MeetingSignExample example = new MeetingSignExample(); MeetingSignExample.Criteria criteria = example.createCriteria(); example.setOrderByClause( "sign_in_type,sign_at,create_at" ); criteria.andFstatusEqualTo( MeetingSignFstatus.VOTED ); criteria.andMeetingIdEqualTo( meetingId ); String memberName = meetingSignDTO.getMemberName(); if ( !StringUtil.isEmpty( memberName ) ) { criteria.andMemberNameLike( SQLUtil.toLink( memberName ) );//模糊条件 } String mobile = meetingSignDTO.getMobile(); if ( !StringUtil.isEmpty( mobile ) ) { criteria.andMobileLike( SQLUtil.toLink( mobile ) );//模糊条件 } Short sex = meetingSignDTO.getSex(); if ( null != sex ) { criteria.andSexEqualTo( sex ); } String org = meetingSignDTO.getOrg(); if ( !StringUtil.isEmpty( org ) ) { criteria.andOrgLike( org ); } Date startAt = meetingSignDTO.getStartAt(); if ( null != startAt ) { criteria.andCreateAtGreaterThan( startAt ); } Date endAt = meetingSignDTO.getEndAt(); if ( null != endAt ) { criteria.andCreateAtLessThan( endAt ); } Short signInType = meetingSignDTO.getSignInType(); if ( null != signInType ) { criteria.andSignInTypeEqualTo( signInType ); } List<MeetingSign> meetingSignList = meetingSignMapper.selectByExample( example ); MeetingCore meetingCore = meetingCoreMapper.selectByPrimaryKey( meetingId ); String consolePerson = meetingCore.getContactPersons(); if ( null == consolePerson ) { consolePerson = "";}//会议的详情

 String meetingInfo = "会议名称:" + meetingCore.getFname() + "\r\n" + "会议时间:" + formatTime( meetingCore.getStartAt() ) + " 至 " + formatTime( meetingCore.getEndAt() ) + "\r\n" + "会议地点:" + meetingCore.getPlace() + "\r\n" + "联系人:" + consolePerson + "\r\n" + "报名人数:" + ( meetingCore.getAttendPersonNum() - meetingCore.getLeftAttendPersonNum() ); //创建workbook HSSFWorkbook workbook = new HSSFWorkbook(); HSSFCellStyle bodyStyle = workbook.createCellStyle(); bodyStyle.setWrapText(true);//设置自动换行 //创建sheet页 HSSFSheet sheet = workbook.createSheet( "报名信息表" );//设置表名 //创建单元格 HSSFRow row = sheet.createRow( 0 );//第一行 HSSFCell c00 = row.createCell( 0 );//第一列 c00.setCellValue( new HSSFRichTextString( meetingCore.getFname() ) );//设置第一列里面的内容 HSSFRow row1 = sheet.createRow( 1 );//第二行 row1.setHeightInPoints(70);//设置行高 HSSFCell c10 = row1.createCell( 0 );//第一列 c10.setCellStyle( bodyStyle );//设置格式 c10.setCellValue( new HSSFRichTextString( meetingInfo ) ); HSSFRow row2 = sheet.createRow( 2 );//第三行 HSSFCell c20 = row2.createCell( 0 );//第一列 c20.setCellValue( new HSSFRichTextString( "序号" ) );//设置第一列内容 HSSFCell c21 = row2.createCell( 1 ); c21.setCellValue( new HSSFRichTextString( "单位" ) ); HSSFCell c22 = row2.createCell( 2 ); c22.setCellValue( new HSSFRichTextString( <
e6bc
span style="color:#6a8759;">"部门" ) ); HSSFCell c23 = row2.createCell( 3 ); c23.setCellValue( new HSSFRichTextString( "姓名" ) ); HSSFCell c24 = row2.createCell( 4 ); c24.setCellValue( new HSSFRichTextString( "性别" ) ); HSSFCell c25 = row2.createCell( 5 ); c25.setCellValue( new HSSFRichTextString( "职务/职称" ) ); HSSFCell c26 = row2.createCell( 6 ); c26.setCellValue( new HSSFRichTextString( "手机号" ) ); HSSFCell c27 = row2.createCell( 7 ); c27.setCellValue( new HSSFRichTextString( "办公固话" ) ); HSSFCell c28 = row2.createCell( 8 ); c28.setCellValue( new HSSFRichTextString( "邮箱" ) ); HSSFCell c29 = row2.createCell( 9 ); c29.setCellValue( new HSSFRichTextString( "住宿" ) ); HSSFCell c210 = row2.createCell( 10 ); c210.setCellValue( new HSSFRichTextString( "到会时间" ) ); HSSFCell c211 = row2.createCell( 11 ); c211.setCellValue( new HSSFRichTextString( "离会时间" ) ); HSSFCell c212 = row2.createCell( 12 ); c212.setCellValue( new HSSFRichTextString( "签到状态" ) ); HSSFCell c213 = row2.createCell( 13 ); c213.setCellValue( new HSSFRichTextString( "签到时间" ) ); HSSFCell c214 = row2.createCell( 14 ); c214.setCellValue( new HSSFRichTextString( "签到地点" ) ); sheet.addMergedRegion( new CellRangeAddress( 0, 0, 0, 14 ) );//合并单元格从第0行到第0行从第0列到第14列 sheet.addMergedRegion( new CellRangeAddress( 1, 1, 0, 14 ) ); SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
//循环填充数据
 for ( int j = 0; j < meetingSignList.size(); j++ ) { MeetingSign ret = meetingSignList.get( j ); row = sheet.createRow( j + 3 ); for ( int i = 0; i < 15; i++ ) { HSSFCell cell = row.createCell(i); if ( i == 0 ) { // 序号 cell.setCellValue( new HSSFRichTextString( ( j + 1 )+"" ) ); } else if ( i == 1 ) { // 单位 cell.setCellValue( new HSSFRichTextString( ret.getOrg() ) ); } else if ( i == 2 ) { // 部门 cell.setCellValue( new HSSFRichTextString( ret.getPub() ) ); } else if ( i == 3 ) { // 姓名 cell.setCellValue( new HSSFRichTextString( ret.getMemberName() ) ); } else if ( i == 4 ) { // 性别 String sexStr = "保密"; if ( ret.getSex().intValue() == 1 ) { sexStr = "男"; } else if ( ret.getSex().intValue() == 2 ) { sexStr = "女"; } cell.setCellValue( new HSSFRichTextString( sexStr ) ); } else if ( i == 5 ) { // 职务/职称 cell.setCellValue( new HSSFRichTextString( ret.getPosition() ) ); } else if ( i == 6 ) { // 手机号 cell.setCellValue( new HSSFRichTextString( ret.getMobile() ) ); } else if ( i == 7 ) { // 办公固话 cell.setCellValue( new HSSFRichTextString( ret.getOfficePhone() ) ); }else if ( i == 8 ) { // 邮箱 cell.setCellValue( new HSSFRichTextString( ret.getEmail() ) ); } else if ( i == 9 ) { // 住宿 cell.setCellValue( new HSSFRichTextString( ret.getPutUpType() ) ); } else if ( i == 10 ) { // 到会时间 cell.setCellValue( new HSSFRichTextString( ret.getGoAt() ) ); } else if ( i == 11 ) { // 离会时间 cell.setCellValue( new HSSFRichTextString( ret.getLeaveAt() ) ); } else if ( i == 12 ) { // 签到状态 String signTypeStr = null; if ( ret.getSignInType().equals( MeetingSignType.WAIT_SIGN_IN ) ) { signTypeStr = "未签到"; } else if ( ret.getSignInType().equals( MeetingSignType.SELF_SIGN_IN ) ) { signTypeStr = "扫码签到"; } else if ( ret.getSignInType().equals( MeetingSignType.MANAGER_SIGN_IN ) ) { signTypeStr = "管理员代签"; } cell.setCellValue( new HSSFRichTextString( signTypeStr ) ); } else if ( i == 13 ) { // 签到时间 if ( ret.getSignAt() != null ) { cell.setCellValue(new HSSFRichTextString(format.format(ret.getSignAt()))); } } else if ( i == 14 ) { // 签到Ip String signInLocation = ret.getSignInLocation(); if ( !StringUtil.isEmpty( signInLocation ) ) { cell.setCellValue(new HSSFRichTextString( ret.getSignInLocation())); } } } } FileOutputStream stream = null; String loadUrl = yjhConfig.getStaticUrl() + "sign" + "/";//下载路径前缀 try { String fileName = RandomUtil.generateString( 15, RandomUtil.ALL_CHAR );//随机生成文件名 stream = new FileOutputStream( yjhConfig.getUploadPath() + "sign" + "/" + fileName + ".xls" );//写入路径 workbook.write(stream);//进行数据写入 stream.close();//关闭流 loadUrl += ( fileName + ".xls" );//文件下载路径 } catch( Exception e ) { logger.error( "文件上传出错", e ); } return loadUrl;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poi excel