private static void export(File file, ResultSetReference ref, boolean withHeader) throws IOException, SQLException {
        Exporter exporter = Exporter.getExporter(file);
        try {
            ResultSet rs = ref.getResultSet();
            ColumnOrder order = ref.getOrder();
            boolean needOrderChange = order.size() > 0;
            int columnCount;
            List<String> header = new ArrayList<String>();
            if (needOrderChange) {
                columnCount = order.size();
                for (int i = 0; i < columnCount; i++) {
                    header.add(order.getName(i));
                }
            } else {
                ResultSetMetaData m = rs.getMetaData();
                columnCount = m.getColumnCount();
                for (int i = 0; i < columnCount; i++) {
                    header.add(m.getColumnName(i + 1));
                }
            }
            if (withHeader) {
                exporter.addHeader(header.toArray());
            }
            int count = 0;
            while (rs.next()) {
                ++count;
                Object[] row = new Object[columnCount];
                for (int i = 0; i < columnCount; i++) {
                    int index = (needOrderChange) ? order.getOrder(i) : i + 1;
                    row[i] = rs.getObject(index);
                }
                exporter.addRow(row);
            }
            ref.setRecordCount(count);
        } finally {
            exporter.close();
        }
    }