有什么方法可以将图形与相应的数据导出到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文件以在其中插入数据
将文件返回给用户下载
图表不应是动态的,并且也应根据用户对数据表的更改而更改,因为已引用了此内容。
这是我能想到的最简单的步骤。我可能没有给您确切的代码,但是我确定您可以在网上搜索它。
希望我能够提供帮助。