Developer forum

Forum » CMS - Standard features » Export Data Publishing to CSV via front-end

Export Data Publishing to CSV via front-end

António Ramos
Reply

Hi guys,

We have a client that want to export the actual view from data publishing to csv. There is any way (without razor code) to export and download the list?

Thanks in advance,

Best regards,

António Ramos


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Antonio

You can make a publishing in datalist module and add a XSLT that transforms the XML til CSV - see dump. That requires a XSLT though, see this:

http://stackoverflow.com/questions/365312/xml-to-csv-using-xslt

BR Nicolai

Capture.PNG
 
Nicolai Høeg Pedersen
Reply

You can also publish the view using the datalist module, and create a simple template that creates a CSV. Attached a template for product catalog that creates a CSV - the same concept you should use in datalists.

 
António Ramos
Reply

Hi Nicolai,

Thanks for your reply. 

I made this approach (works like i wanted) using your last concept with the datalist module and the razor template that will generate and export the csv file to the user. 

Here are the razor code:

@functions{
    void writeFormatted(StreamWriter writer, string str_columnText)
    {
      writer.Write("\"");
      writer.Write(str_columnText.Replace("\"", "\"\"").Replace("\r\n",""));
      writer.Write("\",");
    }
  }

var response = HttpContext.Current.Response;

    response.ContentType = "text/csv";
    response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.csv",DateTime.Now.ToString()));
    response.AddHeader("Pragma", "must-revalidate");
    response.AddHeader("Cache-Control", "must-revalidate");


    var outputStream = response.OutputStream;

    using (var memoryStream = new MemoryStream())
    {
        var stream = new StreamWriter(memoryStream,Encoding.Default);

        foreach (LoopItem rows in GetLoop("Rows")){
          foreach (LoopItem row in rows.GetLoop("Row"))
          {
              writeFormatted(stream,row.GetString("Row.Value"));
          }
            stream.WriteLine();
        }

        stream.Flush();
        outputStream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
    }

 
Nicolai Høeg Pedersen
Reply

Great - thanks for sharing!

 

You must be logged in to post in the forum