Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较包含两个单词的excel的两个字符串,并相应地编写_Java_String_Excel_Jxl - Fatal编程技术网

Java 比较包含两个单词的excel的两个字符串,并相应地编写

Java 比较包含两个单词的excel的两个字符串,并相应地编写,java,string,excel,jxl,Java,String,Excel,Jxl,我想比较两个Columin和excel,并将结果写在第三列 输入文件如下所示 输出文件应该是这样的 这是我到现在为止所做的事 导入jxl.Cell; 进口jxl.Sheet; 导入jxl.工作簿; 导入org.openqa.selenium.By; 导入org.openqa.selenium.WebDriver; 导入org.openqa.selenium.WebElement; 导入org.openqa.selenium.firefox.FirefoxDriver; 导入org.testn

我想比较两个Columin和excel,并将结果写在第三列

输入文件如下所示

输出文件应该是这样的

这是我到现在为止所做的事

导入jxl.Cell;
进口jxl.Sheet;
导入jxl.工作簿;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.testng.annotations.BeforeClass;
导入java.io.FileInputStream;
进口jxl.Cell;
进口jxl.Sheet;
导入jxl.工作簿;
导入jxl.format.color;
导入jxl.format.Pattern;
导入jxl.read.biff.BiffException;
导入jxl.write.Label;
导入jxl.write.WritableCellFormat;
导入jxl.write.WritableFont;
导入jxl.write.WritableSheet;
导入jxl.write.WritableWorkbook;
导入jxl.write.WriteException;
导入jxl.write.biff.rowseceedexception;
公开课考试{
/**
*@param args
*///变量声明
字符串元素;
静态字符串storedElement[][]=null;
静态字符串元素[][]=null;
publicstaticvoidmain(字符串[]args)抛出biffeException、IOException、rowseceedexception、WriteException{
//TODO自动生成的方法存根
FileInputStream fs=新的FileInputStream(“文件位置(.xls)”;
工作簿wb=Workbook.getWorkbook(fs);
表sh=wb.getSheet(0);
工作簿=工作簿.get工作簿(新文件(“文件位置(.xls)”);
WritableWorkbook ww=Workbook.createWorkbook(新文件(“文件位置(.xls)在此处重命名文件”)、工作簿);
WritableSheet ws=ww.getSheet(0);
标签;
//从excel检索值并存储在数组中
storedElement=新字符串[sh.getColumns()][sh.getRows()];
元素=新字符串[sh.getColumns()][sh.getRows()];

//对于(int j=0;j您是否调试了代码?您应该使用equals来比较
字符串

因此,将if(ob[k]==ob1[k]){
替换为if(ob[k].=ob1[k]){


(我没有实际测试您的代码,但这可能是主要问题)

嗨,我已经解决了您的问题。 使用下面的代码以您所说的格式获得结果

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

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class Test {


    String element;
    static String storedElement[][] = null;
    static String Element[][] = null;


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


        FileInputStream fs = new FileInputStream("file location(.xls)");
        Workbook wb = Workbook.getWorkbook(fs);
        Sheet sh = wb.getSheet(0);

        Workbook workbook = Workbook.getWorkbook(new File("file location(.xls)"));
        WritableWorkbook ww = Workbook.createWorkbook(new File("file location(.xls)RENAME THE FILE here"),
            workbook);
        WritableSheet ws = ww.getSheet(0);

        Label label = null;


        Test.storedElement = new String[sh.getColumns()][sh.getRows()];
        Test.Element = new String[sh.getColumns()][sh.getRows()];

        int j = 0; // column #0
        int q = 1; // column #1

        for (int i = 1; i < sh.getRows(); i++) {
            Cell cell = sh.getCell(j, i);
            Cell cell1 = sh.getCell(q, i);

            Test.storedElement[j][i] = cell.getContents();
            Test.Element[q][i] = cell1.getContents();
            String elem = Test.storedElement[j][i];
            System.out.println(elem);
            String elem1 = Test.Element[q][i];
            System.out.println(elem1);


            String[] ob = elem1.split("\n");
            String[] ob1 = elem.split("\n");

            String valid = "valid";
            String invalid = "invalid";
            String Status = "";
            if ((ob[0].equals(ob1[0])) && (ob[1].equals(ob1[1]))) {
                Status = valid + "\n" + valid;
            } else if ((!ob[0].equals(ob1[0])) && (ob[1].equals(ob1[1]))) {
                Status = invalid + "\n" + valid;
            } else if ((!ob[0].equals(ob1[0])) && (!ob[1].equals(ob1[1]))) {
                Status = valid + "\n" + invalid;
            } else {
                Status = invalid + "\n" + invalid;
            }

            label = new Label(2, i, Status);
            ws.addCell(label);


        }
        ww.write();
        ww.close();
        workbook.close();
    }
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
进口jxl.Cell;
进口jxl.Sheet;
导入jxl.工作簿;
导入jxl.read.biff.BiffException;
导入jxl.write.Label;
导入jxl.write.WritableSheet;
导入jxl.write.WritableWorkbook;
导入jxl.write.WriteException;
导入jxl.write.biff.rowseceedexception;
公开课考试{
字符串元素;
静态字符串storedElement[][]=null;
静态字符串元素[][]=null;
publicstaticvoidmain(最终字符串[]args)抛出biffeException、IOException、rowseceedexception、WriteException{
FileInputStream fs=新的FileInputStream(“文件位置(.xls)”;
工作簿wb=Workbook.getWorkbook(fs);
表sh=wb.getSheet(0);
工作簿=工作簿.get工作簿(新文件(“文件位置(.xls)”);
WritableWorkbook ww=Workbook.createWorkbook(新文件(“文件位置(.xls)在此处重命名文件”),
工作手册);
WritableSheet ws=ww.getSheet(0);
标签=空;
Test.storedElement=新字符串[sh.getColumns()][sh.getRows()];
Test.Element=新字符串[sh.getColumns()][sh.getRows()];
int j=0;//列#0
int q=1;//列#1
对于(int i=1;i
包com.test;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
进口jxl.Cell;
进口jxl.Sheet;
导入jxl.工作簿;
导入jxl.format.Alignment;
导入jxl.read.biff.BiffException;
导入jxl.write.Label;
导入jxl.write.WritableCellFormat;
导入jxl.write.WritableSheet;
导入jxl.write.WritableWorkbook;
导入jxl.write.WriteException;
导入jxl.write.biff.rowseceedexception;
公开课考试{
字符串元素;
静态字符串storedElement[][]=null;
静态字符串元素[][]=null;
publicstaticvoidmain(最终字符串[]args)抛出biffeException、IOException、rowseceedexception、WriteException{
FileInputStream fs=newfileinputstream(“D:/app/sweta/Book1.xls”);
工作簿wb=Workbook.getWorkbook(fs);
表sh=wb.getSheet(0);
工作簿=Workbook.getWorkbook(新文件(“D:/app/sweta/Book1.xls”);
WritableWorkbook ww=Workbook.createWorkbook(新文件(“D:/app/sweta/Book4.xls”),
工作手册);
WritableSheet ws=ww.getSheet(0);
标签=空;
Test.storedElement=新字符串[sh.getColumns()][sh.getRows()];
Test.Element=新字符串[sh.get]
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;

import java.io.FileInputStream;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.Pattern;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class Test {

    /**
     * @param args
     */ //variable declarations

    String element;
    static String storedElement[][]=null;
    static String Element[][]=null;


    public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException {
        // TODO Auto-generated method stub

        FileInputStream fs=new FileInputStream("file location(.xls)");  
        Workbook wb=Workbook.getWorkbook(fs);
        Sheet sh=wb.getSheet(0);

        Workbook workbook = Workbook.getWorkbook(new File("file location(.xls)"));
        WritableWorkbook ww= Workbook.createWorkbook(new File("file location(.xls)RENAME THE FILE here "), workbook);
        WritableSheet ws=ww.getSheet(0);

        Label label;

        //retrieving values from excel and storing in array
        storedElement = new String[sh.getColumns()][sh.getRows()];
        Element = new String[sh.getColumns()][sh.getRows()];
        //for (int j = 0; j <sh.getColumns(); j++) 
        //{ 
        int j=0; //column #0
        int q=1; //column #1
        //StringBuilder sb = new StringBuilder();
        for (int i = 2; i < sh.getRows(); i++) //loop to run all rows

        {
            Cell cell = sh.getCell(j, i);
            Cell cell1=sh.getCell(q,i);

            storedElement[j][i] = cell.getContents();
            Element[q][i]=cell1.getContents();
            String elem =  storedElement[j][i];
            System.out.println(elem);
            String elem1= Element[q][i];
            System.out.println(elem1);
            String[] ob1=elem1.split("\n");
            System.out.println(ob1[i]);
            String[] ob = elem.split("\n");
            System.out.println(ob[i]);


        //for(int k=0;k<ob.length;k++){
            int k=0;
            while (k<ob.length){
            if (ob[k]==ob1[k]){
                    System.out.println(ob[k]);
                    System.out.println(ob1[k]);
                    label=new Label(2,i,"Valid");
                }
                else {
                    System.out.println(ob[k]);
                    System.out.println(ob1[k]);
                    label=new Label(2,i,"InValid");

                }
            k++;
            //add new line ??

                ws.addCell(label);
                ww.write();
                ww.close();
                workbook.close();


                }
        }

    }
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class Test {


    String element;
    static String storedElement[][] = null;
    static String Element[][] = null;


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


        FileInputStream fs = new FileInputStream("file location(.xls)");
        Workbook wb = Workbook.getWorkbook(fs);
        Sheet sh = wb.getSheet(0);

        Workbook workbook = Workbook.getWorkbook(new File("file location(.xls)"));
        WritableWorkbook ww = Workbook.createWorkbook(new File("file location(.xls)RENAME THE FILE here"),
            workbook);
        WritableSheet ws = ww.getSheet(0);

        Label label = null;


        Test.storedElement = new String[sh.getColumns()][sh.getRows()];
        Test.Element = new String[sh.getColumns()][sh.getRows()];

        int j = 0; // column #0
        int q = 1; // column #1

        for (int i = 1; i < sh.getRows(); i++) {
            Cell cell = sh.getCell(j, i);
            Cell cell1 = sh.getCell(q, i);

            Test.storedElement[j][i] = cell.getContents();
            Test.Element[q][i] = cell1.getContents();
            String elem = Test.storedElement[j][i];
            System.out.println(elem);
            String elem1 = Test.Element[q][i];
            System.out.println(elem1);


            String[] ob = elem1.split("\n");
            String[] ob1 = elem.split("\n");

            String valid = "valid";
            String invalid = "invalid";
            String Status = "";
            if ((ob[0].equals(ob1[0])) && (ob[1].equals(ob1[1]))) {
                Status = valid + "\n" + valid;
            } else if ((!ob[0].equals(ob1[0])) && (ob[1].equals(ob1[1]))) {
                Status = invalid + "\n" + valid;
            } else if ((!ob[0].equals(ob1[0])) && (!ob[1].equals(ob1[1]))) {
                Status = valid + "\n" + invalid;
            } else {
                Status = invalid + "\n" + invalid;
            }

            label = new Label(2, i, Status);
            ws.addCell(label);


        }
        ww.write();
        ww.close();
        workbook.close();
    }
}
    package com.test;

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

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class Test {


    String element;
    static String storedElement[][] = null;
    static String Element[][] = null;


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


        FileInputStream fs = new FileInputStream("D:/app/sweta/Book1.xls");
        Workbook wb = Workbook.getWorkbook(fs);
        Sheet sh = wb.getSheet(0);

        Workbook workbook = Workbook.getWorkbook(new File("D:/app/sweta/Book1.xls"));
        WritableWorkbook ww = Workbook.createWorkbook(new File("D:/app/sweta/Book4.xls"),
            workbook);
        WritableSheet ws = ww.getSheet(0);

        Label label = null;


        Test.storedElement = new String[sh.getColumns()][sh.getRows()];
        Test.Element = new String[sh.getColumns()][sh.getRows()];

        int j = 0; // column #0
        int q = 1; // column #1



        WritableCellFormat cellFormat = new WritableCellFormat(ws.getColumnFormat(j));
        cellFormat.setAlignment(Alignment.LEFT);
        cellFormat.setWrap(true);
        System.out.println(sh.getRows());
        for (int i = 1; i < sh.getRows(); i++) {

            Cell cell = sh.getCell(j, i);
            Cell cell1 = sh.getCell(q, i);

            Test.storedElement[j][i] = cell.getContents();
            Test.Element[q][i] = cell1.getContents();
            String elem = Test.storedElement[j][i];
            System.out.println(elem);
            String elem1 = Test.Element[q][i];
            System.out.println(elem1);


            String[] ob = elem1.split("\n");
            String[] ob1 = elem.split("\n");
            int l= ob.length;
         //   String valid = "valid";
        //    String invalid = "invalid";
            String Status = "";
            StringBuffer sb=new StringBuffer();
            for(int s=0;s<l;s++){
                if (ob[s].equals(ob1[s])) {
                    sb.append("valid");
                } else {
                    sb.append("invalid");
                }
                if(s==l-1){

                }else{
                    sb.append("\n");
                }

            }

            Status = sb.toString();

            label = new Label(2, i, Status,cellFormat);
            ws.addCell(label);


        }
        ww.write();
        ww.close();
        workbook.close();
    }
}