jexcel API中的IndexOutOfBoundsException

时间:2012-07-07 21:16:15

标签: java jxl

我有一个indexoutofboundsexception,我已经浏览了互联网,并查看了几个不同的解决方案,但没有运气这个代码。

这是我的错误消息:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at jxl.write.biff.WritableWorkbookImpl.getSheet(WritableWorkbookImpl.java:408)
at Segmentation.main(Segmentation.java:81)

第81行:

WritableSheet sheet = copy.getSheet(0); 

非常感谢一些帮助。谢谢!

import java.io.File; 
import java.util.Date; 
import jxl.*; 
import java.io.IOException;
import jxl.read.biff.*;
import jxl.write.*;
import jxl.Workbook;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class Segmentation{



    private static  final String googleTranslate = "http://http://translate.google.com/#zh-CN|zh-TW|";

    public static String translate(String string) {

        String translation = googleTranslate + string;
        URL translationURL;

        try { translationURL = new URL(translation); }
        catch(MalformedURLException e) { return e.getMessage(); }

        BufferedReader httpin;
        String fullPage = "";
        System.out.println(translation);
        try {
            httpin = new BufferedReader(
                    new InputStreamReader(translationURL.openStream()));
            String line;
            while((line=httpin.readLine()) != null) { fullPage += line + '\n'; }
            httpin.close();
        } catch(IOException e) { return e.getMessage(); }

        int begin = fullPage.indexOf("<span class=\"\">");
        int end = fullPage.indexOf("</span>");

        return fullPage.substring(begin + 15, end);

    }


public static void main(String[] args) throws IOException, WriteException, BiffException
    {

        /*try
        {
            Workbook workbook = Workbook.getWorkbook(new File("DATA_Chinese_All.xls"));
            Sheet sheet = workbook.getSheet(0);



        }
        catch (IOException e)
        {   
            e.printStackTrace();
        }
        catch (BiffException e)
        {
            e.printStackTrace();}*/



            Workbook workbook = Workbook.getWorkbook(new File("DATA_Chinese_All.xls"));
            WorkbookSettings s = new WorkbookSettings();  
            s.setUseTemporaryFileDuringWrite(true);  
            WritableWorkbook copy = Workbook.createWorkbook(new File("someFile.xls"), s);
            //WritableWorkbook copy = Workbook.createWorkbook(new File("output.xls"), workbook);


            WritableSheet sheet = copy.getSheet(0); 




            int max = 302;
            int count = 0;
            for(int i = 1; i < max; i++)
            {   
                WritableCell cell  = sheet.getWritableCell(1, i);

                if (cell.getType() == CellType.LABEL);

                {

                    Label l = (Label) cell;
                    //Result = "http://translate.google.com/#zh-CN|zh-TW|"+(l.getContents();)
                    l.setString(translate(l.getContents()));

                    //l.setString();
                }   
                count++;

            copy.write();
            copy.close();






    } 




}
}