Developer forum

Forum » Templates » Response or pageview as CSV download

Response or pageview as CSV download

Finn Frost
Reply

Hi all,

I would like to dynamicly create a downloadable .csv file when visiting a page.
But it seems that no matter what i do, it seems my encoding is wrong, or at least i think it is the encoding that is the problem.
This is my code in the pagetemplate:

@using System.Web
@using System.Text
@{
    string attachment = "attachment; filename=kampagne_data.csv";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.ClearContent();
    
    HttpContext.Current.Response.ContentType = "text/csv";
    HttpContext.Current.Response.Charset = Encoding.UTF8.WebName;
    HttpContext.Current.Response.AddHeader("Content-type", "application/vnd.ms-excel");
    HttpContext.Current.Response.AddHeader("Content-Disposition", attachment);
    HttpContext.Current.Response.AddHeader("Pragma", "must-revalidate");
    HttpContext.Current.Response.AddHeader("Cache-Control", "must-revalidate");
    HttpContext.Current.Response.Write("Test;Test2;Test3;æøåÆØÅ");
    HttpContext.Current.ApplicationInstance.CompleteRequest(); // instead of Response.End()
}

To be able to use the template as a pagetemplate i have the below code in the bottom of the template (seems the masterpage tag is required):

@if(1 == 2) {
<div title="Indhold" id="campaign-content-container" class="col-xs-12 dwcontent"></div>
}<!--@MasterPageFile(Lead_Export_Master.cshtml)-->
<!--@Layout.Title(Kampagne export)-->

Any help how i get the data exported correctly is much appreciated, and not looking like weird characters.

 


Replies

 
Mikkel Ricky
Reply
This post has been marked as an answer

Try using a layout template like this:

@using System.Web
@using System.Text
@{
    var response = HttpContext.Current.Response;
    
    response.ContentType = "text/csv";
    response.AddHeader("Content-Disposition", "attachment; filename=kampagne_data.csv");
    response.AddHeader("Pragma", "must-revalidate");
    response.AddHeader("Cache-Control", "must-revalidate");
    response.Write("Test;Test2;Test3;æøåÆØÅ\n");
}
@* Remove some stuff injected by Dynamicweb @GetValue("Stylesheets") @GetValue("Javascripts") *@

If you save this as Forum_thread_39558.clean inside your design folder, you can apply the template to any page by adding LayoutTemplate=Forum_thread_39558.clean.cshtml to the url, e.g.

/Default.aspx?Id=7&LayoutTemplate=Forum_thread_39558.clean.cshtml

Best regards,
Mikkel

Votes for this answer: 1
 
Finn Frost
Reply

Hi Mikkel,

Thanks, that seemed to do most of the trick. However, DW still appends the below line in the bottom of the response - regardless if I end the response my self. Is there any way to avoid this line?:

<!-- Exe time: 0,1093743 :   < >  <PageID (68)>  <Designs/Website/_parsed/Forum_thread_39558.clean.parsed.cshtml>  -->

Also, any tips on setting the correct encoding, so it supports: æøåÆØÅ ? - Perhaps encode content before writing to response output?

 
Mikkel Ricky
Reply
This post has been marked as an answer

To get rid of that html comment, you should check "Disable performance comment" in Solution settings in the Management center.

Make sure that you actually save your template in the right encoding, i.e. UTF-8, to make exotic characters like æ, ø and å appear correctly. I suppose you'll not hardcode your data into the template, but read it from some data source?

Best regards,
Mikkel

Votes for this answer: 1
 
Finn Frost
Reply

Ok - i'll try and disable the performance comment.

Regarding my data, then you're correct, data is not hardcoded, but from at datasource - data from DW database table.
My template is created in/from DW - so i'll try create it on my desktop and ensure it UTF8 before i upload it, and see if that helps my encoding issue.
 

 
Finn Frost
Reply

Hi again,

Now i've had a chance continue and try solve my encoding issue. After further investigation my issue wasn't really an encoding "issue", since when opening the .csv file in notepad or notepad++, characters was displayed correctly - all æøå and ÆØÅ displayed nicely.

However opening the file is MS Excel the resulted ÆØÅ being: ÆØÅ and æøå: æøå So my real issue seem to be with Excel - making it understand that the file is actually in the UTF-8 encoding.

After a lot of messing around with various encoding conversion, i finally found a solution that worked for me. I had to prepend the Preamble(C# - System.Text.Encoding.GetPreamble()) to the beginning of the file, whish is, as far as i understood is the BOM (byte order mark) that specifies (for decoders - such as readers of a file) which encoding is used in the file. This is my solution:

var sb = new System.Text.StringBuilder("test;Test;ÆØÅ;æøå"); // I have omitted my dataextraction part, since this is not relevant for this.
var data = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
var result = System.TextEncoding.UTF8.GetPreamble().Concat(data).ToArray();
response.OutputStream.Write(result, 0, result.Length); // or response.BinaryWrite(result);

 

I had on other issue with Excel, which complained about issues with the file - Excel thought my .csv file was a SYLK file, and the reason for this was that my first 2 letters/chars in the file was capital ID - lowercasing the letters/header solved the issue. See this link to the microsoft article regarding this.

 

And - removing the checkbox for including the performance comment worked, i now got at much cleaner file. Hence the suggestion marked as answer.

 

You must be logged in to post in the forum