2017-07-09 21 views
0

私はthis.Iでこれを助けて私がこれをしているのを使ってこれをやっています。私はテストファイルで変数を定義しています。私は置き換えのためにテキストファイルを使用しています。これは、次のコードでArrayIndexOutOfBoundエラーが発生します。 import com.aspose.cells。*;私はAspose Cell Apiを使ってExcelシートの値を見つけて置き換えています。私は各変数の置換を定義するためにテキストファイルを使用しています

import javax.swing.*; 
    import java.io.*; 
    import java.util.ArrayList; 
    import java.util.List; 

    /** 
    * Created by Amit on 7/4/2017. 
    */ 
    public class CellExcel { 
     public static void main(String args[]) throws Exception{ 

      Workbook workbook = new Workbook("//home//amit//Documents//test.xlsx"); 

      Worksheet worksheet = workbook.getWorksheets().get(0); 

      // Specify the range where you want to search 
    // Here the range is E3:H6 
      CellArea area = CellArea.createCellArea("A1", "A20"); 

      // Specify Find options 
      FindOptions opts = new FindOptions(); 
      opts.setLookInType(LookInType.VALUES); 
      opts.setLookAtType(LookAtType.ENTIRE_CONTENT); 
      opts.setRange(area); 

      ////////////////////////// 

      Reader reader = 
        null; 

      try { 
       reader = new InputStreamReader(
         new FileInputStream("//home//amit//Documents//demo.txt")); 
      } 
      catch (FileNotFoundException e1) { 
       e1.printStackTrace(); 
      } 
      BufferedReader bufferedReader = new BufferedReader(reader); 
      List<String> lines = new ArrayList<String>(); 
      String line = null; 

      try { 
       while ((line = bufferedReader.readLine()) != null) 
       { 
        lines.add(line); 
       } 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 

      //////////////////////// 

      Cell cell = null; 


       for (int i = 0; i < lines.size(); i++) { 
        String[] splitting = lines.get(i).split("\t"); 

        String array1[] = new String[100]; 
        String array2[] = new String[100]; 
        array1[i] = splitting[0]; 
        array2[i] = splitting[1]; 

        System.out.println(array1[i]+"\t"+array2[i]); 

        for(int j = 0;j < 20;j++){ 
        // Search the cell with value search within range 
        cell = worksheet.getCells().find(array1[i], cell, opts); 

          // If no such cell found, then break the loop 
        if (cell == null) 
         break; 

        // Replace the cell with value replace 
        cell.putValue(array2[i]); 

        } 
        // System.out.println(array1[i]); 
        //System.out.println(array2[i]); 


       } 

    // Save the workbook 
      workbook.save("//home//amit//Documents//output.xlsx"); 
      // JOptionPane.showMessageDialog(null,"Success"); 
      System.out.println("Success"); 
     } 

    } 
+0

で開発者エバンジェリストとして働いています。また、範囲外の配列インデックスにアクセスする正確な場所も表示されます。次に、その場所に何らかの形でコードを修正する必要があります。また、 'i'で配列を無作為にインデックスしているように見えます。なぜなら、このコードでは基本的な誤りが多すぎるため、配列チュートリアルを行う必要があるようです。 – Kayaman

+0

@Amit、この例外はAspose.Cellsのためではないかもしれませんが、配列外にアクセスしている可能性があります。ですから、Kayamanの要求に応じてスタックトレースを提供してください。ありがとうございました。注:私はAsposeでDeveloper Evangelistとして働いています – shakeel

答えて

0

次のサンプルコードを参照してください。それはあなたのものと同じです。私は、サンプルExcelファイルでコードをテストし、エラーなしで正常に出力Excelファイルを生成しました。

私はこれらのサンプルファイルは、あなたをテストするために私によって作成された三つのこと

  1. Test.xlsx
  2. Output.xlsx
  3. Demo.txt

を示してスクリーンショットを添付している

コード。イメージでわかるように、Output.xlsxファイルはDemo.txtごとに正しいです。ここで

enter image description here

ご参照のために少し修正を除き、あなたと同じであるサンプル実行可能なコードです。

のJava

Workbook workbook = new Workbook(dirPath + "test.xlsx"); 

Worksheet worksheet = workbook.getWorksheets().get(0); 

// Specify the range where you want to search 
// Here the range is E3:H6 
CellArea area = CellArea.createCellArea("A1", "A20"); 

// Specify Find options 
FindOptions opts = new FindOptions(); 
opts.setLookInType(LookInType.VALUES); 
opts.setLookAtType(LookAtType.ENTIRE_CONTENT); 
opts.setRange(area); 

////////////////////////// 

Reader reader = null; 

try { 
    reader = new InputStreamReader(new FileInputStream(dirPath + "demo.txt")); 
} catch (FileNotFoundException e1) { 
    e1.printStackTrace(); 
} 
BufferedReader bufferedReader = new BufferedReader(reader); 
List<String> lines = new ArrayList<String>(); 
String line = null; 

try { 
    while ((line = bufferedReader.readLine()) != null) { 
     lines.add(line); 
    } 
} catch (IOException e1) { 
    e1.printStackTrace(); 
} 

//////////////////////// 

Cell cell = null; 

for (int i = 0; i < lines.size(); i++) { 
    String[] splitting = lines.get(i).split("\t"); 

    String array1[] = new String[100]; 
    String array2[] = new String[100]; 
    array1[i] = splitting[0]; 
    array2[i] = splitting[1]; 

    System.out.println(array1[i] + "\t" + array2[i]); 

    for (int j = 0; j < 20; j++) { 

     cell = worksheet.getCells().find(array1[i], cell, opts); 

     // If no such cell found, then break the loop 
     if (cell == null) 
      break; 

     // Replace the cell with value replace 
     cell.putValue(array2[i]); 

    } 

} 

// Save the workbook 
workbook.save(dirPath + "Output.xlsx"); 
System.out.println("Success"); 

注:私はあなたがスタックトレースを含める必要があるのAspose

関連する問題