2017-06-12 54 views
-2

我是二年級學生。我必須完成我的java項目,但如果你們中的任何人都可以幫忙,我會面臨一些問題。我的項目是使用java在excel工作表中輸入學生的標記並計算標記的平均值。我寫了下面的代碼,但它無法正常工作......在java中使用excel表格

這裏是我的代碼...

import java.util.*; 
import org.apache.poi.hssf.usermodel.*; 
import org.apache.poi.hssf.util.*; 
import org.apache.poi.ss.usermodel.*; 
import org.apache.poi.ss.util.*; 
import java.io.*; 

public class exceluser { 

    public static void main(String[] args){ 

     String St_Id = ""; 
     String Name = ""; 
     int Marks = 0; 
     boolean keepRunning = true; 


     //Blank workbook 
     HSSFWorkbook workbook = new HSSFWorkbook(); 
     //Blank sheet 
     HSSFSheet sheet = workbook.createSheet("Seating Details"); 

     //create heading 

     Row rowHeading = sheet.createRow(0); 
     rowHeading.createCell(0).setCellValue("Student ID"); 
     rowHeading.createCell(1).setCellValue("Student(s) Name"); 
     rowHeading.createCell(2).setCellValue("Marks"); 

     //Font size and style loop for my headers 


     for(int i = 0; i < 3; i++) 
     { 
      CellStyle stylerowHeading = workbook.createCellStyle(); 
      Font font = workbook.createFont(); 
      font.setBold(true); 
      font.setFontName(HSSFFont.FONT_ARIAL); 
      font.setFontHeightInPoints((short) 11); 
      stylerowHeading.setFont(font); 
      rowHeading.getCell(i).setCellStyle(stylerowHeading); 
     } 
     int rownum=1; 

     while(keepRunning){ 
      Scanner scnr = new Scanner(System.in); 

      System.out.print("Enter student ID : "); 
      St_Id = scnr.nextLine(); 
      if(St_Id.equals("EXIT")) 
      { 
       System.out.print("You have ended the program"); 
       System.exit(0); 
      } 

      else 
      { 
       System.out.print("Enter the student name : "); 
       Name = scnr.nextLine(); 
       System.out.print("Enter the marks of the student : "); 
       Marks = scnr.nextInt(); 

       //This data needs to be written (Object[]) 
       Map <String, Object[]> data = new TreeMap<String, Object[]>(); 
       data.put("2", new Object[] {St_Id, Name, Marks}); 

       //Iterate over data and write to sheet 
       Set<String> keyset = data.keySet(); 

       for(String Key : keyset) 
       { 
        Row row = sheet.createRow(rownum++); 
        Object [] objArr = data.get(Key); 
        int cellnum = 0; 
        for(Object obj : objArr) 
        { 
         Cell cell = row.createCell(cellnum++); 
         if(obj instanceof String) 
         { 
          cell.setCellValue((String)obj); 
         } 
         else if(obj instanceof Integer) 

          cell.setCellValue((Integer)obj); 
        } 
       } 
       if(!(St_Id.equals("EXIT"))) 
       { 
        rownum++; 
       } 

       //Auto size my columns that will be filled out with user input info.    
       for (int i = 0; i < 3; i++) 
       { 
        sheet.autoSizeColumn(i); 
       } 
      } 
      try{ 

       //save to excel file 
       FileOutputStream out = new FileOutputStream(new File("Student Grading Report.xls")); 
       workbook.write(out); 
       out.flush(); 
       out.close(); 
       System.out.println("Record added Succesfully..."); 
      } catch (FileNotFoundException e) { 

       e.printStackTrace(); 

      } catch (IOException e){ 

       e.printStackTrace(); 

      } catch (Exception e) { 

       System.out.println(e.getMessage()); 
      } 
     } 

    }//pub static void end brace  
}//pub class end brace 

幫我在此代碼,如果任何人都可以..這段代碼執行時,它離開輸入一個學生數據後,excel中的一行,以及找到學生平均分數的問題。

+0

正確縮進代碼將幫助您查看代碼中每個點發生了什麼 –

回答

0

這是一個任務,所以我不會給你的代碼,但會給你一些要考慮的問題。

  • 你真的應該爲不同的任務使用單獨的方法 - 這將使你的代碼更容易遵循,也更容易編寫和調試。

  • 您目前一次只能爲學生輸入一個標記。您可能希望將您的地圖放在循環之外,並將關鍵字作爲學生ID,並使用Integers的ArrayList作爲學生分數。更好的辦法是創建一個Student類來保存這些數據。

  • 要計算你只需要計算值的平均值在列表中對於學生的平均

  • 不要使用System.exit(0)退出程序,而是用它來設置你的keepRunning布爾假以打破循環。

  • 考慮在更改上述內容後,將代碼寫入循環外的文件。