简体   繁体   English

在asp.net MVC中导出HTML表

[英]Export HTML Table in asp.net MVC

I trying to export an HTML table named Table that is dynamically binded to ViewData.Model in C#. 我试图导出一个名为Table的HTML表,该表被动态绑定到C#中的ViewData.Model。 I have a method called export that is called based on another method's actions. 我有一个名为export的方法,它根据另一个方法的动作调用。 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. 所以在此之前的所有内容都已设置好。我只是不知道如何将数据导出到CSV或Excel文件中。所以当我进入导出方法时,我不知道接下来要做什么来导出表格。 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(); 

    }

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. 我不太了解整个“导出一个名为Table的HTML表,它被动态绑定到ViewData.Model”,因此我将忽略它并专注于您的Export(List <data> list)方法。 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. 我看到你写过“如果他们有Excel导出到excel,如果不导出到csv” - 我个人只是将它导出为CSV文件,因为excel可以处理csv文件没问题。

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(); 
}

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));

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). 我认为你的控制器动作方法需要将数据项包装在一个html表中,你可能想以你喜欢的方式做任何事情,所以你的html +数据将存储在一个字符串中然后你可以做类似下面的事情 - (它不是exacly为MVC构建,但它很容易修改它)。

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

CSV is a simple format and can be built up easily as a string. CSV是一种简单的格式,可以作为字符串轻松构建。

http://en.wikipedia.org/wiki/Comma-separated_values 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. 您可以创建一个excel电子表格,其中包含您认为最终产品的外观,保存为CSV,在记事本中打开并尝试使用字符串生成器进行复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM