[% USE CSV %]
[% CSV.dump(headings) %]

[% FOREACH row IN results %]
  [% mylist = [] %]
  [% FOREACH col IN columns %]
    [% cell = "" %]
    [% FOREACH record IN row.item(col) %]
      [% cell = cell _ record %]
      [% LAST IF loop.last %]
      [% cell = cell _ "\r\n" %]
    [% END %]
    [% mylist.push(cell) %]
  [% END %]
  [% CSV.dump(mylist) %]

[% END %]
