Spire.XLS
I made a solution using the component Spire.XLS the This is the developer’s site and there’s a good documentation on the component.
You can install the Spire.XLS by Nuget:
Install-Package Spire.XLS -Version 8.11.6
Here’s an example of how I converted an excel bar chart 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);
}
}
Project with examples on Github
I made a repository on Github with a web project that reads an excel file with a column chart and converts it to image and prints on screen.
In the example, if you access the controller Home/Index find how to generate the image and save in file or memory (byte[]
) and her impression on View.
In excel file there is only one chart, but if others were created all will be printed.
Note: The problem is that the free version of this component limits you in some things. If you use only for this purpose I believe your only problem will be a text that is printed on the graphic image stating that it was generated by this component, but I recommend that you read about the limitations on their site and do tests with reading larger spreadsheets and more graphic.
Other components:
Epplus
With the Epplus I couldn’t find a way to render the image. The following code that was our first attempt does not work despite compiling, but when trying to read the graph it does not render the image and the var img
comes nil.
I believe it’s really not possible, but I’ll leave it as a consultation if someone 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 also not free 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 chart list 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 did not find a completely free component that does not limit you in any aspect that renders the graph to excel. But it follows a list of Nuget Packages working with excel file.
Alexandre, here the questions should be asked in Portuguese. But, see the end of this answer: How to save an XLSX (Excel) file instead of CSV?
– Pedro Gaspar
Ops, I thought you were going to Stack Global, sorry. So, I know Epplus, I’ve done some work with him, but can’t handle graphics with him.
– Alexandre Videschi
So translate the question, please, and take advantage and already include this information, which Epplus you already know and does not serve because it does not allow you to manipulate graphics. You might even want to drop the tag
interop
, since you want just without interoperability!– Pedro Gaspar
Done haha, thank you
– Alexandre Videschi