(八)excel、word導入導出

5626 ワード

      Excel      javaAPI     ,
   JXL(Java Excel API)     :http://jexcelapi.sourceforge.net/
   Apache POI(Poor Obfuscation Implementation)    :http://poi.apache.org/

POI     OLE2    Office 2003     ;       OOXML(Office Open XML)  ,   Office 2007    。JXL     Excel 2003       。

POI  HSSF    OLE2  Excel,     .xls ;  XSSF、SXSSF    OOXML  Excel,     .xlsx 。

  OLE2   Excel,  Sheet           65536 ,    256 ;
  OOXML   Excel,  Sheet         1048576 ,    16384 。



  API:


    :


Excel2003   2007、2010
 :  255     16384
 :  65535       1048576

===================    ===================

     :
HSSFWorkbook wb = new HSSFWorkbook();   

     :
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(xlsFile));


    sheet  :
HSSFSheet sheet = wb.createSheet("        ");

        :
HSSFSheet sheet = wb.getSheetAt(0);

        :
wb.setSheetName(0, "        "); 


     : 
HSSFRow nRow = null;
n   Row = sheet.createRow(1);           // 2 

           :
HSSFCell nCell = null;
nCell = nRow.createCell((short)(2));        // 3 


           :
nCell.setCellValue("     ");


          :        
nCell.setCellStyle(leftStyle(wb));


      1:

           ,       。

    excel  
FileOutputStream fOut = new FileOutputStream(xlsFile);      //  xls  ,    0  
wb.write(fOut);                         //   ,xls        
fOut.flush();                           //     
fOut.close();                           //  


      2:


    //7.  excel  
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();          //     
    wb.write(byteArrayOutputStream);                                // excel   

    //   ,       :      
    String outFile = "       .xls";
    DownloadBaseAction down = new DownloadBaseAction();
    down.download(byteArrayOutputStream, response, outFile);


      3:(   struts2)


    ServletActionContext.getResponse().setContentType("application/octet-stream");
    String returnName = ServletActionContext.getResponse().encodeURL( new String("    .xls".getBytes(), "ISO-8859-1"));
    ServletActionContext.getResponse().addHeader("Content-Disposition", "attachment;filename=" + returnName);

    wb.write(ServletActionContext.getResponse().getOutputStream());

      4:

    //    
    response.setContentType("application/octet-stream");
    String returnName = response.encodeURL( new String("       .xls".getBytes(), "ISO-8859-1"));
    response.addHeader("Content-Disposition", "attachment;filename=" + returnName);

    wb.write(response.getOutputStream());

    :

//       
private HSSFCellStyle leftStyle(HSSFWorkbook wb){
    HSSFCellStyle curStyle = wb.createCellStyle();
    HSSFFont curFont = wb.createFont();                 //    
    //curFont.setFontName("Times New Roman");               //      
    curFont.setFontName("    ");                    //      
    curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);               //      ,                
    curFont.setFontHeightInPoints((short)10);               //    
    curFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);            //  

    curStyle.setFont(curFont);

    curStyle.setBorderTop(HSSFCellStyle.BORDER_THICK);          //   
    curStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);            //  
    curStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);            //     
    curStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);         //  

    curStyle.setWrapText(true);                         //     
    curStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);           //      
    curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);       //       

    return curStyle;
}


=================== web   ===================

      :    

PrintSetup ps = sheet.getPrintSetup();
ps.setLandscape(true);                          //    


     :
//bug        ,     
for(int i=0 ;i oList = oDao.find("from ContractProduct o where o.contract.id='"+contractId+"' order by o.factory.id,o.orderNo");

//          ,        
if(oProduct.getFactory().getFactoryName().equals(oldFactory)){  
}

2)            ,      
if(contract.getPrintStyle().equals("2")){
}


9、       

//       ,           
Map pageMap = null;
List pageList = new ArrayList();           //   



==    :

wb.cloneSheet(0);               //  sheet0   ,