使用apache 提供的jxl.jar 导出excel

public String exportExcel() { try { /** * 设置标题 */ List<String> headerList = new ArrayList<String>(); headerList.add("序号"); headerList.add("专业"); headerList.add("题目"); headerList.add("姓名"); headerList.add("状态"); /** * 得到要导出的数据集 */ List<Document> dataList = managerCentreService.exportAnsweringDocList(getMajor(),getStatus()); HttpServletResponse response = getResponse(); OutputStream out = response.getOutputStream(); response.setHeader("Content-disposition","attachment; filename=currentDocs.xls"); response.setContentType("application/msexcel;charset=UTF-8"); putDataOnOutputStream(out,dataList,headerList); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } return null; } private void putDataOnOutputStream(OutputStream os,List dataList,List headerList) { try { // 创建工作簿实例 WritableWorkbook workbook = Workbook.createWorkbook(os); // 创建工作表实例 WritableSheet sheet = workbook.createSheet("当前论文列表",0); if (headerList != null && headerList.size() > 0) { for(int i=0; i<headerList.size();i++) { String header = (String)headerList.get(i); // 创建第一行标题 Label label = new Label(i, 0, header!=null?header:""); sheet.addCell(label); } } if(dataList != null && dataList.size()>0) { //给excel填充数据 for(int j=0; j<dataList.size();j++) { Document data = (Document)dataList.get(j); Student stu = CommonFunctions.getStudentMajorByPersonId(data.getCreator().getPerson().getId()); Number id = new Number(0, j+1, data.getId()); sheet.addCell(id); Label major = new Label(1, j+1, stu.getMajor()!=null?stu.getMajor().getCodeDesc():""); sheet.addCell(major); Label title = new Label(2, j+1, data.getTitle()!=null?data.getTitle():""); sheet.addCell(title); Label name = new Label(3, j+1, stu.getName()!=null?stu.getName():""); sheet.addCell(name); Label status = new Label(4, j+1, data.getStatus()!=null?data.getStatus():""); sheet.addCell(status); } } workbook.write(); workbook.close(); } catch(Exception e) { e.printStackTrace(); } } 

你可能感兴趣的:(apache,exception,String,header,Excel,null)