如何将记录显示为列?

时间:2010-07-06 23:10:35

标签: java csv

我创建了csv文件。记录显示行顺序。但我需要coloumn order。

如何修改代码?

for(int i = 0;i < maps.length;i++) {    
    Map<String, String> map = (Map<String, String>)maps[i];
    sep = "";
    for(int j = 0;j < labels.length;j++) {  
        String val = map.get(labels[i]);
        out.write(sep);
        out.write(""+val);
        sep = ",";
    }
    out.write(LINEFEED);
}

1 个答案:

答案 0 :(得分:0)

首先,你应该使用新式循环,它们更清晰,更漂亮:

for(Map<String, String> map : maps) {    
    sep = "";
    for(String label: labels){  
        out.write(sep);
        out.write(map.get(label));
        sep = ",";
    }
    out.write(LINEFEED);
}

然后,你只需要切换内循环和外循环

for(String label: labels){  
    sep = "";
    for(Map<String, String> map : maps) {    
        out.write(sep);
        out.write(map.get(label));
        sep = ",";
    }
    out.write(LINEFEED);
}

应该从行切换到列