使用apache poi从存在于不同行中的两列中提取公共值

时间:2014-08-13 07:56:00

标签: java excel apache apache-poi

我使用apache poi用java读取和写入excel文件的值。现在假设我有三列和三行,我的值为

{row1, column1} = data1
{row1, column2} = data1
{row2, column1} = data2
{row2, column2} = data4
{row3, column1} = data3
{row3, column2} = data2

现在,我已将所有这些值存储在两个字符串中' a'和' b'分别为第1列和第2列。我需要输出两列中常见的代码:

for(int i=2; i<=rows_count;i++)
    {

        String a = datatable1.getCellData("temp", 1, i );
        //System.out.println("a is " + a);
        String b = datatable1.getCellData("temp", 3, i);
        //System.out.println("b is " + b);

        if(a.equals(b))
        {
        System.out.println(b);
        }

    }

有了这个,我得到输出为&#39; data1&#39;只有但不是&#39; data2&#39;因为它们在不同的行中。任何想法如何解决这个问题。谢谢!

1 个答案:

答案 0 :(得分:0)

我相信您要问的是如何报告在文件的某个部分中出现多次的所有单元格值。假设是这样,最简单的事情可能是

Set<String> alreadySeen = new HashSet<String>();
Set<String> duplicates = new HashSet<String>();
DataFormatter fmt = new DataFormatter();

// Update these as per your requirements
int firstRowToCheck = 0;
int lastRowToCheck = Math.min(3, sheet.getLastRowNum());
int firstColumnToCheck = 0;
int lastColumnToCheck = 1;

// Loop over the rows and cells of interest
for (int rn=firstRowToCheck; rn <= lastRowToCheck; rn++) {
    Row r = sheet.getRow(rn);
    if (r == null) {
       // No cells in this row have any values
    } else {
       for (int cn=firstColumnToCheck; cn <= lastColumnToCheck; cn++) {
           Cell c = row.getCell(cn, Row.RETURN_BLANK_AS_NULL);
           if (c == null) {
               // No value for this cell
           } else {
               String value = fmt.formatCellValue(c);
               if (alreadySeen.contains(value)) {
                  // Duplicate!
                  duplicates.put(value);
               } else {
                  // First time this has been seen
                  alreadySeen.put(value);
               }
           }
       }
    }
}

// Report duplicates
for (String dup : duplicates) {
    System.out.println(dup);
}