Due to a large amount of requests regarding data extraction from JSF, i have compiled a small java class which you can embed in jDeveloper, that allows you to extract any IteratorBinding to a CSV format file.
The file is named according to the current date/time where the request was made.
This class can be used for the Greek character set as well.
Below follows the complete source code. Enjoy :)
import java.io.IOException; import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Date; import java.util.Locale;
import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.jbo.AttributeDef; import oracle.jbo.AttributeHints; import oracle.jbo.LocaleContext; import oracle.jbo.Row; import oracle.jbo.RowSetIterator;
import oracle.jbo.common.DefLocaleContext;
public class ExportDataClass { public ExportDataClass() { } public void exportToCSV(DCIteratorBinding tableContent) throws IOException {
ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext(); HttpServletResponse response = (HttpServletResponse)ectx.getResponse(); Date now = Calendar.getInstance().getTime(); SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd-hhmmss"); String filename = formatter.format(now.getTime()) + "file.csv"; String label = ""; String strBuffer = "";
//define the encoding of the returned data. Critical for proper Greek Language exported data. String contentType = "text/csv; charset=windows-1253"; try { response.setContentType(contentType); response.setHeader("Content-disposition", "attachment; filename=" + filename); RowSetIterator rsi = tableContent.getRowSetIterator(); String[] attNames = rsi.getRowAtRangeIndex(0).getAttributeNames(); AttributeDef[] attr = tableContent.getAttributeDefs(attNames); for(int i=0; i < attr.length; i++){ AttributeHints hints = attr[i].getUIHelper(); label = hints.getLabel(this.getLocaleContext()); strBuffer = strBuffer + label + ";"; } strBuffer = strBuffer + "\n"; rsi.first(); for (int i = 0; i < rsi.getFetchedRowCount() ; i++) { Row currentRow = rsi.getRowAtRangeIndex(i); Object[] attValues = currentRow.getAttributeValues(); for (int j = 0; j < attValues.length; j++) {
strBuffer = strBuffer + attValues[j] + ";"; } strBuffer = strBuffer + "\n"; } Row[] ArrayRows = rsi.getNextRangeSet() ; while (ArrayRows.length != 0){ for (int i = 0; i < ArrayRows.length; i++) { Row currentRow = ArrayRows[i]; Object[] attVals = currentRow.getAttributeValues( ); for (int j = 0; j < attVals.length; j++){ strBuffer = strBuffer + attVals[j] + ";"; } strBuffer = strBuffer + "\n"; } ArrayRows = rsi.getNextRangeSet() ; } PrintWriter out = response.getWriter(); response.setContentLength(strBuffer.length() + 1); out.write(strBuffer); out.flush(); out.close(); if (response.isCommitted() == false){ response.reset(); } } catch(IOException ex){ ex.printStackTrace(); throw ex; } finally { FacesContext.getCurrentInstance().responseComplete(); } } private LocaleContext getLocaleContext(){ Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); LocaleContext myLocale = new DefLocaleContext(locale); return myLocale; } }
Labels: character set, CSV, Export, Export to CSV, Greek encoding, Java Server Faces, jDeveloper, jsf |