是否有任何方法將圖表導出到具有相應數據的Excel表格中。目前,我已經創建了一個mvc應用程序,並在下載按鈕中單擊,我能夠將存儲在數組中的數據導出到excel表格中。但我的主要任務是將動態圖形和數據一起帶到excel中,並且該圖形應該在excel中的值被修改時動態更改。請幫助我this.Am只是在這樣的初學者,所以.... 這裏是我的controller.cs代碼...如何動態地將圖形導出到excel表格以及mvc中的相應數據
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public void download()
{
var data = new[]{
new{ Name="Ram", Email="[email protected]", Phone="111-222-3333" },
new{ Name="Shyam", Email="[email protected]", Phone="159-222-1596" },
new{ Name="Mohan", Email="[email protected]", Phone="456-222-4569" },
new{ Name="Sohan", Email="[email protected]", Phone="789-456-3333" },
new{ Name="Karan", Email="[email protected]", Phone="111-222-1234" },
new{ Name="Brij", Email="[email protected]", 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(善意地(?))爲您掩蓋了錯誤。如果你想要一個圖表,你必須導出一個'.xlsx',而不是文本。您可以使用OpenXml(https://msdn.microsoft.com/en-us/library/office/bb448854.aspx)或任何包裝(ClosedXml是一種),因爲OpenXml並非最明顯的API。 –
感謝您提供這方面的信息。正如我之前所說的那樣,我在這個領域有了新的東西。所以請幫助我糾正我的錯誤。我的代碼中應該做些什麼改變來實現這一目標。您是人才的天才。 –
對於SO,代碼回答太寬泛。你可以谷歌如何操縱C#中的xlsx文件。 –