简体   繁体   English

将地图列表写入CSV

[英]Write List of Maps to a CSV

I have a standalone application, which connects to a SQL database and saves ResultSet in a list of Map. 我有一个独立的应用程序,该应用程序连接到SQL数据库并将ResultSet保存在Map列表中。 This is what I have so far: 这是我到目前为止的内容:

List<Map<String, Object>> rows;
stmt = conn.createStatement();
Resultset rs = stmt.executeQuery(queryString);
ResultSetMetaData rsmd; //Properties of Resultset object and column count

while(rs.next){
  Map<String, Object> rowResult = new HashMap<String, Object>(columnCount);
   for(int i =1; i <=columnCount; i++){
     rowResult.put(rsmd.getColumnName(i), rs.getObject(i));
   }
  rows.add(rowResult);
}

//WRITE TO CSV  
String csv = "C:\\Temp\\data.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));

//Write the record to file
writer.writeNext(rows);

//close the writer
writer.close();

How do I add this "rows" of List to a csv with columns? 如何将列表的“行”添加到带有列的csv中? Any clues and suggestions. 任何线索和建议。 Your help is appreciated. 感谢您的帮助。

Since every record will have the same columns in the same order, then I would just use a List<List<Object>> for the rows. 由于每条记录将以相同的顺序具有相同的列,因此我只对行使用List<List<Object>>

For the headers, you don't need to get them on every row. 对于标题,您不需要在每一行上都获取它们。 Just get them once like so: 只需一次获得它们,如下所示:

List<String> headers = new ArrayList<>();
for (int i = 1; i <= columnCount; i++ )
{
    String colName = rsmd.getColumnName(i);
    headers.add(colName);
}

Next, you can get the rows like this: 接下来,您可以获取以下行:

List<List<Object>> rows = new ArrayList<>();

while(rs != null && rs.next)
{
   List<Object> row = new ArrayList<>();
   for(int i =1; i <=columnCount; i++)
   {
       row.add(rs.getObject(i));
   }
   rows.add(row);
}

Finally, to create the CSV file, you can do this: 最后,要创建CSV文件,您可以执行以下操作:

// create the CSVWriter
String csv = "C:\\Temp\\data.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));

// write the header line
for (String colName : headers)
{
    writer.write(colName);
}
writer.endRecord();

// write the data records
for (List<Object> row : rows)
{
    for (Object o : row)
    {
        // handle nulls how you wish here
        String val = (o == null) ? "null" : o.toString();
        writer.write(val);
    }
    writer.endRecord();
}

// you should close the CSVWriter in a finally block or use a 
// try-with-resources Statement
writer.close;

Note : In my code examples, I'm using Type Inference 注意 :在我的代码示例中,我正在使用类型推断

See : Try-With-Resources Statement . 请参阅Try-With-Resources语句

Honestly for what you are trying to do I would recommend you use the writeAll method in CSVWriter and pass in the ResultSet . 老实说,我建议您在CSVWriter中使用writeAll方法并传递ResultSet

writer.writeAll(rs, true);

The second parameter is the include column names so the first row in your csv file will be the column names. 第二个参数是include列名,因此csv文件中的第一行将是列名。 Then when you read the file you can translate that back into your Map if you want to (though it will be all strings unless you know when you are reading it what the types are). 然后,当您读取文件时,可以根据需要将其转换回Map中(尽管它将是所有字符串,除非您在读取文件时知道类型是什么)。

Hope that helps. 希望能有所帮助。

Scott :) 斯科特:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM