function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jasondoddsjasondodds 

export CSV or <table> content to VF page to render as Excel using JavaScript

I am trying to implement the following functionality:

 

I have a dashboard on a VF page that I need to export to Excel(.xls(x) or .csv).  The Dashboard itself is not one continuous <table>, so I am creating an output stream via JavaScript that will create a cohesive <table> or formatted CSV to be passed to an apex page with contenttype="application/vnd.ms-excel" to be downloaded as a file.  I know I need the server to generate the contenttype so the data downloads as a file, so I just need to populate the page with my passed-in output stream.

 

My question is, what mechanism do I use to get my JS data (either csv or html) to the VF export page to render?  I have tried actionFunctions and JS Remoting with no success.  I thought I would throw it out to the developer community to get input on which is the correct path to take (if it is even possible) so I could concentrate my efforts.  

 

Thanks,

 

Jason

sfdcfoxsfdcfox

How large is the expected data? If small (a few KB) you could pass it in through a URL. Other than that, you have a few options:

 

1) Use the same controller on both pages. When you invoke the second page, your view state from the first will automatically transfer over, and the file will render as you expect.

 

2) Save the data to an attachment or document, then pass the document ID to the second page. It can read this, translate it back into a normal string, and output the results.

 

3) You can probably use the new HTML 5 "file" specification to allow the data to be saved directly by the user (assuming they accept, of course). Browser support for this feature is fairly widespread.

 

I'm sure there's other ways you do do this, too. Actually, if you save the file to a Document, you could actually just redirect them to the standard download link for Documents.

jasondoddsjasondodds

Thanks for the response.  My data set in't well suited to pass in a URL (big chunk of CSV or HTML).  Here are my comments on each approach:

 

1) Use the same controller on both pages. When you invoke the second page, your view state from the first will automatically transfer over, and the file will render as you expect.  That is the main issue I am having.  My data is in a JS variable, and isn't in the view state of the requesting page.  I use the same controller for both pages, and I am currently trying to pass the data into an actionFunction with an <apex:param assignTo="{!myOutputVar}">, but the data is not coming over to the controller to the export page.

 

2) Save the data to an attachment or document, then pass the document ID to the second page. It can read this, translate it back into a normal string, and output the results.  I haven't tried this yet, but this is probably my last resort.  I don't have any experience writing to files in SF, so could you delete the document after you rendered to the export page so I don't have a big bin of temp export files?

 

3) You can probably use the new HTML 5 "file" specification to allow the data to be saved directly by the user (assuming they accept, of course). Browser support for this feature is fairly widespread.  I think at some point our app will need to be certified for IE9 or above, which doesn't support the File API.  Using native HTML5 functionality is my preferred way to handle this, but I don't think it is an option.