7

I trying to export an HTML table named Table that is dynamically binded to ViewData.Model in C#. I have a method called export that is called based on another method's actions. so everything before that is set up.. I just don't know how to export the data to a CSV or Excel file.. So when the I step inside the Export method I don't know what next to do to export the table. Can someone help me

    public void Export(List<data> List)
    {
     //the list is the rows that are checked and need to be exported
       StringWriter sw = new StringWriter();

     //I don't believe any of this syntax is right, but if they have Excel export to excel and if not export to csv  "|" delimeted

   for(int i=0; i<List.Count;i++)
    {
              sw.WriteLine(List[i].ID+ "|" + List[i].Date + "|" + List[i].Description);

    }
    Response.AddHeader("Content-Disposition", "attachment; filename=test.csv");
    Response.ContentType = "application/ms-excel";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
    Response.Write(sw);
    Response.End(); 

    }
5
  • it has been updated with code Commented Jan 9, 2009 at 20:45
  • encode your data from the list in the html table tags. Your rest of the code should work. Commented Jan 10, 2009 at 22:54
  • What do you mean by this? encode your data from the list in the html table tags. Commented Jan 12, 2009 at 22:34
  • Use a foreach and don't capitalize the name of your List<data> List variable! (But do capitalize the name of you class data) Commented Jun 21, 2011 at 17:31
  • check this out: stackoverflow.com/questions/14038811/… Commented Oct 30, 2014 at 3:03

4 Answers 4

15

I don't quite understand the whole "export an HTML table named Table that is dynamically binded to ViewData.Model" so I'll just ignore that and focus on your Export(List<data> list) method. Btw, you never really mentioned what was going wrong and where.

I see you had written "if they have Excel export to excel and if not export to csv" - I would personally just export it as a CSV file in both cases because excel can handle csv files no problem.

So with that in mind, here would be my export method based on your code.

public void Export(List<DataType> list)
{
    StringWriter sw = new StringWriter();

    //First line for column names
    sw.WriteLine("\"ID\",\"Date\",\"Description\"");

    foreach(DataType item in list)
    {
        sw.WriteLine(string.format("\"{0}\",\"{1}\",\"{2}\"",
                                   item.ID,
                                   item.Date,
                                   item.Description));
    }

    Response.AddHeader("Content-Disposition", "attachment; filename=test.csv");
    Response.ContentType = "text/csv";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
    Response.Write(sw);
    Response.End(); 
}
Sign up to request clarification or add additional context in comments.

6 Comments

What if there are commas in your description?
What if there are quotes in your data?
item.Description.Replace("\"", "\"\"")
and if I want to show number as text then add "=" example: =\"{1}\"
If you're exporting to CSV, using github.com/JoshClose/CsvHelper is a no-brainer.
|
2

This is an excellent example, but I think that need a globalization modification.

String ltListSeparator = CultureInfo.CurrentUICulture.TextInfo.ListSeparator;
sw.WriteLine(string.format("{0}" + ltListSeparator + "{1}" + ltListSeparator + "{2}", item.ID, item.Date, item.Description));

Comments

1

I think your controller action method will need to wrap the data items in an html table which you may want to do any way you like, So your html+ data will be stored in a string and then you could do something like below- (its not exacly built for MVC but its easy to modify for it).

        Response.ClearContent();    
        Response.AddHeader("content-disposition", attachment);    
        Response.ContentType = "application/ms-excel";                
        Response.Write(yourDataAndHtmlAsString);
        Response.End();

Comments

0

CSV is a simple format and can be built up easily as a string.

http://en.wikipedia.org/wiki/Comma-separated_values

You could create an excel spreadsheet of what you think the end product should look like, save as CSV, open it in notepad and try and replicate it using a string builder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.