繁体   English   中英

我正在使用Aspose Cell Api从Excel工作表中查找和替换值。 我正在使用文本文件来定义每个变量的替换

[英]I am using Aspose Cell Api to find and replace values from excel sheet. I am using text file to define replacement of each variable

我正在使用java执行此操作。请帮助我。我在测试文件中定义了变量,并且我正在使用文本文件进行替换,但未执行。请在此帮助我。在以下代码中出现ArrayIndexOutOfBound错误。 导入com.aspose.cells。*;

    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");
        }

    }

请参见以下示例代码。 和你一样 我已经使用示例Excel文件测试了代码,并成功生成了输出Excel文件,没有任何错误。

我已经附上了显示三件事的屏幕截图

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

这些示例文件是我创建的,用于测试您的代码。 如您在图像中看到的,根据Demo.txt,Output.xlsx文件是正确的。

在此处输入图片说明

这是示例可运行代码,与您的代码相同,只是稍作修改以供参考。

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担任开发人员布道者

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM