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\n");  
                              }  
                          }  
                          sb.append("\t\n");  
                      }  
                  }  
                  System.out.println("成功读取了:" + file + "\n");  
              }  
          } catch (Exception e) {  
              System.out.println(e.getMessage());  
          } finally {  
              wb.close();  
          }  
          return sb.toString();  
      } 

你可能感兴趣的:(JavaScript,工作,.net,J#)