2015-07-09 54 views
1
package excel2JSON; 

import java.io.File; 
import java.io.FileInputStream; 
import java.util.Iterator; 

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.json.JSONArray; 
import org.json.JSONObject; 

public class excel { 

    public static void main(String[] args) 
    { 
     String str = ExcelToJSON(new File ("C:\\workbook.xlsx")); 
     System.out.println("JSON = " + str); 
    } 

    private static String ExcelToJSON(File file_open) 
    { 

      JSONObject json = null; 
      try { 
       FileInputStream input = new FileInputStream(file_open); 
       Workbook workbook = WorkbookFactory.create(input); 

       Sheet sheet = workbook.getSheetAt(0); 

       json = new JSONObject(); 

       JSONArray rows = new JSONArray(); 
       for (Iterator<Row> rows_it = sheet.rowIterator(); rows_it.hasNext();) { 
        Row row = rows_it.next(); 
        JSONObject jason_row = new JSONObject(); 


        JSONArray cells = new JSONArray(); 
    for (Iterator<Cell> cells_it = row.cellIterator(); cells_it.hasNext();) { 
         Cell cell = cells_it.next(); 

         //Check the cell type and format accordingly 
         switch (cell.getCellType()) 
         { 
          case Cell.CELL_TYPE_NUMERIC: 
          double numeric=cell.getNumericCellValue(); 
           cells.put(String.valueOf(numeric)); 
           break; 
          case Cell.CELL_TYPE_STRING: 
           cells.put(cell.getStringCellValue()); 
           break; 
         } 
        } 
        jason_row.put("cell", cells); 
        rows.put(jason_row); 
       } 

       // Create the JSON. 
       json.put("rows", rows); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      // Get the JSON text. 
      return json.toString(); 
     } 
} 

收到此錯誤:錯誤:無法找到或加載主類excel2JSON.excel

錯誤:無法找到或加載主類excel2JSON.excel

我不知道我爲什麼得到這個錯誤。我是Java的新手,我需要幫助來解決這個問題。我試圖將Excel文件轉換爲JSON文檔。這是我爲此編寫的代碼,但當我在Eclipse中運行該程序時遇到問題。

回答

1

eclipse中此錯誤的最常見原因是,當您移動類時,Eclipse將嘗試使用過時的運行配置,該配置不起作用。

單擊Eclipse中綠色播放按鈕旁邊的向下箭頭,選擇運行配置。

刪除對這個類的引用,然後關閉那個窗口,選擇excel類。再次單擊播放按鈕旁邊的向下箭頭,然後選擇作爲Java應用程序運行。

相關問題