2010-10-06 47 views
3

这将创建提供了文件格式的Excel文件是不是要处理它的时候有效的错误,不正确的Excel文件上添加了许多:为什么此代码生成无效的Excel文件?

public static void write() throws IOException, WriteException { 
    WorkbookSettings settings = new WorkbookSettings(); 
    File seurantaraportti = new File("ta.xls"); 
    WritableWorkbook seurw = Workbook.createWorkbook(ta,settings); 
    seurw.createSheet("ta", 0); 
    WritableSheet ws = seurw.getSheet(0); 
    addNumber(ws,0,0,100.0); 
    seurw.close(); 
} 

private static void addNumber(WritableSheet sheet, int column, int row, Double d) 
    throws WriteException, RowsExceededException { 
    Number number=new Number(column, row,d); 
    sheet.addCell(number); 
} 

我在做什么错?

+0

格式化您的问题请 – 2010-10-06 08:13:29

+0

您使用的是什么库?这不是标准的Java库。 – 2010-10-06 08:16:53

+1

* ta *变量是什么(它既不可见也不可见,或者您重写了它,甚至不会编译)。 * seurantaraportti *未使用。它应该是* ta *吗? – haylem 2010-10-06 08:22:19

回答

-2

writableWorkbook seurw = Workbook.createWorkbook(ta,settings);

必须是

将WritableWorkbook seurw = Workbook.createWorkbook( “TA”,设置);

+1

-1:很显然,如果代码看起来像这样,代码甚至不会编译,所以它是一个错字。如果你真的想纠正这个问题,把它写成评论。发布不是答案的答案只会阻止可能帮助您查看问题的其他人。 – chiccodoro 2010-10-06 09:01:30

+0

不会编译一个未定义的var?其他答案没有提到完全相同的? – Grumpy 2010-10-06 09:55:05

+0

它不会在Java中使用未定义的变量进行编译(除非使用允许部分编译的Eclipse编译器,但仍然无法运行程序)。 Chiccodoro的观点是有效的,尽管我发现反应有点严厉,因为我们只是试图领导OP澄清他的问题。这就是说,我删除了我的答案,因为有人发布了一个解决方案,现在问题已被澄清。 – haylem 2010-10-06 10:02:53

4

您不写任何内容到工作簿。你缺少

seurm.write()

关闭工作簿

seurw.close前();

以下是工作代码。

import java.io.File; 
import java.io.IOException; 

import jxl.Workbook; 
import jxl.WorkbookSettings; 
import jxl.write.Number; 
import jxl.write.WritableSheet; 
import jxl.write.WritableWorkbook; 
import jxl.write.WriteException; 
import jxl.write.biff.RowsExceededException; 

public class WriteExcel { 

    public static void write() throws IOException, WriteException { 
     WorkbookSettings settings = new WorkbookSettings(); 
     // settings.setLocale(new Locale("en", "EN")); 
     File ta = new File("ta.xls"); 
     WritableWorkbook seurw = Workbook.createWorkbook(ta, settings); 
     seurw.createSheet("ta", 0); 
     WritableSheet ws = seurw.getSheet(0); 
     addNumber(ws, 0, 0, 100.0); 
     seurw.write(); // You missed this line. 
     seurw.close(); 
    } 

    private static void addNumber(WritableSheet sheet, int column, int row, 
      Double d) throws WriteException, RowsExceededException { 
     Number number = new Number(column, row, d); 
     sheet.addCell(number); 
    } 

    public static void main(String[] args) { 
     try { 
      write(); 
     } catch (WriteException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 
相关问题