java - jexcel API中的IndexOutOfBoundsException

原文 标签 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();






    } 




}
}

最佳答案

尝试使用createSheet而不是getSheet

http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/write/WritableWorkbook.html#createSheet%28java.lang.String,%20int%29

关于java - jexcel API中的IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11378688/

相关文章:

grails - 无法将类 'jxl.biff.EmptyCell@47821b4b'的对象 'jxl.biff.EmptyCell'强制转换为类 'jxl.DateCell'

java - 如何使用jxl java jar将自动筛选器添加到Excel单元格

java - 初始化图像

java - 如何在代码中添加错误处理?

java - 如何递归画一条线

java - 使用JXL在Excel中访问超过256列

java - 如何保证ThreadPoolExecutor中的FIFO执行顺序

java - 我不知道赋值运算符如何工作[处理]

java - JXL 和 Apache POI 解析带有附加图像对象的 excel

java - JExcel API:如何更改单元格的颜色?