JAVA 의 EXCEL 내 보 내기

4962 단어 자바Excel

   서버 가 Liux 를 사용 할 때 임시 파일 디 렉 터 리 권한 은 jboss 사용 자 를 시작 하 는 권한 과 같 아야 합 니 다.그렇지 않 으 면 데이터 가 불완전 하거나 가 져 온 엑셀 이 절반 이 되면 가 져 올 수 없습니다.
 
	
	
	public String exportExcel() throws Exception{

		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);

		OutputStream os = null;
		String filename = "  .xls"; //      encode  
		
		
		try {			
			response.reset();//      
			response.setContentType("application/vnd.ms-excel");//       
			response.setHeader("Content-Disposition", "filename=" + filename + "");//        
			os = response.getOutputStream();//      
 
		
			long t0 = System.currentTimeMillis();
			List<User> list = new ArrayList();
			expordUtil(list, os);
			long t1 = System.currentTimeMillis();
 
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (os != null) {
				os.flush();
				os.close();
			}
		}

		return null;
	}

 

 	 //          list      
	public String expordUtil(List  list,OutputStream os) throws Exception {
  
		
		WorkbookSettings wbSetting = new WorkbookSettings();  
        wbSetting.setUseTemporaryFileDuringWrite(true);  //          
        wbSetting.setTemporaryFileDuringWriteDirectory(new File("\\excel"));//
        os.flush();
        
		WritableWorkbook wbook = Workbook.createWorkbook(os);//   Excel  
		WritableSheet sheet = wbook.createSheet("    ", 0);//     sheet    
		WritableFont wfont;
		WritableCellFormat wcformat; //      excel
		Label wlabel;

		//      start
		wfont = new WritableFont(WritableFont.createFont("  "), 11,
				WritableFont.BOLD, false,
				jxl.format.UnderlineStyle.NO_UNDERLINE,
				jxl.format.Colour.DARK_GREEN); //        

		//      excel  
		wcformat = new jxl.write.WritableCellFormat(wfont);
		wcformat.setBackground(Colour.AQUA);
		wcformat.setLocked(true);
		wcformat.setAlignment(Alignment.CENTRE);
		wcformat.setBorder(Border.ALL,jxl.format.BorderLineStyle.THIN);
		//        title
		wlabel = new jxl.write.Label(0, 0, "  ", wcformat);
		sheet.addCell(wlabel);
		wlabel = new jxl.write.Label(1, 0, "  ", wcformat);
		sheet.addCell(wlabel);
		wlabel = new jxl.write.Label(2, 0, "  ", wcformat);
		sheet.addCell(wlabel);
		wlabel = new jxl.write.Label(3, 0, "  ", wcformat);
		sheet.addCell(wlabel);
		wlabel = new jxl.write.Label(4, 0, "  ", wcformat);
		sheet.addCell(wlabel);
		
 
		//      end

		//           start

		wfont = new jxl.write.WritableFont(WritableFont.createFont("  "), 10,
				WritableFont.NO_BOLD, false,
				jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); //        
		wcformat = new jxl.write.WritableCellFormat(wfont);
		wcformat.setBackground(Colour.WHITE);
		wcformat.setBorder(Border.ALL,jxl.format.BorderLineStyle.THIN);
		wcformat.setLocked(false);
		wcformat.setAlignment(Alignment.LEFT);
		
		//         
		wfont = new WritableFont(WritableFont.createFont("  "), 11,
				WritableFont.BOLD, false,
				jxl.format.UnderlineStyle.NO_UNDERLINE,
				jxl.format.Colour.DARK_GREEN); //           11      
		
		WritableCellFormat wcformat1 = new jxl.write.WritableCellFormat(wfont);
		wcformat1.setBackground(Colour.GRAY_25);
		wcformat1.setLocked(false);
		wcformat1.setAlignment(Alignment.CENTRE);
		wcformat1.setBorder(Border.ALL,jxl.format.BorderLineStyle.THIN);


		//         
		wlabel = new jxl.write.Label(0, 1, "  ", wcformat);
		sheet.addCell(wlabel);	

		wlabel = new jxl.write.Label(1, 1, " ", wcformat);
		sheet.addCell(wlabel);

		wlabel = new jxl.write.Label(2, 1, "  ", wcformat);
		sheet.addCell(wlabel);

		wlabel = new jxl.write.Label(3 , 1, "20060512", wcformat);
		sheet.addCell(wlabel);

		wlabel = new jxl.write.Label(3 , 1, "    ", wcformat);
		sheet.addCell(wlabel);

 
		//sheet.mergeCells(10, 0, 12, 0);
		try {
		
			//         
			wbook.write();
			//    ,    
			wbook.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
		
		//           end
		return "success";
	}









좋은 웹페이지 즐겨찾기