javascript - 如何将图表与mvc中的相应数据一起动态导出到excel工作表

标签 javascript c# jquery .net asp.net-mvc

有没有办法将图形导出到具有相应数据的 Excel 工作表。目前我已经创建了一个 MVC 应用程序,单击下载按钮,我能够将数组中存储的数据导出到 Excel 工作表。但是我的主要任务是将动态图表与数据一起导入Excel,并且当修改Excel中的值时,该图表应该动态变化。请帮助我。我只是这方面的初学者,所以...... 这是我的controller.cs代码...

 public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }
    [HttpPost]

    public void  download()
    {
        var data = new[]{ 
                           new{ Name="Ram", Email="ram@techbrij.com", Phone="111-222-3333" },
                           new{ Name="Shyam", Email="shyam@techbrij.com", Phone="159-222-1596" },
                           new{ Name="Mohan", Email="mohan@techbrij.com", Phone="456-222-4569" },
                           new{ Name="Sohan", Email="sohan@techbrij.com", Phone="789-456-3333" },
                           new{ Name="Karan", Email="karan@techbrij.com", Phone="111-222-1234" },
                           new{ Name="Brij", Email="brij@techbrij.com", Phone="111-222-3333" }                       
                  };

        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment;filename=Contact.xls");
        Response.AddHeader("Content-Type", "application/vnd.ms-excel");
        WriteTsv(data, Response.Output);
        Response.End();
        Response.Write ("downloaded");
    }

    public void WriteTsv<T>(IEnumerable<T> data, TextWriter output)
    {
        PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
        foreach (PropertyDescriptor prop in props)
        {
            output.Write(prop.DisplayName); // header
            output.Write("\t");
        }
        output.WriteLine();
        foreach (T item in data)
        {
            foreach (PropertyDescriptor prop in props)
            {
                output.Write(prop.Converter.ConvertToString(
                     prop.GetValue(item)));
                output.Write("\t");
            }
            output.WriteLine();
        }
    }
}

}

在index.cshtml中..

   @{
        ViewBag.Title = "Home Page";
    }

    <!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
    @{using (Html.BeginForm("download","home"))
    {
         <div>
            <button>Download</button>
        </div>
    }}

    </body>
    </html>

最佳答案

我们之前做过的事情是使用 Excel 模板。步骤如下:

  1. 创建一个包含图表的 Excel 文件。它应该引用包含数据的工作表。默认情况下,数据表当然是空白的
  2. 在 C# 中,操作此 Excel 文件以将数据插入
  3. 将文件返回给用户下载
  4. 图表不应是动态的,也不应根据用户对数据表的更改而变化,因为这是引用的。

这是我能想到的最简单的步骤。我可能没有给您具体的操作代码,但我确信您可以在网上搜索它。 希望我能够提供帮助。

关于javascript - 如何将图表与mvc中的相应数据一起动态导出到excel工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37917336/

相关文章:

javascript - 有没有办法使用 cycle2 根据图像是垂直还是水平调整图像大小?

c# - 从数据表中选择和更新数据

jquery - 如何删除 anchor 的跳转?

javascript - 在列上添加底行以求和整列的值

PHP终端模拟器

javascript - TypeError : authService. isAuthenticated 不是一个函数

javascript - 如何使用 $transitions 服务获取 toState 参数? (新的用户界面路由器)

javascript - 如何设置 css width 或 left 属性以获得像素完美的结果?

c# - JSON 根节点 - 与类名称不同

c# - 为什么 Entity Framework 不使用工作单元实现恒等映射?