比较一个有两个词的excel的两个string并相应地写

我想比较两个colums和excel,并把结果写在第三列。

input文件如下所示 在这里输入图像描述

输出文件应该是这样的

在这里输入图像说明

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

import java.util.List; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; 
 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(); } } } } 

我想比较两列和excel,并在第三列写结果。 这里单元格包含两个单词,我需要写两个单词的状态,请帮助我这样做

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

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

你甚至debugging过你的代码吗? 你应该使用equals来比较Strings

所以replaceif (ob[k]==ob1[k]){ if (ob[k].equals(ob1[k])){

(我没有真正testing你的代码,但这可能是主要问题)

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