java 讀取excel 2007 .xlsx文件 poi實現


工作需要讀取excel里面的行內容,使用java實現較為簡單。

在最開始,嘗試使用 jxl-2.6.12 來實現讀取excel 的行內容。但是按照網上的方法,程序根本無法正確處理文件流。經過谷姐的一番努力,發現jxl只能支持excel 2000而已(或許我用的方法有誤)。jxl 操作excel 2007 無望,無奈放棄之。

之后轉到apache 的poi 庫,看到它的文檔里面說到,都可以支持office 2010了,對於2007 應該不在話下。果斷轉投poi 的懷抱。

poi官方網址:http://poi.apache.org/

我下載的是poi 3.10版本。

解壓包后,將下面的jar包加入工程。

poi——1

測試poi代碼

package rw_excel;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class test_poi {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Test 
    public void test() throws IOException {
//        fail("Not yet implemented");
        String file_dir = "test.xlsx";
        Workbook book = null;
        book = getExcelWorkbook(file_dir);
        Sheet sheet = getSheetByNum(book,0);
        
        int lastRowNum = sheet.getLastRowNum();
        
        System.out.println("last number is "+ lastRowNum);
        
        for(int i = 0 ; i <= lastRowNum ; i++){
            Row row = null;
            row = sheet.getRow(i);
            if( row != null ){
                System.out.println("reading line is " + i);
                int lastCellNum = row.getLastCellNum();
                System.out.println("lastCellNum is " + lastCellNum );
                Cell cell = null;
                
                for( int j = 0 ; j <= lastCellNum ; j++ ){
                    cell = row.getCell(j);
                    if( cell != null ){
                        String cellValue = cell.getStringCellValue();
                        System.out.println("cell value is \n" + cellValue);
                    }
                }
            }
        }
        
    }

    public static Sheet getSheetByNum(Workbook book,int number){  
        Sheet sheet = null;  
        try {  
            sheet = book.getSheetAt(number);  
//          if(sheet == null){  
//              sheet = book.createSheet("Sheet"+number);  
//          }  
        } catch (Exception e) {  
            throw new RuntimeException(e.getMessage());  
        }  
        return sheet;  
    }  
    public static Workbook getExcelWorkbook(String filePath) throws IOException{  
        Workbook book = null;  
        File file  = null;  
        FileInputStream fis = null;   
          
        try {  
            file = new File(filePath);  
            if(!file.exists()){  
                throw new RuntimeException("文件不存在");  
            }else{  
                fis = new FileInputStream(file);  
                book = WorkbookFactory.create(fis); 
            }  
        } catch (Exception e) {  
            throw new RuntimeException(e.getMessage());  
        } finally {  
            if(fis != null){  
                fis.close();  
            }  
        }  
        return book;  
    } 
//    
}

excel 文件就在工程的目錄下,直接填寫了文件名。開始時,寫的是絕對路徑,可能是windows下 “\”的問題,出現文件不存在的情況。原因不明。

程序非常簡單,很多的代碼就是拷貝參考文章的。這里感謝“北京大鵬”這位博主,博文寫得很詳細。

jxl 參考文章:http://heisetoufa.iteye.com/blog/1932073

poi參考文章:http://blog.csdn.net/qq522935502/article/details/8374118


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM