在ADF导出excel文件并保存在客户端

来源:互联网 发布:php include 变量 编辑:程序博客网 时间:2024/05/22 03:40

import java.net.URLEncoder;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class Jexcel {

    public String exprot3A4(){
        try {
          ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
          HttpServletResponse response = (HttpServletResponse)ectx.getResponse();
          response.setHeader("Content-disposition","attachment;filename="+URLEncoder.encode("测试.xls","UTF-8"));
          response.flushBuffer();
          WritableWorkbook book = Workbook.createWorkbook(response.getOutputStream());
         
            WritableSheet sheet  =  book.createSheet( " 第一页 " ,  0 );  
                       //  在Label对象的构造子中指名单元格位置是第一列第一行(0,0)  
                       //  以及单元格内容为test   
                       Label label  =   new  Label( 0 ,  0 ,  " test " );  
            
                       //  将定义好的单元格添加到工作表中   
                       sheet.addCell(label);  
            
                       /*  
                       * 生成一个保存数字的单元格 必须使用Number的完整包路径,否则有语法歧义 单元格位置是第二列,第一行,值为789.123 
                        */   
                      jxl.write.Number number  =   new  jxl.write.Number( 1 ,  0 ,  555.12541 );  
                      sheet.addCell(number);  
                      /**
                       * 字体为TIMES,字号16,加粗显示。WritableFont有非常丰富
                       * 构造子,供不同情况下使用,jExcelAPI的java-doc中有详细列表,这里不再列出。 
                       */
                      WritableFont font1 = new WritableFont(WritableFont.TAHOMA,9,WritableFont.BOLD);
                      WritableCellFormat format1 = new WritableCellFormat(font1);
                     
                      Label labelHear = new Label(5,1,"Purchase Order 3A4",format1);
                      sheet.addCell(labelHear);
                     
                    Label lbPONo = new Label(1,3,"PO No.:",format1);
                    sheet.addCell(lbPONo);                           
                    Label lbPORev = new Label(3,3,"PO Rev.:",format1);
                    sheet.addCell(lbPORev);                   
                    Label lbAgreementNo = new Label(5,3,"Agreement No.:",format1);
                    sheet.addCell(lbAgreementNo);                  
                    Label lbContractNo = new Label(7,3,"Contract No.:",format1);
                    sheet.addCell(lbContractNo);
                    Label lbCreatedTime = new Label(9,3,"Created Time:",format1);
                    sheet.addCell(lbCreatedTime);
                   
                    Label lbBuyer = new Label(1,4,"Buyer:",format1);
                    sheet.addCell(lbBuyer);                           
                    Label lbBuyerCode = new Label(3,4,"Buyer Code:",format1);
                    sheet.addCell(lbBuyerCode);                  
                    Label lbContactName = new Label(5,4,"Contract Name:",format1);
                    sheet.addCell(lbContactName);                   
                    Label lbContractPerson = new Label(7,4,"Contract Person:",format1);
                    sheet.addCell(lbContractPerson);                   
                    Label lbTel = new Label(9,4,"Tel:",format1);
                    sheet.addCell(lbTel);                   
                           
                    Label lbTotalPrice = new Label(1,5,"Total Price:",format1);
                    sheet.addCell(lbTotalPrice);                           
                    Label lbCurrency = new Label(3,5,"Currency:",format1);
                    sheet.addCell(lbCurrency);                  
                    Label lbSCNo = new Label(5,5,"SC No.:",format1);
                    sheet.addCell(lbSCNo);                   
                    Label lbSONo = new Label(7,5,"SO No.",format1);
                    sheet.addCell(lbSONo);                   
                    Label lbSalesPerson = new Label(9,5,"Sales Person:",format1);
                    sheet.addCell(lbSalesPerson);        
                   
                    Label lbStatus = new Label(1,6,"Status:",format1);
                    sheet.addCell(lbStatus); 
                    Label lbRemark = new Label(3,6,"Remark:",format1);
                    sheet.addCell(lbRemark); 
           
                    Label lbLine = new Label(1,8,"Line",format1);
                    sheet.addCell(lbLine);                           
                    Label lbItem = new Label(2,8,"Item",format1);
                    sheet.addCell(lbItem);                  
                    Label lbDescription = new Label(3,8,"Description",format1);
                    sheet.addCell(lbDescription);                   
                    Label lbAddDesc = new Label(4,8,"Add Desc.",format1);
                    sheet.addCell(lbAddDesc);                   
                    Label lbRequestQty = new Label(5,8,"Request Qty",format1);
                    sheet.addCell(lbRequestQty);
                    Label lbPromisedQty = new Label(6,8,"Promised Qty",format1);
                    sheet.addCell(lbPromisedQty);                           
                    Label lbUOM = new Label(7,8,"UOM",format1);
                    sheet.addCell(lbUOM);                  
                    Label lbListPrice = new Label(8,8,"Lsit Price",format1);
                    sheet.addCell(lbListPrice);                   
                    Label lbUnitPrice = new Label(9,8,"Unit Price",format1);
                    sheet.addCell(lbUnitPrice);                   
                    Label lbRequestDate = new Label(10,8,"Request Date",format1);
                    sheet.addCell(lbRequestDate);
                    Label lbPromisedDate = new Label(11,8,"Promised Date",format1);
                    sheet.addCell(lbPromisedDate);
                    Label lbSta= new Label(12,8,"Status",format1);
                    sheet.addCell(lbSta);
                                           
                    sheet.setColumnView(1,15);
                    sheet.setColumnView(2,15);
                    sheet.setColumnView(3,15);
                    sheet.setColumnView(4,15);
                    sheet.setColumnView(5,15);
                    sheet.setColumnView(6,15);
                    sheet.setColumnView(7,15);
                    sheet.setColumnView(8,15);
                    sheet.setColumnView(9,15);
                    sheet.setColumnView(10,15);
                    sheet.setColumnView(11,15);
                    sheet.setColumnView(12,15);
                   
                   //  写入数据并关闭文件   
                    book.write();  
                    book.close();  

        } catch (Exception e) {
            // TODO
            System.out.println(e);
        }
        return null;
    }
}

原创粉丝点击