从导出XLS文件到CSV文件[C# - ASP.NETmvc3]

时间:2012-09-21 14:10:05

标签: asp.net-mvc-3 razor export-to-excel export-to-csv

我实际上这段代码允许我将视图导出到xls文件:

public ActionResult Export(string searchString, int searchOrder = 0)
{
    var user = from m in db.Orders select m;
    if (!String.IsNullOrEmpty(searchString))
    {
        user = user.Where(s => s.ClientID.Contains(searchString));
    }
    if (searchOrder > 0)
    {
        user = user.Where(c => c.OrderId == searchOrder);
    }
    Response.AddHeader("Content-Type", "application/vnd.ms-excel");
    return this.View(user);
}

我导出的视图看起来像这样:

 <table cellpadding="3" cellspacing="3">
        <tr border="1" bgcolor="#777777" color="#ffffff">       
            <td width="12%" align="center">
                Customer
            </td>
            <td width="15%" align="center">
              Order ID
            </td>
            <td width="30%" align="center">
                Product            </td>
            <td width="10%" align="center">
                Price
            </td>
            <td width="10%" align="center">
                Quantity
            </td>
            <td width="10%" align="center">
                Total
            </td>
            <td width="13%" align="center">
                Date
            </td>
        </tr>   
@foreach (var item in Model)
{          
            <tr border="1" bgcolor="@Odd">
               <td> @Html.DisplayFor(modelItem => item.Username) </td>    
               <td> @Html.DisplayFor(modelItem => item.OrderId) </td>  
                <td>@Html.DisplayFor(modelItem => o.UnitPricePack) </td>
                <td> @Html.DisplayFor(modelItem => item.Total) </td>      
                <td> @Html.DisplayFor(modelItem => item.OrderDate) </td>
            </tr>     
   }     
 </table>

如何以相同的方式导出到csv文件,并保持我的html显示。因为我试图用csv将其导出:

  

Response.AddHeader(“Content-Type”,“text / csv”);

     

Response.AddHeader(“Content-Type”,“application / csv”);

     

Response.AddHeader(“Content-Disposition”,“attachment; filename = report.csv”);

但是在每种情况下,我的视图都显示它是如何的,使用html标签&amp;一切...

感谢您的帮助,答案,提示,链接等等......对不起我的英语,我希望您能理解我的问题。

0 个答案:

没有答案