打开使用Java创建的Excel电子表格时的输入/输出错误

时间:2013-11-08 14:00:38

标签: java ios macos excel apache-poi

我使用NetBeans 7.4实现了以下Java类,以便使用用户提供的数据创建Excel电子表格:

      package registration;
      import  java.io.*; 
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.FileOutputStream;
      import java.io.InputStreamReader;
      import  org.apache.poi.hssf.usermodel.HSSFSheet;  
      import  org.apache.poi.hssf.usermodel.HSSFWorkbook; 
      import  org.apache.poi.hssf.usermodel.HSSFRow;
      import  org.apache.poi.hssf.usermodel.HSSFCell;  
      import java.util.Scanner; // program uses class Scanner 
      /**
      *
      * @author user
       */
     public class Registration {

            /**
           * @param args the command line arguments
           */
           /**  Now a recursive class**/
          public static void club(int option, int i, HSSFRow rowhead, HSSFSheet sheet,
        HSSFWorkbook hwb) {
             if (option <= 12);{
                HSSFRow row =   sheet.createRow((short)i);
        switch (option) {
          case 1:  System.out.println("Ardboe");
                    row.createCell((short) 0).setCellValue("Ardboe");
                 break;
          case 2:  System.out.println("Moortown");
                    row.createCell((short) 0).setCellValue("Moortown");              
                 break;
          case 3:  System.out.println("Ballinderry");
                   row.createCell((short) 0).setCellValue("Ballinderry"); 
                 break;
          case 4:  System.out.println("The Loup");
                   row.createCell((short) 0).setCellValue("The Loup");            
                 break;
          case 5:  System.out.println("Ballymaguigan");
                   row.createCell((short) 0).setCellValue("Ballymaguigan");      
                 break;
          case 6:  System.out.println("Brocagh");
                    row.createCell((short) 0).setCellValue("Brocagh");               
                 break;
          case 7:  System.out.println("Clonoe");
                    row.createCell((short) 0).setCellValue("Clonoe");               
                 break;
          case 8:  System.out.println("Derrylaughan");
                    row.createCell((short) 0).setCellValue("Derrylaughan");               
                 break;
          case 9:  System.out.println("Derrytresk");
                    row.createCell((short) 0).setCellValue("Derrytresk");               
                 break;
          case 10: System.out.println("Stewartstown");
                    row.createCell((short) 0).setCellValue("Stewartstown");               
                 break;
          case 11: System.out.println("Ogra Columcille");
                    row.createCell((short) 0).setCellValue("Ogra Columcille");               
                 break;
          case 12: System.out.println("Newbridge");
                    row.createCell((short) 0).setCellValue("Newbridge");               
                 break;
         default: break;
      }
    }
        }

         public static void main(String[] args) throws IOException{
          // TODO code application logic here
         int option;
          int i = 1;

         HSSFWorkbook hwb=new HSSFWorkbook();

         // create Scanner to obtain input from command window
        Scanner input = new Scanner( System.in );

          String filename="/Users/user/Documents/clubs.xls" ;
         HSSFSheet sheet =  hwb.createSheet("Clubs in Loughshore");
         System.out.println("Select Loughshore Club Number. 1-12. 13 for exit.");
         HSSFRow rowhead=   sheet.createRow((short)0);
         rowhead.createCell((short) 0).setCellValue("Clubs Listed");

         FileOutputStream fileOut =  new FileOutputStream(filename);
          option = input.nextInt();
        do{  
           club(option, i, rowhead, sheet, hwb);
           System.out.println("Select Loughshore Club Number. 1-12. 13 for exit.");

           option = input.nextInt();

             i ++;
           } 
          while (option <= 12);
          }

         }

但是,当我尝试在LibreOffice上打开创建的电子表格时,我不断收到“常规错误:输入/输出错误”。

似乎有什么问题?我正在使用Mac OS X Mountain Lion。

1 个答案:

答案 0 :(得分:2)

问题是你没有写文件HSSFWorkbook内的文件:你刚刚创建了一个名为clubs.xls的空文件(不是真正的xls文件)

看看我的实施:

public static void main(String[] args) throws IOException {
    // TODO code application logic here
    int option;
    int i = 1;
    // create Scanner to obtain input from command window
    Scanner input = new Scanner(System.in);
    String filename = "/Users/user/Documents/clubs.xls";
    HSSFWorkbook hwb = new HSSFWorkbook();
    HSSFSheet sheet = hwb.createSheet("Clubs in Loughshore");
    System.out.println("Select Loughshore Club Number. 1-12. 13 for exit.");
    HSSFRow rowhead = sheet.createRow((short) 0);
    rowhead.createCell((short) 0).setCellValue("Clubs Listed");
    option = input.nextInt();
    do {
        club(option, i, rowhead, sheet, hwb);
        System.out.println("Select Loughshore Club Number. 1-12. 13 for exit.");
        option = input.nextInt();
        i++;
    } while (option <= 12);
    input.close();
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(new File(filename));
        hwb.write(out);
        System.out.println("Excel written successfully..");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        out.close();
    }
}

最后,我使用FileOutputStream使用HSSFWorkbook方法记下您的write

此外,我注意到您使用了一种弃用的方法,例如createCell((short) 0),并且当我首先插入数字13时算法有一种奇怪的行为,至少对我而言。

侨!