2013-03-26 45 views
0

我試着在eclipse中運行這段代碼,但是我得到了這個:選擇不包含主類型的eclipse。 有誰知道我會怎麼做?我是java中的新手,我需要幫助! 我試圖做的程序是使用POI讀取excel文件! :)如何運行這個程序,它使用POI讀取excel

import java.io.File;  
import java.io.FileInputStream;  
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 sample2 { 

private void sample2(test) 

FileInputStream file = new FileInputStream(new File("C:\\test.xls")); 

//Get the workbook instance for XLS file 
HSSFWorkbook workbook = new HSSFWorkbook(test); 

//Get first sheet from the workbook 
HSSFSheet sheet = workbook.getSheetAt(0); 

//Get iterator to all the rows in current sheet 
Iterator<Row> rowIterator = sheet.iterator(); 

//Get iterator to all cells of current row 
Iterator<Cell> cellIterator = row.cellIterator(); 


try { 

FileInputStream file = new FileInputStream(new File("C:\\test.xls")); 

//Get the workbook instance for XLS file 
HSSFWorkbook workbook = new HSSFWorkbook(file); 

//Get first sheet from the workbook 
HSSFSheet sheet = workbook.getSheetAt(0); 

//Iterate through each rows from first sheet 
Iterator<Row> rowIterator = sheet.iterator(); 
while(rowIterator.hasNext()) { 
    Row row = rowIterator.next(); 

    //For each row, iterate through each columns 
    Iterator<Cell> cellIterator = row.cellIterator(); 
    while(cellIterator.hasNext()) { 

     Cell cell = cellIterator.next(); 

     switch(cell.getCellType()) { 
      case Cell.CELL_TYPE_BOOLEAN: 
       System.out.print(cell.getBooleanCellValue() + "\t\t"); 
       break; 
      case Cell.CELL_TYPE_NUMERIC: 
       System.out.print(cell.getNumericCellValue() + "\t\t"); 
       break; 
      case Cell.CELL_TYPE_STRING: 
       System.out.print(cell.getStringCellValue() + "\t\t"); 
       break; 
     } 
     } 
     System.out.println(""); 
    } 
    file.close(); 
    FileOutputStream out = 
     new FileOutputStream(new File("C:\\test.xls")); 
    workbook.write(out); 
    out.close(); 

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

    } 

回答

2

如果沒有主方法,則無法運行Java應用程序。

你需要的東西像下面這樣:

public static void main(String[] args) { 
    sample2 s = new sample2(); 
    s.sample(); 
} 

而且你的代碼包含了很多錯誤。您的位置:

  • 缺少的重要手段
  • 資本是爲SAMPLE2方法的輸入參數錯誤
  • 小姐類型
  • 的代碼被打破許多方面(字符串測試?)。您複製代碼以讀取文件兩次,以進行錯誤處理等。

閱讀關於Java的良好教程在這裏會有很大的幫助。有關Java和Excel的很好的教程可以找到here,並且注意一下主要方法,那就是你的Java應用程序的入口。

+0

我已經改變它,結果是未解決的問題編譯: \t方法sample()未定義類型sample2 \t at sample2.main(sample2.java:18) – userefoikon 2013-03-26 14:24:42

+0

正確。這意味着樣本方法不存在沒有參數。您需要刪除'testde'參數,如下面'javadev'所述。 – 2013-03-26 14:41:12

0

由於sample2方法中的標識符「test」,您的代碼將無法編譯。刪除它,然後運行該程序:在線程異常「主要」 java.lang.Error的:

只需添加下面的方法:

public static void main(String[] args) { 
    new sample2().sample2(); 
} 
相關問題