java数据导出Excel 详细代码

直接上代码

public void bugExport(PageSearQueryVo vo, HttpServletResponse response,HttpServletRequest request) {
		//获取导出的全部数据
		List<BugListExport> bugExportList = bugListService.selectBugExportList(vo);
		//创建文档对象
		HSSFWorkbook wb = new HSSFWorkbook();
		//创建表单
		HSSFSheet sheet=wb.createSheet("1");
		
		CreationHelper createHelper = wb.getCreationHelper();
		//创建行
		HSSFRow row1=sheet.createRow(0);
		// 表头样式
		CellStyle headerStyle = wb.createCellStyle();
		//创建单元格样式 自动换行
		HSSFCellStyle cellStyle=wb.createCellStyle(); 
		Font headerFont2 = wb.createFont();
		headerFont2.setFontHeightInPoints((short) 10);
		//headerFont2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		cellStyle.setFont(headerFont2);
		cellStyle.setWrapText(true);   
		//水平居中
		headerStyle.setAlignment(CellStyle.ALIGN_CENTER);
		//垂直居中
		headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		//设置边框
		headerStyle.setBorderTop(CellStyle.BORDER_THIN);
		headerStyle.setBorderRight(CellStyle.BORDER_THIN);
		headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
		headerStyle.setBorderLeft(CellStyle.BORDER_THIN);
		
		//设置颜色
		headerStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
		headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		
		
		Font headerFont = wb.createFont();
		headerFont.setFontHeightInPoints((short) 8);
		headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		headerStyle.setFont(headerFont);
		
		//超链接字体颜色
		CellStyle hlink_style = wb.createCellStyle(); 
		Font hlink_font = wb.createFont();  
		hlink_font.setUnderline(Font.U_SINGLE);  
		hlink_font.setColor(IndexedColors.BLUE.getIndex());  
		hlink_style.setFont(hlink_font); 
	 
        row1.createCell(0).setCellValue("BUG创建人");
	    row1.getCell(0).setCellStyle(headerStyle);
	    row1.createCell(1).setCellValue("项目名称");
	    row1.getCell(1).setCellStyle(headerStyle);
	    row1.createCell(2).setCellValue("所属模块");
	    row1.getCell(2).setCellStyle(headerStyle);
	    row1.createCell(3).setCellValue("版本信息");
	    row1.getCell(3).setCellStyle(headerStyle);
	    row1.createCell(4).setCellValue("结束时间");
	    row1.getCell(4).setCellStyle(headerStyle);
	    row1.createCell(5).setCellValue("严重程度");
	    row1.getCell(5).setCellStyle(headerStyle);
	    row1.createCell(6).setCellValue("优先级");
	    row1.getCell(6).setCellStyle(headerStyle);
	    row1.createCell(7).setCellValue("BUG类型");
	    row1.getCell(7).setCellStyle(headerStyle);
	    row1.createCell(8).setCellValue("操作系统");
	    row1.getCell(8).setCellStyle(headerStyle);
	    row1.createCell(9).setCellValue("浏览器类型");
	    row1.getCell(9).setCellStyle(headerStyle);
	    row1.createCell(10).setCellValue("完成状态");
	    row1.getCell(10).setCellStyle(headerStyle);
	    row1.createCell(11).setCellValue("BUG标题");
	    row1.getCell(11).setCellStyle(headerStyle);
	    row1.createCell(12).setCellValue("BUG备注");
	    row1.getCell(12).setCellStyle(headerStyle);
	    row1.createCell(13).setCellValue("创建时间");
	    row1.getCell(13).setCellStyle(headerStyle);
	    row1.createCell(14).setCellValue("附件");
	    row1.getCell(14).setCellStyle(headerStyle);
	    //设置单元格内容
	    
   	 //单元格自适应
	    sheet.autoSizeColumn(0);
       sheet.setColumnWidth(0,sheet.getColumnWidth(0)*15/10);
       sheet.autoSizeColumn(1);
       sheet.setColumnWidth(1,sheet.getColumnWidth(1)*30/10);
       sheet.autoSizeColumn(2);
       sheet.setColumnWidth(2,sheet.getColumnWidth(2)*15/10);
       sheet.autoSizeColumn(3);
       sheet.setColumnWidth(3,sheet.getColumnWidth(3)*35/10);
       sheet.autoSizeColumn(4);
       sheet.setColumnWidth(4,sheet.getColumnWidth(4)*40/10);  
       sheet.autoSizeColumn(5);
       sheet.setColumnWidth(5,sheet.getColumnWidth(5)*15/10);
       sheet.autoSizeColumn(6);
       sheet.setColumnWidth(6,sheet.getColumnWidth(6)*15/10);
       sheet.autoSizeColumn(7);
       sheet.setColumnWidth(7,sheet.getColumnWidth(7)*15/10);
       sheet.autoSizeColumn(8);
       sheet.setColumnWidth(8,sheet.getColumnWidth(8)*25/10);
       sheet.autoSizeColumn(9);
       sheet.setColumnWidth(9,sheet.getColumnWidth(9)*15/10);
       sheet.autoSizeColumn(10);
       sheet.setColumnWidth(10,sheet.getColumnWidth(10)*15/10);
       sheet.autoSizeColumn(11);
       sheet.setColumnWidth(11,sheet.getColumnWidth(11)*40/10);
       sheet.autoSizeColumn(12);
       sheet.setColumnWidth(12,sheet.getColumnWidth(12)*100/10);
       sheet.autoSizeColumn(13);
       sheet.setColumnWidth(13,sheet.getColumnWidth(13)*40/10);
       sheet.autoSizeColumn(14);
       sheet.setColumnWidth(14,sheet.getColumnWidth(14)*60/10);
	    for(int i = 0; i<bugExportList.size();i++){
	        //创建行
	        HSSFRow row=sheet.createRow(i+1);
		    row.createCell(0).setCellValue(bugExportList.get(i).getCreateName());
		    row.getCell(0).setCellStyle(cellStyle);
		    row.createCell(1).setCellValue(bugExportList.get(i).getProjectName());
		    row.getCell(1).setCellStyle(cellStyle);
		    row.createCell(2).setCellValue(bugExportList.get(i).getModuleName());
		    row.getCell(2).setCellStyle(cellStyle);
		    row.createCell(3).setCellValue(bugExportList.get(i).getVersionName());
		    row.getCell(3).setCellStyle(cellStyle);
		    row.createCell(4).setCellValue(bugExportList.get(i).getEndTime());
		    row.getCell(4).setCellStyle(cellStyle);
		    row.createCell(5).setCellValue(bugExportList.get(i).getSeverityName());
		    row.getCell(5).setCellStyle(cellStyle);
		    row.createCell(6).setCellValue(bugExportList.get(i).getPrioityName());
		    row.getCell(6).setCellStyle(cellStyle);
		    row.createCell(7).setCellValue(bugExportList.get(i).getStutusName());
		    row.getCell(7).setCellStyle(cellStyle);
		    row.createCell(8).setCellValue(bugExportList.get(i).getOperatingName());
		    row.getCell(8).setCellStyle(cellStyle);
		    row.createCell(9).setCellValue(bugExportList.get(i).getBrowserName());
		    row.getCell(9).setCellStyle(cellStyle);
		    row.createCell(10).setCellValue(bugExportList.get(i).getStutusName());
		    row.getCell(10).setCellStyle(cellStyle);
		    row.createCell(11).setCellValue(bugExportList.get(i).getBugTitlel());
		    row.getCell(11).setCellStyle(cellStyle);
		    row.createCell(12).setCellValue(bugExportList.get(i).getBugRemark());
		    row.getCell(12).setCellStyle(cellStyle);
		    row.createCell(13).setCellValue(bugExportList.get(i).getCreateTime());
		    row.getCell(13).setCellStyle(cellStyle);
		    
		    row.createCell(14).setCellValue("附件<点击查看详情>");
		    Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL); 
		    //获取Ip
            String ip = ApplicationUtil.ipAdress();
            //获取端口号
		    String port = String.valueOf(request.getServerPort());
		    String id = "details";
		    String adress ="http://"+ip+":"+port+"/OA/bug/selectById?bugId="+bugExportList.get(i).getBugId()+"&Id="+id+"&types=1";
		    link.setAddress(adress);
		    row.getCell(14).setHyperlink(link);
		    row.getCell(14).setCellStyle(hlink_style);
		    
	    }
		    String fileName=FileUtil.excelExportName("testCase");
		    //输出Excel文件
		    OutputStream output;
			try {
				output = response.getOutputStream();
				response.reset();
				response.setHeader("Content-disposition", "attachment; filename="+fileName);
				response.setContentType("application/msexcel");        
				wb.write(output);
			    output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}

猜你喜欢

转载自blog.csdn.net/qq_38092788/article/details/81505192
今日推荐