java实现Excel数据导出

private void buildExcelData(HttpServletResponse response,List<ScheduleInfo> todoList,String title) {
			ByteArrayOutputStream byteArrayOutputStream = null;
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try{
				HSSFWorkbook book = new HSSFWorkbook();
	            HSSFSheet sheet = book.createSheet();
	            //创建样式
	            HSSFCellStyle style1 = HSSFUtil.getStyle(book, HSSFCellStyle.ALIGN_CENTER,1);
	            HSSFCellStyle style2 = HSSFUtil.getStyle(book, HSSFCellStyle.ALIGN_CENTER,2);
	            HSSFCellStyle style3 = HSSFUtil.getStyle(book, HSSFCellStyle.ALIGN_CENTER,3);
				
	            int rows = 0;
	            int cells = 0;
	            HSSFRow row = sheet.createRow(rows);
	            row.setHeight((short)800);
	            HSSFCell cell = row.createCell(cells);
	            cell.setCellValue(title);
	            cell.setCellStyle(style1);
	            sheet.setColumnWidth(cells, (15*256));
	            cells++;
	            cell = row.createCell(cells);
	            sheet.setColumnWidth(cells, (30*256));
	            cells++;
	            cell = row.createCell(cells);
	            sheet.setColumnWidth(cells, (50*256));
	            
	            cells++;
	            cell = row.createCell(cells);
	            sheet.setColumnWidth(cells, (25*256));
	            cells++;
	            cell = row.createCell(cells);
	            sheet.setColumnWidth(cells, (25*256));
	            cells++;
	            cell = row.createCell(cells);
	            sheet.setColumnWidth(cells, (25*256));
	            /*cells++;
	            cell = row.createCell(cells);
	            sheet.setColumnWidth(cells, (25*256));
	            cells++;
	            cell = row.createCell(cells);
	            sheet.setColumnWidth(cells, (25*256));*/
	               
	            
	            cells++;
	            cell = row.createCell(cells);
	            sheet.setColumnWidth(cells, (25*256));
	            cells++;
	            cell = row.createCell(cells);
	            sheet.setColumnWidth(cells, (25*256));
	            cells++;
	            cell = row.createCell(cells);
	            sheet.setColumnWidth(cells, (35*256));
	            cells++;
	            cell = row.createCell(cells);
	            sheet.setColumnWidth(cells, (25*256));
	            rows++;
	            sheet.addMergedRegion(new CellRangeAddress(0,0,0,cells-2));
	            
	            cells = 0;
	            row = sheet.createRow(rows);
	            row.setHeight((short)500);
	            cell = row.createCell(cells);
	            cell.setCellValue("序号");
	            cell.setCellStyle(style3);
	            cells++;
	            cell = row.createCell(cells);
	            cell.setCellValue("姓名");
	            cell.setCellStyle(style3);
	            cells++;
	            cell = row.createCell(cells);
	            cell.setCellValue("日程主题");
	            cell.setCellStyle(style3);
	            
	            cells++;
	            cell = row.createCell(cells);
	            cell.setCellValue("日程内容");
	            cell.setCellStyle(style3);
	            cells++;
	            cell = row.createCell(cells);
	            cell.setCellValue("开始时间");
	            cell.setCellStyle(style3);
	            cells++;
	            cell = row.createCell(cells);
	            cell.setCellValue("结束时间");
	            cell.setCellStyle(style3);
	            cells++;
	            cell = row.createCell(cells);
	            cell.setCellValue("创建人");
	            cell.setCellStyle(style3);
	            cells++;
	            cell = row.createCell(cells);
	            cell.setCellValue("创建时间");
	            cell.setCellStyle(style3);
	           
	            /*cells++;
	            cell = row.createCell(cells);
	            cell.setCellValue("办理时间(开始/结束)");
	            cell.setCellStyle(style3);
	            cells++;
	            cell = row.createCell(cells);
	            cell.setCellValue("办文状态");
	            cell.setCellStyle(style3);*/
	            rows++;
	            
	            for (int i = 0; i < todoList.size(); i++) {
	            	
	            	ScheduleInfo todo = (ScheduleInfo) todoList.get(i);
	            	row = sheet.createRow(rows);
	            	row.setHeight((short)600);
	            	cells = 0;
	            	cell = row.createCell(cells);
	               	cell.setCellValue(String.valueOf(i+1));//序号
	                cell.setCellStyle(style2); 
	                cells++;
	                cell = row.createCell(cells);
	               //	cell.setCellValue(todo.getFormName());//姓名
	                cell.setCellValue(todo.getPerson());
	                cell.setCellStyle(style2); 
	                cells++;
	                cell = row.createCell(cells);
	                cell.setCellValue(todo.getTheme());//日程主题
	                cell.setCellStyle(style2);
	                
	                cells++;
	                cell = row.createCell(cells);
	                cell.setCellValue(todo.getContent());
	                cell.setCellStyle(style2);
	                cells++;
	                cell = row.createCell(cells);
	                    cell.setCellValue(sdf.format(todo.getStartime()));//==null?"":sdf.format(todo.getStartime())
	                    //cell.setCellValue("");
	                    cell.setCellStyle(style2);
	                    cells++;
	                    cell = row.createCell(cells);
	                    cell.setCellValue(sdf.format(todo.getEndtime()));
	                    cell.setCellStyle(style2);
	                    cells++;
	                    cell = row.createCell(cells);
	                    cell.setCellValue(todo.getCreateperson());
	                    cell.setCellStyle(style2);
	                    cells++;
	                    cell = row.createCell(cells);
	                    cell.setCellValue(sdf.format(todo.getCreatetime()));
	                    cell.setCellStyle(style2);
	                  
	               
	                
	                rows++;
	            }
	            
	            //导出excel名字
	            String excelName = "日程列表.xls";
	            byteArrayOutputStream = new ByteArrayOutputStream();
	            book.write(byteArrayOutputStream);
	            DownloadResponse downloadResponse = new DownloadResponse(response);
	            downloadResponse.downloadBytes(byteArrayOutputStream.toByteArray(), StringUtil.toUtf8String(excelName));
	            
			}catch(Exception e){
				LogUtil.errorLog(this.getClass(), "日程列表导出Excel出错", e);
			}finally{
				if(byteArrayOutputStream != null){
					try{
						byteArrayOutputStream.close();
					}catch(IOException e){
						e.printStackTrace();
					}
				}
			}
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值