I also couldn't find a node plugin that wrote CSV files, but there's not
much to it (open any CSV in a text editor you can see how simple it is).
So write quick function to surround your values with quotes, escape the
inner quotes, and put each row on a new line ("\r\n"). Then write or
stream your rows to the download.
-Brandon
On Wednesday, June 20, 2012 7:42:44 AM UTC-4, ravi prakash wrote:
>
> Hi Andrew,
>
> You really solved my problem.
> One more help, I went through ya-csv module and was able to upload
> and parse csv file.
> But i did not understand how i could create a csv using ya-csv
> module.
> Can you please provide me sample code to create csv using ya-csv.
> Once again thanks for sharing information.
>
> - Ravi
>
>
> On Jun 20, 11:43 am, Andrew Stone <[email protected]> wrote:
> > Hi Ravi,
> >
> > Your for loop look fine, but I believe you messed up the headers.
> > Don't capitalize them. Also, the 'content-disposition' header is a bit
> > of an anomaly. It just doesn't look right to most programmer's eyes,
> > and I've screwed it up also resulting in no file download. It cannot
> > have any (unescaped) quote characters within the value. It is a a
> > single string like this: 'attachment; filename=sample.csv', not
> > 'attachment; 'filename=sample.csv'.
> >
> > The easy way to remember this is that HTTP headers are just key value
> > pairs consisting of strings. Thus the value of all of them must be a
> > single string (potentially with escaped characters - not sure about
> > that). Fixing the headers should fix your problems. Also, the headers
> > must be set before the calls to res.write because they have to be
> > written first to the client (hence the term header).
> >
> > Finally, since you already have the array in memory, writing it in
> > multiple chunks won't make a difference. It will just get buffered by
> > the OS, especially since it's all being written in a synchronous for
> > loop. The real reason to use streaming is so you can limit your
> > runtime memory consumption. This is typically achieved by receiving
> > some data from the db, writing it to the socket, then fetching more
> > from the db, etc... That way you can keep a constant memory load on
> > the server and not have to worry about really large stuff building up
> > in memory. That being said, your for loop is absolutely correct, and
> > the right way to do this in your circumstances. I just wanted to give
> > you a bit more background.
> >
> > Hope that helps,
> > Andrew
> >
> >
> >
> >
> >
> >
> >
> > On Wed, Jun 20, 2012 at 2:24 AM, ravi prakash <[email protected]>
> wrote:
> >
> > > Hi Andrew,
> >
> > > Thanks for your quick response. I tried your first option.
> > > Say, i have data from database in array called results,
> >
> > > for(var i = 0; i < results.length; i++) {
> >
> > > body = '';
> > > body = results[i]+'\n';
> > > res.write(body);
> > > }
> > > res.setHeader('Content-disposition', 'attachment;
> > > filename='sample.csv');
> > > res.setHeader('Content-type', 'text/csv');
> > > res.end('');
> >
> > > I want to write data in chunks since my array is very big. I used
> > > res.write() for this. By doing this my response is written directly to
> > > browser
> > > and file download option is not asked. How can i solve this?
> >
> > > -Ravi
> >
> > > On Jun 20, 10:40 am, Andrew Stone <[email protected]> wrote:
> > >> Hi Ravi,
> >
> > >> Since you already have the csv content generated you just want to
> send
> > >> it out with res.write() or res.end() depending upon whether you are
> > >> writing it out in chunks or not (streaming).
> >
> > >> To get the CSV to be a downloadable file you need to add the
> > >> 'content-type' and 'content-disposition' headers to your response:
> > >> res.header('content-type','text/csv');
> > >> res.header('content-disposition', 'attachment;
> filename=report.csv');
> >
> > >> For more complex stuff, ya-csv looks useful.
> >
> > >> -Andrew
> >
> > >> On Wed, Jun 20, 2012 at 1:47 AM, Matthew Vickers <
> [email protected]> wrote:
> > >> > I have used ya-csv in the past, it works well for our basic usage.
> >
> > >> >https://github.com/koles/ya-csv
> >
> > >> > Matt
> >
> > >> > ----- "ravi prakash" <[email protected]> wrote:
> >
> > >> >> Hi group,
> >
> > >> >> How can i generate a excel/csv file from nodejs application. Say,
> I
> > >> >> have a link in webpage and on click of link, i query database and
> put
> > >> >> result into excel/csv file. Finally the file download must be
> > >> >> opened.
> > >> >> I am building application using nodejs+express.
> >
> > >> >> My goal is to export data into a excel/csv file ie generate
> excel/csv
> > >> >> file from my application.
> >
> > >> >> Say i have link in webpage -----
> >
> > >> >> <a href='/report'>Export</a>
> >
> > >> >> Server side i have code ------
> >
> > >> >> app.get('/report', function(req, res) {
> >
> > >> >> //get data from db
> >
> > >> >> How to put data to csv/excel file ???
> >
> > >> >> }
> >
> > >> >> --
> > >> >> Job Board:http://jobs.nodejs.org/
> > >> >> Posting guidelines:
> > >> >>https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> > >> >> You received this message because you are subscribed to the Google
> > >> >> Groups "nodejs" group.
> > >> >> To post to this group, send email to [email protected]
> > >> >> To unsubscribe from this group, send email to
> > >> >> [email protected]
> > >> >> For more options, visit this group at
> > >> >>http://groups.google.com/group/nodejs?hl=en?hl=en
> >
> > >> > --
> > >> > Job Board:http://jobs.nodejs.org/
> > >> > Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> > >> > You received this message because you are subscribed to the Google
> > >> > Groups "nodejs" group.
> > >> > To post to this group, send email to [email protected]
> > >> > To unsubscribe from this group, send email to
> > >> > [email protected]
> > >> > For more options, visit this group at
> > >> >http://groups.google.com/group/nodejs?hl=en?hl=en
> >
> > > --
> > > Job Board:http://jobs.nodejs.org/
> > > Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> > > You received this message because you are subscribed to the Google
> > > Groups "nodejs" group.
> > > To post to this group, send email to [email protected]
> > > To unsubscribe from this group, send email to
> > > [email protected]
> > > For more options, visit this group at
> > >http://groups.google.com/group/nodejs?hl=en?hl=en
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en