The code snippet helps to export data from a jsp page which has fetched data from SQL Database into CSV format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | private static void writeCsv(FileWriter writer , String [] array ) throws Exception{ try { int sizeOfArr = array.length; int index = 0 ; while (index < sizeOfArr){ writer.append(array[index] == null ? "" : array[index]); if ( index != (sizeOfArr- 1 )){ writer.append( "," ); } else { writer.append( '\n' ); } index++; } } catch (Exception e){ e.printStackTrace(); throw e; } } |