Writing List as CSV

import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanWriter;
import org.supercsv.io.ICsvBeanWriter;
import org.supercsv.prefs.CsvPreference;


private static void writeWithCsvBeanWriter(
ArrayList<ExportScreenDTO> lstExport) throws Exception {
String currentTime = new SimpleDateFormat("ddMMyy_HHmmss")
.format(new Date());

String newFileName = currentTime + "_DashboardExport.csv";
ICsvBeanWriter beanWriter = null;
try {
beanWriter = new CsvBeanWriter(new FileWriter(
"D:\\Shoppers_Dashboard\\" + newFileName),
CsvPreference.STANDARD_PREFERENCE);
// the header elements are used to map the bean values to each
// column (names must match)
final String[] header = new String[] { "storeId",
"masterInterface", "group", "startTime", "endTime",
"totalTime", "finalStatus", "staging", "oraposPull",
"oraposProcessing", "dataDifference" };
final CellProcessor[] processors = getProcessors();
// write the header
beanWriter.writeHeader(header);
// write the beans
for (final ExportScreenDTO exportData : lstExport) {
beanWriter.write(exportData, header, processors);
}
} finally {
if (beanWriter != null) {
beanWriter.close();
}
}
}
private static CellProcessor[] getProcessors() {
final CellProcessor[] processors = new CellProcessor[] {
new Optional(), new Optional(), new Optional(), new Optional(),
new Optional(), new Optional(), new Optional(), new Optional(),
new Optional(), new Optional(), new Optional() };
return processors;
}

Comments

Popular posts from this blog

Conversion from Clob To String in java and Vice versa

Portable Java 8/ JDK8 setup without admin rights

Multi Threading , Producer consumer problem in Java