WorkbookDesigner mvc里面返回file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace test.Controllers
{
    using System.Data;
    using System.IO;
    using Aspose.Cells;


    public class ExcelController : Controller
    {
        public FileResult excels()
        {
            DataTable dt = new DataTable();
            dt.TableName = "table1";
            dt.Columns.Add(new DataColumn("ROW1", typeof(string)));
            dt.Columns.Add(new DataColumn("ROW12", typeof(string)));
            DataRow dr;
            for (int i = 0; i <= 10; i++)
            {
                dr = dt.NewRow();
                dr["ROW1"] = Guid.NewGuid().ToString();
                dr["ROW12"] = Guid.NewGuid().ToString();
                dt.Rows.Add(dr);
            }

            WorkbookDesigner designer = new WorkbookDesigner();
            string path = Server.MapPath("/Templete/DDD.xls");
            designer.Workbook.Open(path);
            designer.SetDataSource(dt);
            designer.Process();

            //将文件存在服务器端,名称是123.xls
            designer.Save(Server.MapPath("/Templete/123.xls"), FileFormatType.Default);

            //将服务器端的文件/Templete/123.xls下载到客户端
            return File(Server.MapPath("/Templete/123.xls"), "application/ms-excel", "dsds.xls");

            //将流文件写到客户端流的形式写到客户端,名称是_report.xls
           //designer.Save("_report.xls", SaveType.OpenInExcel, FileFormatType.Excel2003, System.Web.HttpContext.Current.Response);
           // Response.Flush();
           //Response.Close();
          //designer = null;
           // Response.End();
           // return View("getexcel");

        }

 

http://ming2882code.googlecode.com/svn-history/r279/trunk/ext3mvc/Web/Controllers/ReportController.cs

posted @ 2013-12-02 18:16  红萝卜  阅读(1441)  评论(0编辑  收藏  举报