C# 在 asp.net MVC 中导出 HTML 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/429588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:02:37  来源:igfitidea点击:

Export HTML Table in asp.net MVC

c#asp.net-mvc

提问by TStamper

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

我试图导出一个名为 Table 的 HTML 表,该表在 C# 中动态绑定到 ViewData.Model。我有一个名为 export 的方法,它是根据另一个方法的操作调用的。所以在这之前的一切都设置好了..我只是不知道如何将数据导出到 CSV 或 Excel 文件..所以当我进入导出方法时我不知道下一步要做什么来导出表. 有人能帮我吗

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

    }

采纳答案by Charlino

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.

我不太明白整个“导出一个名为 Table 动态绑定到 ViewData.Model 的 HTML 表”,所以我将忽略它并专注于您的Export(List<data> list)方法。顺便说一句,你从来没有真正提到过哪里出了问题。

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

回答by Iain Holder

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,在记事本中打开它,然后尝试使用字符串生成器复制它。

回答by Perpetualcoder

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+ 数据将存储在一个字符串中,然后您可以执行以下操作-(它不是专为 MVC 构建,但易于修改)。

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

回答by Charlino

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