有没有办法将图形导出到具有相应数据的 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 模板。步骤如下:
- 创建一个包含图表的 Excel 文件。它应该引用包含数据的工作表。默认情况下,数据表当然是空白的
- 在 C# 中,操作此 Excel 文件以将数据插入
- 将文件返回给用户下载
- 图表不应是动态的,也不应根据用户对数据表的更改而变化,因为这是引用的。
这是我能想到的最简单的步骤。我可能没有给您具体的操作代码,但我确信您可以在网上搜索它。 希望我能够提供帮助。
关于javascript - 如何将图表与mvc中的相应数据一起动态导出到excel工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37917336/