Writing out from a Java Server Faces table to a CSV….

Imagine if you will that you have a Java Server Faces table and you want to write it out to a CSV file.

1. Put The following code in your application bean.

public String OutputFromTableToCSV(
String sFilename,
TableRowGroup tableRowGroup1,
CachedRowSetDataProvider tblzonesDataProvider) {
// Create an ooutput text file
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object

try {
out = new FileOutputStream(sFilename
+".txt");

p=new PrintStream(out);
} catch (Exception e) {
return "Error, could not write to "+sFilename+".txt";
}

String sFields = "";
String sColumnTitle = "";
String sThisFieldName="";
List theseColumns = tableRowGroup1.getChildren();
// Columns
for (int i = 0; i < tableRowGroup1.getColumnCount(); ++i) {
sColumnTitle = sColumnTitle.concat(sColumnTitle);
sColumnTitle = sColumnTitle.concat(",");

// Get the SQL mapped fields
TableColumn thisComponant = (TableColumn) theseColumns.get(i);
try {
sColumnTitle = sColumnTitle.concat(
thisComponant.getHeaderText());
} catch (Exception e) {
}
}
if (!sColumnTitle.equals("")) {
sColumnTitle=sColumnTitle.substring(1); // Chop off initial ,
}
// Write it out
p.println(sColumnTitle);

RowKey[] TheseRows = tblzonesDataProvider.getAllRows();
for (int rowCount = 0; rowCount < TheseRows.length; ++rowCount) {
// Now do the rows
tblzonesDataProvider.setCursorRow(TheseRows[rowCount]);
for (int i = 0; i < tableRowGroup1.getColumnCount(); ++i) {
sFields = sFields.concat(sFields);
sFields = sFields.concat(",");
// Get the SQL mapped fields
TableColumn thisColumn = (TableColumn) theseColumns.get(i);
UIComponent thisChild=(UIComponent) thisColumn.getChildren().get(0);
// ",[#{currentRow.value['ZoneDescription']}]"
try {
sThisFieldName = thisChild.getValueExpression("text").toString();
sThisFieldName = sThisFieldName.substring(
sThisFieldName.indexOf("['") + 2);
sThisFieldName = sThisFieldName.substring(0,
sThisFieldName.indexOf("']"));

} catch (Exception e) {
}

// Add the field to the string to write out

sFields = sFields.concat(
tblzonesDataProvider.getValue(sThisFieldName).toString()
);

}
if (!sFields.equals("")) {
sFields = sFields.substring(1); // Chop off initial ,
}
p.println(sFields);

sFields="";
} // Next Row
// At this point we have to csv strings
// sfields and column title

// Now we have a comma delimited string called sresult in theory

p.close();
return "OK";
}

2. On the form with the JSF page.
a. Add the binding attribute to the tableRowGroup (in the left hand bottom pane).

Create an “Export to CSV file” button and in the event when clicked add this code:-

thefilename – the filename you want to write to
tableRowGroup1 – The table row group from your JSF page table
yourdataprovider – The data provider that feeds the JSF table

getApplicationBean1().OutputFromTableToCSV("thefilename", tableRowGroup1, yourDataProvider);
Advertisements

About Paul C

I am a Senior Java programmer/Web Technologies, Play in a band called Mandolin Monday, I have a cat and I live on a boat.

Posted on July 25, 2008, in Howto and tagged , , , . Bookmark the permalink. 9 Comments.

  1. Very cool idea, thanks. Have you ever added a button or image to a netbeans faces table cell. I created a Object Array [][] data provider that I use to populate the table and it works fine as long as I work with strings. If I try to make one of the columns objects like labels, buttons, or images, the table only renders a long string com.something.something.something@numbers???

  2. Ah yes – you may need to do a getText().toString() on those.

  3. Ok I need help one more time. I think everything is working now except ….. When I click the button the code seems to parse the table data and generate the string but I am never prompted to download and save the file. What makes the button deliver the file?

    This is the action on my button:

    return getApplicationBean1().OutputFromTableToCSV(“myfilename”, tableRowGroup1, myDataProvider);

  4. Jonnadula Nanaji

    Great Work.

  5. Hi,

    I’m trying to run your code, but i have a compilation error in the following line:
    thisChild.getValueExpression(“text”).toString();
    The error is:
    The type javax.el.ValueExpression cannot be resolved. It is indirectly referenced from required .class files

    I’m using Eclipse 3.4.1 with JBOSS v4.2

    Can you help me?

    Thank you,
    Best Regards

  6. I’m using the following imports:

    import java.awt.List;
    import java.io.FileOutputStream;
    import java.io.PrintStream;

    import javax.faces.component.UIComponent;
    import javax.swing.table.TableColumn;

    I am using JBOSS richfaces 3.3.0

    Thank you
    Best Regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: