How to export in CSV

lock
push_pin
done
Answered
0

Hi,

I have a script and i want to export my result to an CSV that directly open in Browser (download the file from the browser).

Can you help me to do that ?

Thanks

20 Nov 2019 | 12:00 AM

All Replies (0)

You must create a script which prints it's own HTTP headers. That way you can specify the Content-Type header to be "text/csv", and also add a Content-Disposition header. Some details here: https://stackoverflow.com/questions/393647/response-content-type-as-csv

If it is an internal script, then you can link to it with .../blogic.fcgi?action=doScript&id=...&noContentType=1

And then in your script, you need to start by printing the HTTP headers: 

printLine("Content-Type: text/csv);
printLine("Content-Disposition: attachment;filename=myfilename.csv");
printLine("");
print(Yourstuff);

Or at least something like this. Have not tested myself.

Sverre

20 Nov 2019 | 12:00 AM

Hi Sverre,

here my actual code.. what did you yet change so that it's do ?

Thanks

#setLanguageLevel 3;

String output = "Name;benutzt\n";
String wwRoot = getParserVariable("WwwRoot");

SearchEngine seDocument;
//SQL bauen - Suche alle DocTmpl die nicht gelöscht sind
seDocument.bypassNetServer(true);
seDocument.addField("DocTmpl.DocTmpl_id");
seDocument.addField("DocTmpl.name");
seDocument.addCriteria("DocTmpl.deleted", "OperatorEquals", "0", "And", 0);
seDocument.addOrder("DocTmpl.name", true);
for (seDocument.execute(); !seDocument.eof(); seDocument.next())
{
  //für alle Dokument.. prüfen wieviel diesen benutzt sind
  String sDocTmpl_id = seDocument.getField(0);
  String sDocTmpl_name = seDocument.getField(1);
  SearchEngine seDocumentCount;
  seDocumentCount.bypassNetServer(true);
  seDocument.addField("DocTmplGroupLink.doctmplgrouplink_id");
  seDocumentCount.addCriteria("DocTmplGroupLink.doctmpl_id", "OperatorEquals", sDocTmpl_id, "And", 0);
  Integer sCount = seDocumentCount.countRows();
  output.append(sDocTmpl_name+";"+sCount.toString()+"\n");
}

String encoded = encodeBase64(output.toByteArray());
 
Attachment att;
att.setValue("name", "Dokument export");
att.setValue("contentType", "text/csv");
Integer attId = att.save();
att.saveBase64(encoded);
String export = att.getDownloadUrl(True, False);

String url = "http://crm.akgsoftware.de/"+export;

HTTP h;
h.open(url);
20 Nov 2019 | 12:00 AM

Hi Fabrice,

Less is more :)

There is no need to save the content as an attachment and then download it. You can just print it out directly. Something like this:

#setLanguageLevel 3;

String output = "Name;benutzt\n";

SearchEngine seDocument;
//SQL bauen - Suche alle DocTmpl die nicht gelöscht sind
seDocument.bypassNetServer(true);
seDocument.addField("DocTmpl.DocTmpl_id");
seDocument.addField("DocTmpl.name");
seDocument.addCriteria("DocTmpl.deleted", "OperatorEquals", "0", "And", 0);
seDocument.addOrder("DocTmpl.name", true);
for (seDocument.execute(); !seDocument.eof(); seDocument.next())
{
  //für alle Dokument.. prüfen wieviel diesen benutzt sind
  String sDocTmpl_id = seDocument.getField(0);
  String sDocTmpl_name = seDocument.getField(1);
  SearchEngine seDocumentCount;
  seDocumentCount.bypassNetServer(true);
  seDocument.addField("DocTmplGroupLink.doctmplgrouplink_id");
  seDocumentCount.addCriteria("DocTmplGroupLink.doctmpl_id", "OperatorEquals", sDocTmpl_id, "And", 0);
  Integer sCount = seDocumentCount.countRows();
  output.append(sDocTmpl_name+";"+sCount.toString()+"\n");
}

printLine("Content-Type: text/csv");
printLine("Content-Disposition: attachment;filename=myfilename.csv");
printLine("");
printLine(output);

Remember to call the script with the noContentType parameter in the URL.

Sverre

20 Nov 2019 | 12:00 AM

Add reply