开发者

Download different table columns in csv format using mvc3

I want to download the different column data from the sql server using 开发者_开发问答mvc3, For instance I want to download 5 columns from table account where id = 1, and 2 columns from products where id =1. Can any one provide me with some tutorial or any links to achieve this . Thanks


you can do something like

public ActionResult ExportCsv(int page, string orderBy, string filter)
        {
            IEnumerable orders = GetOrders().AsQueryable().ToGridModel(page, 10, orderBy, string.Empty, filter).Data;
            MemoryStream output = new MemoryStream();
            StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
            writer.Write("OrderID,");
            writer.Write("ContactName,");
            writer.Write("ShipAddress,");
            writer.Write("OrderDate");
            writer.WriteLine();
            foreach (Order order in orders)
            {
                writer.Write(order.OrderID);
                writer.Write(",");
                writer.Write("\"");
                writer.Write(order.Customer.ContactName);
                writer.Write("\"");
                writer.Write(",");
                writer.Write("\"");
                writer.Write(order.ShipAddress);
                writer.Write("\"");
                writer.Write(",");
                writer.Write(order.OrderDate.Value.ToShortDateString());
                writer.WriteLine();
            }
            writer.Flush();
            output.Position = 0;

            return File(output, "text/comma-separated-values", "Orders.csv");
        }

For further detail visit Telerik Demos. you do not need to use telerik to implement this feature. just write actionresult, fetch data transform it to csv, call it from your browser and you are done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜