Java(或Excel)-如何对齐无序的列数据

Java(或Excel)-如何对齐无序的列数据,java,excel,Java,Excel,我有以下数据: a b c d f g h i j a b d e f h i j a b c d e f j k l a b c d e f g h m 我想将其输出(例如,输入Excel),如下所示: a b c d e f g h i j a b d e f h i j a b c d e f j k l a b c d e f g h m 在excel术语中,我想在单元格之间移动,以便

我有以下数据:

    a b c d f g h i j
    a b d e f h i j
    a b c d e f j k l
    a b c d e f g h m
我想将其输出(例如,输入Excel),如下所示:

    a b c d e f g h i j
    a b   d e f   h i j
    a b c d e f       j k l
    a b c d e f g h         m
在excel术语中,我想在单元格之间移动,以便文本在列中匹配

注意:为了简单起见,我使用了字母顺序,但实际上没有这种顺序,但我需要保持原始顺序

更新示例:

    Original Data                                                   
    a   b   c   d   f   g   h   i   j                   
    a   b   d   e   f   h   i   j                       
    a   b   c   d   e   f   j   k   l                   
    a   b   x   d   e   f   g   h   m                   

    Dougs Output                                                    
    a   b   c   d           f   g   h   i   j           
    a   b       d       e   f       h   i   j           
    a   b   c   d       e   f               j   k   l   
    a   b       d   x   e   f   g   h                   m

    My Manual Output (Required) 
    a   b   c       d       f   g   h   i   j           
    a   b           d   e   f       h   i   j           
    a   b   c       d   e   f               j   k   l   
    a   b       x   d   e   f   g   h                   m

上面的x出现在指数2,但d出现在指数2和3,因此x应该在d之前。

我想我知道了。这取决于不能向
集合添加重复值这一事实。诀窍是以正确的顺序解析源数据。我将其解释为向下遍历一列中的每个单元格,然后转到下一列。然后,集合包含在该搜索顺序中找到的每个项目的第一个实例

该代码需要两个工作表,一个包含源工作表,另一个包含输出的目标工作表。在本例中,我使用代码将
wsSource
wsTarget
设置为工作簿中的前两页,但您可以根据需要进行更改

您可以更改
rng
的定义。只要源代码表上没有其他内容,代码就会确定数据的最后一行和最后一列。这是通过包含
Find
的行完成的:

Sub Rearrange()
Dim wsSource As Excel.Worksheet
Dim rng As Excel.Range
Dim i As Long, j As Long
Dim cell As Excel.Range
Dim coll As Collection
Dim wsTarget As Excel.Worksheet
Dim SourceLastRow As Long
Dim SourceLastCol As Long

Set wsSource = ThisWorkbook.Sheets(1)
Set wsTarget = ThisWorkbook.Sheets(2)
Set rng = wsSource.Range("A1:i4")    'change to suit

Set coll = New Collection
SourceLastRow = rng.Cells.Find("*", rng.Cells(1), xlValues, , xlByRows, xlPrevious).Row
SourceLastCol = rng.Cells.Find("*", rng.Cells(1), xlValues, , xlByColumns, xlPrevious).Column
'cycle through the cells in the range down through each column from left to right
For i = 1 To SourceLastCol
    For j = 1 To SourceLastRow
        Set cell = wsSource.Cells(j, i)
        If cell.Value <> "" Then
            On Error Resume Next
            'can only add an item once - no duplicates
            coll.Add cell.Value, cell.Value
            On Error GoTo 0
        End If
    Next j
Next i
'Clear and re-load wsTarget
wsTarget.Cells.Clear
For i = 1 To coll.Count
    For j = 1 To SourceLastRow
        If Application.WorksheetFunction.CountIf(wsSource.Cells(j, 1).EntireRow, coll(i)) = 1 Then
            wsTarget.Cells(j, i) = coll(i)
        End If
    Next j
Next i
End Sub
子重排()
将wsSource设置为Excel.工作表
尺寸为Excel.Range
我和我一样长,我和我一样长
将单元格设置为Excel.Range
Dim coll As系列
将wsTarget设置为Excel.工作表
与最后一行一样长
暗淡的颜色和颜色一样长
设置wsSource=ThisWorkbook.Sheets(1)
Set-wsTarget=ThisWorkbook.Sheets(2)
设置rng=wsSource.Range(“A1:i4”)“更改以适应
Set coll=新集合
SourceLastRow=rng.Cells.Find(“*”,rng.Cells(1),xlValues,xlByRows,xlPrevious).Row
SourceLastCol=rng.Cells.Find(“*”,rng.Cells(1),xlValues,xlByColumns,xlPrevious).Column
'在从左到右的每列中向下循环单元格
对于i=1到SourceLastCol
对于j=1到SourceLastRow
设置单元格=wsSource.Cells(j,i)
如果单元格的.Value为“”,则
出错时继续下一步
'只能添加一次项目-无重复项
coll.Add cell.Value,cell.Value
错误转到0
如果结束
下一个j
接下来我
'清除并重新加载wsTarget
wsTarget.Cells.Clear
对于i=1到coll.Count
对于j=1到SourceLastRow
如果Application.WorksheetFunction.CountIf(wsSource.Cells(j,1.EntireRow,coll(i))=1,则
W目标单元格(j,i)=coll(i)
如果结束
下一个j
接下来我
端接头

我想我知道了。这取决于不能向
集合添加重复值这一事实。诀窍是以正确的顺序解析源数据。我将其解释为向下遍历一列中的每个单元格,然后转到下一列。然后,集合包含在该搜索顺序中找到的每个项目的第一个实例

该代码需要两个工作表,一个包含源工作表,另一个包含输出的目标工作表。在本例中,我使用代码将
wsSource
wsTarget
设置为工作簿中的前两页,但您可以根据需要进行更改

您可以更改
rng
的定义。只要源代码表上没有其他内容,代码就会确定数据的最后一行和最后一列。这是通过包含
Find
的行完成的:

Sub Rearrange()
Dim wsSource As Excel.Worksheet
Dim rng As Excel.Range
Dim i As Long, j As Long
Dim cell As Excel.Range
Dim coll As Collection
Dim wsTarget As Excel.Worksheet
Dim SourceLastRow As Long
Dim SourceLastCol As Long

Set wsSource = ThisWorkbook.Sheets(1)
Set wsTarget = ThisWorkbook.Sheets(2)
Set rng = wsSource.Range("A1:i4")    'change to suit

Set coll = New Collection
SourceLastRow = rng.Cells.Find("*", rng.Cells(1), xlValues, , xlByRows, xlPrevious).Row
SourceLastCol = rng.Cells.Find("*", rng.Cells(1), xlValues, , xlByColumns, xlPrevious).Column
'cycle through the cells in the range down through each column from left to right
For i = 1 To SourceLastCol
    For j = 1 To SourceLastRow
        Set cell = wsSource.Cells(j, i)
        If cell.Value <> "" Then
            On Error Resume Next
            'can only add an item once - no duplicates
            coll.Add cell.Value, cell.Value
            On Error GoTo 0
        End If
    Next j
Next i
'Clear and re-load wsTarget
wsTarget.Cells.Clear
For i = 1 To coll.Count
    For j = 1 To SourceLastRow
        If Application.WorksheetFunction.CountIf(wsSource.Cells(j, 1).EntireRow, coll(i)) = 1 Then
            wsTarget.Cells(j, i) = coll(i)
        End If
    Next j
Next i
End Sub
子重排()
将wsSource设置为Excel.工作表
尺寸为Excel.Range
我和我一样长,我和我一样长
将单元格设置为Excel.Range
Dim coll As系列
将wsTarget设置为Excel.工作表
与最后一行一样长
暗淡的颜色和颜色一样长
设置wsSource=ThisWorkbook.Sheets(1)
Set-wsTarget=ThisWorkbook.Sheets(2)
设置rng=wsSource.Range(“A1:i4”)“更改以适应
Set coll=新集合
SourceLastRow=rng.Cells.Find(“*”,rng.Cells(1),xlValues,xlByRows,xlPrevious).Row
SourceLastCol=rng.Cells.Find(“*”,rng.Cells(1),xlValues,xlByColumns,xlPrevious).Column
'在从左到右的每列中向下循环单元格
对于i=1到SourceLastCol
对于j=1到SourceLastRow
设置单元格=wsSource.Cells(j,i)
如果单元格的.Value为“”,则
出错时继续下一步
'只能添加一次项目-无重复项
coll.Add cell.Value,cell.Value
错误转到0
如果结束
下一个j
接下来我
'清除并重新加载wsTarget
wsTarget.Cells.Clear
对于i=1到coll.Count
对于j=1到SourceLastRow
如果Application.WorksheetFunction.CountIf(wsSource.Cells(j,1.EntireRow,coll(i))=1,则
W目标单元格(j,i)=coll(i)
如果结束
下一个j
接下来我
端接头

我已经用Java解决了这个问题。有一个自定义比较,它查看每个值的最大和最小索引,并对其进行排序。然后我把它们打印到屏幕上

**注意,我的数据在HashMap中,原因在这里没有解释,但它可以很容易地放在一个简单的列表中

**请原谅我没有经验的编码实践

@道格·格兰西如果你能在VB中使用它,那就太棒了

ValueComparator.java

import java.util.Comparator;
import java.util.Map;

public class ValueComparator implements Comparator<String> {

    private Map<String, Integer[]> base;

    public ValueComparator(Map<String, Integer[]> m) {
        this.base = m;
    }

    public int compare(String so1, String so2) {

        // get the max and min indices from each data peice
        Integer[] o1 = base.get(so1);
        Integer[] o2 = base.get(so2);

        // compare their min index first
        if (o1[0] < o2[0]) {
            return -1;
        }
        if (o1[0] == o2[0]) { //if they are the same
            if ( o1[1] < o2[1]) { // then look at the max index
                return -1;
            }
            else {
                return 1;
            }                   
        }
        else { 
            return 1;
        }
    }
}
import java.util.Comparator;
导入java.util.Map;
公共类ValueComparator实现了Comparator{
私人地图库;
公共价值比较表(地图m){
这个基数=m;
}
公共整数比较(字符串so1,字符串so2){
//从每个数据点获取最大和最小索引
整数[]o1=base.get(so1);
整数[]o2=基.get(so2);
//首先比较它们的最小索引
if(o1[0]