不完全相同的问题,但我们在尝试将电子表格齿轮图表图像保留在 MVC 应用程序的上下文中时遇到了各种麻烦,因此决定采用一种方法,将图表作为 png 图像保存到服务器上,然后从服务器中调用到视图。
这是在服务器上保存 Excel 图表并在剃刀视图中检索它的代码:
<!-- language: lang-cs -->
// Get the image from the spreadsheet
SpreadsheetGear.Shapes.IShape chart7 = worksheet.Shapes["Chart 7"];
SpreadsheetGear.Drawing.Image image7 = new SpreadsheetGear.Drawing.Image(chart7);
saveChartImage(image7, "Chart7.png");
//Save chart images to the server
private void saveChartImage(SpreadsheetGear.Drawing.Image image, string chartName)
{
var imageFile = System.IO.Path.Combine(imagePath, chartName);
System.Drawing.Image bitmap = image.GetBitmap();
bitmap.Save(imageFile, System.Drawing.Imaging.ImageFormat.Png);
}
// Action result to get image
public ActionResult getImage(string imageDir, string filename)
{
var file = filename;
var fullPath = Path.Combine(imageDir, file);
return File(fullPath, "image/png", file);
}
检索视图中的图像:
<img src="@Url.Action("getImage", new { imageDir = "~/images/" "filename = "Chart7.png" })" />
我们在一些非常大且复杂的电子表格(6 - 8MB)上使用了这种方法。该代码从 16 个散点图中调用图像,其中包含 400 多个数据点。这些图表在服务器上大约 16KB,据我所知,保存/检索时间受互联网连接而不是服务器的限制。
这种方法的缺点是它不适合多用户环境。为此,您需要创建用户会话并为用户提供唯一的图表名称。