利用POI 組件包讀寫Excel文件

一.             下載與環境配置

 1. 下載POI組件包

下載地址:

 http://apache.mirror.phpchina.com/poi/release/bin/poi-bin-3.0.2-FINAL-20080204.zip

說明:   1docs目錄:爲POI的文檔;

        2)根目錄下的jar 文件該組件的開發包

 

  二.             源碼解析

程序解析展示:

導入的包有:

java.util.*

org.apache.poi.hssf.usermodel.HSSFWorkbook

org.apache.poi.hssf.usermodel.HSSFSheet

org.apache.poi.hssf.usermodel.HSSFRow

org.apache.poi.hssf.usermodel.HSSFCell

java.io.*

 

//創建新的EXCEL 工作薄
    HSSFWorkbook wb  =   new  HSSFWorkbook();
    HSSFSheet sheet  =  wb.createSheet( " sheet1 " );
    HSSFSheet sheet1  =  wb.createSheet( " sheet2 " );

     // 以下以寫表頭
     // 表頭爲第一行  沒一行輸入時都要重新定義  row
     HSSFRow row  =  sheet.createRow(( short )0);
     HSSFRow row1  = sheet1.createRow(( short )0);

    HSSFCell cell1  =  row.createCell(( short )0);
    HSSFCell cell2  =  row.createCell(( short )1);
    HSSFCell cell3  =  row.createCell(( short )2);

    HSSFCell cell11  =  row1.createCell(( short )0);
    HSSFCell cell21  =  row1.createCell(( short )1);
    HSSFCell cell31  =  row1.createCell(( short )2);

    //設置每列的屬性名
    cell1.setEncoding(( short )1);
    cell1.setCellType( 1 );
    cell2.setEncoding(( short )1);
    cell2.setCellType( 1 );
    cell3.setEncoding(( short )1);
    cell3.setCellType( 1 );

    cell11.setEncoding(( short )1);
    cell11.setCellType( 1 );
    cell21.setEncoding(( short )1);
    cell21.setCellType( 1 );
    cell31.setEncoding(( short )1);
    cell31.setCellType( 1 );

     // 定義表頭的內容
     cell1.setCellValue( " 湖南" );
     cell2.setCellValue( " 廣州" );
     cell3.setCellValue( " 上海" );

     cell11.setCellValue( " 湖南1" );
     cell21.setCellValue( " 廣州2 " );
     cell31.setCellValue( " 上海3 " );

     for  ( int i = 0 ; i < 4 ; i ++ )   {
         // 定義數據從第二行開始       
         row  =  sheet.createRow(( short ) i  +   1 );
        cell1  =  row.createCell(( short )  0 );
        cell2  =  row.createCell(( short )  1 );
        cell3  =  row.createCell(( short )  2 );

        cell1.setEncoding(( short )1);
        cell1.setCellType( 1 );
        cell2.setEncoding(( short )1);
        cell2.setCellType( 1 );
        cell3.setEncoding(( short )1);
        cell3.setCellType( 1 );

         // 填充內容
 
        cell1.setCellValue( "長沙" );
        cell2.setCellValue( "廣州" );
        cell3.setCellValue( "上海" );

    }
 String filename=application.getRealPath("/")+"test.xls";
 FileOutputStream fo=new FileOutputStream(filename);
        //設置該Excel是否可以讀寫 
        wb.write(fo);
 
        out.println("excel 文件生成,存放在"+filename);

 

 

 三.             全部代碼:

<%@ page language="java"  contentType ="text/html;charset=gb2312" %>
 <%@ page import=
"java.util.*,org.apache.poi.hssf.usermodel.HSSFWorkbook,org.apache.poi.hssf.usermodel.HSSFSheet,org.apache.poi.hssf.usermodel.HSSFRow,org.apach

e.poi.hssf.usermodel.HSSFCell,java.io.* " %>
 <%

    //創建新的EXCEL 工作薄
    HSSFWorkbook wb  =   new  HSSFWorkbook();
    HSSFSheet sheet  =  wb.createSheet( " sheet1 " );
    HSSFSheet sheet1  =  wb.createSheet( " sheet2 " );

     // 以下以寫表頭
     // 表頭爲第一行  沒一行輸入時都要重新定義  row
     HSSFRow row  =  sheet.createRow(( short )0);
     HSSFRow row1  = sheet1.createRow(( short )0);

    HSSFCell cell1  =  row.createCell(( short )0);
    HSSFCell cell2  =  row.createCell(( short )1);
    HSSFCell cell3  =  row.createCell(( short )2);

    HSSFCell cell11  =  row1.createCell(( short )0);
    HSSFCell cell21  =  row1.createCell(( short )1);
    HSSFCell cell31  =  row1.createCell(( short )2);

    //設置每列的屬性名
    cell1.setEncoding(( short )1);
    cell1.setCellType( 1 );
    cell2.setEncoding(( short )1);
    cell2.setCellType( 1 );
    cell3.setEncoding(( short )1);
    cell3.setCellType( 1 );

    cell11.setEncoding(( short )1);
    cell11.setCellType( 1 );
    cell21.setEncoding(( short )1);
    cell21.setCellType( 1 );
    cell31.setEncoding(( short )1);
    cell31.setCellType( 1 );

     // 定義表頭的內容
     cell1.setCellValue( " 湖南" );
     cell2.setCellValue( " 廣州" );
     cell3.setCellValue( " 上海" );

     cell11.setCellValue( " 湖南1" );
     cell21.setCellValue( " 廣州2 " );
     cell31.setCellValue( " 上海3 " );

     for  ( int i = 0 ; i < 4 ; i ++ )   {
         // 定義數據從第二行開始       
         row  =  sheet.createRow(( short ) i  +   1 );
        cell1  =  row.createCell(( short )  0 );
        cell2  =  row.createCell(( short )  1 );
        cell3  =  row.createCell(( short )  2 );

        cell1.setEncoding(( short )1);
        cell1.setCellType( 1 );
        cell2.setEncoding(( short )1);
        cell2.setCellType( 1 );
        cell3.setEncoding(( short )1);
        cell3.setCellType( 1 );

         // 填充內容
 
        cell1.setCellValue( "長沙" );
        cell2.setCellValue( "廣州" );
        cell3.setCellValue( "上海" );

    }
 String filename=application.getRealPath("/")+"test.xls";
 FileOutputStream fo=new FileOutputStream(filename);
        //設置該Excel是否可以讀寫 
        wb.write(fo);
 
        out.println("excel 文件生成,存放在"+filename);
   
 
 %>

  四.             結果截圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章