2014-03-12 18 views
1

我的成績簿程序(我們在成績中找到平均分)在打印出excel文檔時工作不正常。只有學生1和平均分數爲標題打印和學生1的平均分數爲0,沒有測試成績或任何測試成績。我的成績簿程序不能正確打印出excel文檔

這裏是我的代碼:

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.text.DecimalFormat; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 

public class array { 


public static final String Dabook = "wording.xls"; 



@SuppressWarnings("unused") 
public static void main(String[] args) throws IOException { 

HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("wording.xls")); 
HSSFSheet sheet = wb.getSheet("Sheet1"); 

    int col = 0, num = 1; 
    Row startRow = sheet.createRow(0); 
    Cell startCell = startRow.createCell(0); 
    int cellNumber = 0; 
    double total = 0; 

    double [ ] [ ] gridGrades = 
     { { 87.5, 61.0, 70.5 }, 
      { 90.0, 88.5, 87.5 }, 
      { 78.0, 65.5, 70.5 }, 
      { 80.0, 79.0, 81.5 }, 
      { 60.5, 55.5, 50.0 } }; 
    for (int colTest = 0; colTest < gridGrades[colTest].length + 1; colTest++) { 
     for (int testNum = 0; testNum < gridGrades[colTest].length + 1;testNum++) { 

      if (testNum == 1) { 
       startCell.setCellValue(""); 
      } 
     if (testNum == gridGrades[0].length) { 
      startCell = startRow.createCell(testNum + 1); 
      startCell.setCellValue("Average"); 
      startCell = startRow.createCell(testNum + 2); 

     } 
    } 
    } 
for (double grade[] : gridGrades) { 
    startRow = sheet.createRow(num); 
    startCell = startRow.createCell(cellNumber); 
    startCell.setCellValue("Student " + num); 

    for (int poop = 0; poop < gridGrades[num - 1].length; poop++) { 
     if (poop == gridGrades[0].length - 1) { 
      double average = total/gridGrades[num - 1].length; 

      startCell = startRow.createCell(poop + 2); 
      DecimalFormat df = new DecimalFormat("###.##"); 
      Double averageDouble = Double.parseDouble(df 
        .format(average)); 
      startCell.setCellValue(averageDouble); 
      startCell = startRow.createCell(poop + 3); 

      total = 0; 
     } 
     for (int Scott = 0; startRow.getCell(Scott) != null; Scott++) { 
      sheet.autoSizeColumn(Scott); 
     } 

     FileOutputStream output = new FileOutputStream("wording.xls"); 
     wb.write(output); 
     output.close(); 
    } 

} 
{  
     System.out.printf("     Test %d:  Test %d: Test %d: Average:%n", num, num+1, num+2); 
    for(double[] grade : gridGrades) { 
     double average = (grade[col] + grade[col+1] + grade[col+2])/3; 
    System.out.printf("Student %d   %.2f  %.2f  %.2f  %.2f %n", num, grade[col], grade[col+1], grade[col+2], average); 
    num++; 


    } 
} 

}}

什麼是我做錯了什麼?

+0

你有什麼錯誤? – user902383

+0

您需要展示它應該是什麼樣子以及它實際外觀的例子。 –

+0

@ user902383唯一的錯誤是,它不能打印到excel電子表格 – Scott

回答

0
  • 將寫入塊置於循環之外,您只需寫入一次。
  • as @ user902383表示您需要在外部循環中使用num++語句。
  • 你從來沒有在單元格中設置評分,也沒有設置標題「測試N」。
  • 當您設置單元格中的等級爲@ user902383時,將此值添加到total

希望這會有所幫助。