2013-04-02 16 views
5

Utworzono ten kod, aby odczytać zawartość plików Excela za pomocą Apache POI. Używam eclipse jako edytor, ale kiedy uruchomiłem kod mam problem w wierszu, który mam pogrubioną czcionką. Jaki jest problem? Zawartość Excel jest następujący:odczytać plik Excela za pomocą Apache POI

Emp ID Name Salary 

1.0 john 2000000.0 

2.0 dean 4200000.0 

3.0 sam  2800000.0 

4.0 cass 600000.0 

import java.io.*; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
import org.apache.poi.hssf.usermodel.HSSFCell; 
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; 



public class ExcelRead { 

public static void main(String[] args) throws Exception { 
    File excel = new File ("C:\\Users\\Efi\\Documents\\test.xls"); 
    FileInputStream fis = new FileInputStream(excel); 

    HSSFWorkbook wb = new HSSFWorkbook(fis); 
    HSSFSheet ws = wb.getSheet("Input"); 

    int rowNum = ws.getLastRowNum()+1; 
    int colNum = ws.getRow(0).getLastCellNum(); 
    String[][] data = new String[rowNum][colNum]; 


    for (int i=0; i<rowNum; i++){ 
     HSSFRow row = ws.getRow(i); 
      for (int j=0; j<colNum; j++){ 
       HSSFCell cell = row.getCell(j); 
       String value = cellToString(cell); 
       data[i][j] = value; 
       System.out.println("The value is" + value); 

      } 
     } 
    } 

public static String cellToString (HSSFCell cell){ 

int type; 
Object result; 
type = cell.getCellType(); 

    switch(type) { 


    case 0://numeric value in excel 
     result = cell.getNumericCellValue(); 
     break; 
    case 1: //string value in excel 
     result = cell.getStringCellValue(); 
     break; 
    case 2: //boolean value in excel 
     result = cell.getBooleanCellValue(); 
     break; 
    default: 
     ***throw new RunTimeException("There are not support for this type of    
     cell");*** 
     } 

return result.toString(); 
} 

} 
+0

problem tis tutaj: throw new RuntimeException ("Istnieją nie obsługuje tego typu komórki "); – user2235454

+0

gdzie jest linia pogrubiona? "Throw new Exception ..." ?, funkcja cellToString musi złapać wyjątek, który rzucasz lub dołącza "rzuca wyjątek" za siebie. – user2235520

Odpowiedz

0

Należy zmienić ten RuntimeException z informacjami o jaki typ nie jest obsługiwany z instrukcji switch. Wtedy będziesz mógł dodać obsługę, więc żaden wyjątek nie zostanie zgłoszony.

Więc, aby zobaczyć obraz tego, co program robi zamiast

throw new RunTimeException("There are not support for this type of cell");

należy dodać

throw new RunTimeException("There are not support for type with id ["+type+"] of cell");

Będzie tylko, poinformować co tęsknisz . Jak poradzić sobie z tą sytuacją, zależy od Ciebie.

+0

Czy możesz mi pomóc z kodem, który muszę dodać? – user2235454

+0

@ user2235454, patrz edycja. Powodzenia ! –

3

Istnieje additional cell types oprócz tych, które przechwytujesz w swoim oświadczeniu switch. Masz przypadki dla 0 (CELL_TYPE_NUMERIC), 1 (CELL_TYPE_STRING) i 2, ale 2 jest CELL_TYPE_FORMULA. Oto dodatkowe Możliwe wartości:

  • 3: CELL_TYPE_BLANK
  • 4: CELL_TYPE_BOOLEAN
  • 5: CELL_TYPE_ERROR

Użyj Cell stałe dla typu komórek w zestawieniu przełącznika zamiast całkowitych literałów i użyj wszystkich 6, aby uchwycić wszystkie możliwe przypadki.

I tak jak @Vash już zasugerował, dołącz rzeczywistą komórkę type do swojej wiadomości RuntimeException.

2

Sprawdź, czy utworzyłem , który stworzyłem do odczytu zarówno plików XLSX, XLS, jak i CSV z łatwością używając lambdas z Java8, która używa Apache POI wewnątrz.

Oto przykład:

RowConverter<Country> converter = (row) -> new Country(row[0], row[1]); 

ExcelReader<Country> reader = ExcelReader.builder(Country.class) 
    .converter(converter) 
    .withHeader() 
    .csvDelimiter(';') 
    .sheets(1) 
    .build(); 

List<Country> list; 
list = reader.read("src/test/resources/CountryCodes.xlsx"); 
list = reader.read("src/test/resources/CountryCodes.xls"); 
list = reader.read("src/test/resources/CountryCodes.csv"); 

z następujących plików Excel i fasoli:

public static class Country { 
    public String shortCode; 
    public String name; 

    public Country(String shortCode, String name) { 
    this.shortCode = shortCode; 
    this.name = name; 
    } 
} 

Excel:

Code Country 
ad Andorra 
ae United Arab Emirates 
af Afghanistan 
ag Antigua and Barbuda 
... 
Powiązane problemy