Spire.XLS
I made a solution using the component Spire.XLS or this developer site is and there is a good documentation about the component .
You can install Spire.XLS by Nuget :
Install-Package Spire.XLS -Version 8.11.6
Here is an example of how I converted a bar chart from excel to image:
public void ConvertChartXlsToImg()
{
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"D:\MinhaPasta\column-chart.xlsx");
Worksheet sheet = workbook.Worksheets[0];
Image[] imgs = workbook.SaveChartAsImage(sheet);
for (int i = 0; i < imgs.Length; i++)
{
imgs[i].Save(string.Format("img-{0}.jpeg", i), ImageFormat.Jpeg);
}
}
Design with examples in GitHub
I made a repository in GitHub with a web project that reads an excel file with a graph of columns and converts it to image and prints on screen.
In the example, if you access the Home / Index controler you will find how to generate the image and save to file or memory ( byte[]
) and print it in View >.
In the excel file there is only one graphic, but if others have been created they will all be printed.
Note: The problem is that the free version of this component limits you to some things. If I use it only for this purpose I believe your only problem will be text that is printed on the graphic image stating that it was generated by this component, but I recommend you read about the limitations on their site and make tests with reading larger and more spreadsheets graphic.
Other components:
EPPlus
With EPPlus I could not find a way to render the image. The following code that was ours first attempt does not work despite compiling, but when trying to read the graph it does not render the image and var img
comes null.
I do not think it's really possible, but I will leave it as a query if anyone tries to use it.
FileInfo arquivoExcel = new FileInfo("CaminhoArquivo");
using (ExcelPackage package = new ExcelPackage(arquivoExcel))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
var img = (ExcelPicture)worksheet.Drawings["Chart 1"];
}
Aspose Cells
Another good option is the Aspose Cells for .NET , but it's not free either and there are some limitations.
In the same GitHub project I made a method that also prints the image using the Aspose Cells component.
Example with Aspose Cells returning Model with list of graphics for printing:
public ActionResult Index()
{
HomeModel model = new HomeModel();
model.ListaExcelChartImg = new List<byte[]>();
//Pegar o caminho do projeto
string path = Server.MapPath("~");
//Abrir arquivo excel com Aspose Cells
Workbook workbook = new Workbook(path + "\Content\column-chart.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
foreach (var grafico in worksheet.Charts)
{
MemoryStream ms = new MemoryStream();
grafico.ToImage().Save(ms, ImageFormat.Jpeg);
byte[] bmpBytes = ms.ToArray();
model.ListaExcelChartImg.Add(bmpBytes);
}
return View(model);
}
Unfortunately I have not found a totally free component that does not limit you to some aspect that renders the graph to excel. But here's a list of Nuget Packages that work with excel file .