2012-11-30 39 views

Trả lời

26

Bạn phải bỏ qua hai hàng đầu tiên sử dụng rownum() .Here là mẫu mã

HSSFWorkbook  workBook = new HSSFWorkbook (fileSystem); 
HSSFSheet   sheet = workBook.getSheetAt (0); 
Iterator<HSSFRow> rows  = sheet.rowIterator(); 

while (rows.hasNext()) 
{ 
    HSSFRow row = rows.next(); 
    // display row number in the console. 
    System.out.println ("Row No.: " + row.getRowNum()); 
    if(row.getRowNum()==0 || row.getRowNum()==1){ 
    continue; //just skip the rows if row number is 0 or 1 
    } 
} 

Dưới đây là ví dụ hoàn chỉnh

import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFRichTextString; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.poifs.filesystem.POIFSFileSystem; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 

import java.util.Iterator; 

public class POIExcelReader 
{ 

/** Creates a new instance of POIExcelReader */ 
public POIExcelReader() 
{} 

@SuppressWarnings ("unchecked") 
public void displayFromExcel (String xlsPath) 
{ 
InputStream inputStream = null; 

try 
{ 
inputStream = new FileInputStream (xlsPath); 
} 
catch (FileNotFoundException e) 
{ 
System.out.println ("File not found in the specified path."); 
e.printStackTrace(); 
} 

POIFSFileSystem fileSystem = null; 

try 
{ 
fileSystem = new POIFSFileSystem (inputStream); 

HSSFWorkbook  workBook = new HSSFWorkbook (fileSystem); 
HSSFSheet   sheet = workBook.getSheetAt (0); 
Iterator<HSSFRow> rows  = sheet.rowIterator(); 

while (rows.hasNext()) 
{ 
HSSFRow row = rows.next(); 
if(row.getRowNum()==0 || row.getRowNum()==1){ 
     continue; //just skip the rows if row number is 0 or 1 
     } 
// once get a row its time to iterate through cells. 
Iterator<HSSFCell> cells = row.cellIterator(); 

while (cells.hasNext()) 
{ 
HSSFCell cell = cells.next(); 

System.out.println ("Cell No.: " + cell.getCellNum()); 

/* 
* Now we will get the cell type and display the values 
* accordingly. 
*/ 
switch (cell.getCellType()) 
{ 
    case HSSFCell.CELL_TYPE_NUMERIC : 
    { 

     // cell type numeric. 
     System.out.println ("Numeric value: " + cell.getNumericCellValue()); 

     break; 
    } 

    case HSSFCell.CELL_TYPE_STRING : 
    { 

     // cell type string. 
     HSSFRichTextString richTextString = cell.getRichStringCellValue(); 

     System.out.println ("String value: " + richTextString.getString()); 

     break; 
    } 

    default : 
    { 

     // types other than String and Numeric. 
     System.out.println ("Type not supported."); 

     break; 
    } 
} 
} 
} 
} 
catch (IOException e) 
{ 
e.printStackTrace(); 
} 
} 

public static void main (String[] args) 
{ 
POIExcelReader poiExample = new POIExcelReader(); 
String   xlsPath = "c://test//test.xls"; 

poiExample.displayFromExcel (xlsPath); 
} 
} 
+0

Tôi cần đọc nội dung của tệp .xls và .xlsx, có 9 cột và 100 hàng. Các nội dung đã đọc phải được thêm vào danh sách và chèn vào một sql db dưới dạng chèn hàng loạt và giá trị shud được chèn vào cột tương ứng. làm ơn giúp tôi !! – Priya

4

Apache POI cung cấp hai cách để truy cập vào các hàng và các tế bào trong một tệp Excel. Một là một trình lặp cung cấp cho bạn tất cả các mục nhập, cái kia là lặp lại theo chỉ mục. (POI cũng sẽ cho bạn biết các hàng/cột bắt đầu/kết thúc). Trình vòng lặp thường đơn giản hơn để sử dụng, nhưng cả hai đều đều nhanh như nhau.

Nếu bạn có yêu cầu cụ thể về các hàng cần tìm, tôi khuyên bạn nên sử dụng hàng sau. Mã của bạn sẽ muốn có một cái gì đó như:

int FIRST_ROW_TO_GET = 2; // 0 based 

Sheet s = wb.getSheetAt(0); 
for (int i = FIRST_ROW_TO_GET; i < s.getLastRowNum(); i++) { 
    Row row = s.getRow(i); 
    if (row == null) { 
     // The whole row is blank 
    } 
    else { 
     for (int cn=row.getFirstCellNum(); cn<row.getLastCellNum(); cn++) { 
     Cell c = row.getCell(cn, Row.RETURN_BLANK_AS_NULL); 
     if (c == null) { 
      // The cell is empty 
     } else { 
      // Process the cell 
     } 
     } 
    } 
} 
+0

hello sir tôi có một truy vấn "làm thế nào để đọc chỉ có dòng đầu tiên tế bào đầu tiên chỉ" xin vui lòng giúp tôi –

Các vấn đề liên quan