robotium之Java讀取excel2010

來源:互聯網
上載者:User

標籤:

Java中讀取excel當然是比較麻煩的事情,不過現在開源的東西很多,自然就變的簡單,讓我們來看一下,上一篇和大家分享的是用jxl讀取excel,坑能有所缺陷,現在和大家分享的是poi讀取excel,它們兩個有什麼區別呢?

我們引用下面一段話

資料量3000條資料,每條60列.JVM虛擬機器記憶體大小64M.
使用POI:運行到2800條左右就報記憶體溢出.
使用JXL:3000條全部出來,並且記憶體還有21M的空間.
可想而知,在對記憶體的消耗方面差距還是挺大的.
也許是由於JXL在對資源回收利用方面做的還挺不錯的.
關於兩者效率方面,也是基於大資料量而言的,資料量小的話基本上差別不大,也不難被發覺.但是大的資料量,POI消耗的JVM記憶體遠比JXL消耗的多.但相比提供的功能的話,JXL又相對弱了點.所以如果要實現的功能比較複雜的情況下可以考慮使用POI,但如果只想產生一些大資料量可以考慮使用JXL,或者CSV也是一個不錯的選擇,不過CSV並不是真正的excel

這段話來自http://blog.csdn.net/lilinandlilin/article/details/24098699

這很明顯的區分了兩者的區別;這個呢只是個開頭,不是今天要分享的內容:

   以下是今天要分享的內容:poi讀取excel

首先jar包不可少,在我的網盤中可以下到串連為:

http://pan.baidu.com/s/1eQH7L30

然後找到

poi-ooxml-3.9-20121203.jar

poi-ooxml-schemas-3.9-20121203.jar

poi-3.9-20121203.jar

dom4j-1.6.1.jar

xmlbeans-2.3.0.jar

後面兩個jar在ooxml-lib檔案夾中;

添加完成後在D盤下面建兩個excel我們用來讀取;代碼如下

 

package poi;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFWorkbook;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.xssf.usermodel.XSSFWorkbook; public class ReadExcel001 {public static void main(String[] args) {readXml("D:/測試案例.xlsx");System.out.println("-------------");readXml("d:/測試案例.xls"); }public static void readXml(String fileName){boolean isE2007 = false;//判斷是否是excel2007格式if(fileName.endsWith("xlsx"))isE2007 = true;try {InputStream input = new FileInputStream(fileName);//建立輸入資料流Workbook wb  = null;//根據檔案格式(2003或者2007)來初始化if(isE2007)wb = new XSSFWorkbook(input);elsewb = new HSSFWorkbook(input);Sheet sheet = wb.getSheetAt(0);//獲得第一個表單Iterator<Row> rows = sheet.rowIterator();//獲得第一個表單的迭代器while (rows.hasNext()) {Row row = rows.next();//獲得行資料System.out.println("Row #" + row.getRowNum());//獲得行號從0開始Iterator<Cell> cells = row.cellIterator();//獲得第一行的迭代器while (cells.hasNext()) {Cell cell = cells.next();System.out.println("Cell #" + cell.getColumnIndex());switch (cell.getCellType()) {//根據cell中的類型來輸出資料case HSSFCell.CELL_TYPE_NUMERIC:System.out.println(cell.getNumericCellValue());break;case HSSFCell.CELL_TYPE_STRING:System.out.println(cell.getStringCellValue());break;case HSSFCell.CELL_TYPE_BOOLEAN:System.out.println(cell.getBooleanCellValue());break;case HSSFCell.CELL_TYPE_FORMULA:System.out.println(cell.getCellFormula());break;default:System.out.println("unsuported sell type");break;}}}} catch (IOException ex) {ex.printStackTrace();}}}

 

 

這樣就能讀取到excel內容了 

robotium之Java讀取excel2010

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.