jxl



public ActionForward exportExcel(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception {
    	response.setContentType("application/x-javascript;");
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("Application/msexcel");
    	String fileName = java.net.URLEncoder.encode("    ","UTF-8"); 
    	response.setContentType("application/vnd.ms-execl");  
    	response.setHeader("Content-disposition","attachment; filename="+fileName+".xls");
    	String n = request.getParameter("n");
    	
    	jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.COURIER, 18, WritableFont.BOLD,true);
    	jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
    	wcfFC.setAlignment(jxl.format.Alignment.CENTRE);
    	wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

    	//    
    	jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(WritableFont.COURIER,10, WritableFont.BOLD,true);
    	jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(wfc1);
    	wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
    	wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

    	//    
    	jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
    	wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
    	wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);  

    	ServiceManager sm = new ServiceManagerImp();
    	TrainPlanService service = (TrainPlanService) sm.getService(ServiceManager.TRAIN_PLAN_SERVICE, request);
    	
    	WritableWorkbook wbook = Workbook.createWorkbook(response.getOutputStream()); 
    	// sheet  
    	WritableSheet wsheet = wbook.createSheet("    ", 0);
    	String tpid = request.getParameter("tpid");
    	wsheet.addCell(new Label(0,0,service.getTTrainPlanByID(tpid).getName()));
    	wsheet.addCell(new Label(0,1,"  "));
    	wsheet.addCell(new Label(1,1,"  "));
    	wsheet.addCell(new Label(2,1,"   "));
    	
    	
    	
    	wbook.write();
    	wbook.close();
    	response.getOutputStream().close();  
    	
    	return null;
    }

     public static String read(File file) throws Exception {  
          StringBuffer sb = new StringBuffer();  
            
          Workbook wb = null;  
          try {  
              //          
              wb = Workbook.getWorkbook(file);  
              if (wb != null) {  
                  //                         
                  Sheet[] sheets = wb.getSheets();  
                  if (sheets != null && sheets.length != 0) {  
                      //              
                      for (int i=0;i<sheets.length;i++) {  
                          //             
                          int rows = sheets[i].getRows();  
                          //      
                          for (int j=0;j<rows;j++) {  
                              //              
                              Cell[] cells = sheets[i].getRow(j);  
                              if (cells != null && cells.length != 0) {  
                                  //        
                                  for (int k=0;k<cells.length;k++) {  
                                      //            
                                      String cell = cells[k].getContents();  
                                      //     
                                      sb.append(cell + "\t");  
                                  }  
                                  sb.append("\t
"); } } sb.append("\t
"); } } System.out.println(" :" + file + "
"); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { wb.close(); } return sb.toString(); }