Export graphs from an Excel spreadsheet without Interop

Asked

Viewed 419 times

6

I need to read and write data from Excel spreadsheets, and export some charts using C#. But I cannot use the interoperability library (Microsoft.Office.Interop) for that reason.

How can I do?
Does anyone have any indication of Nuget package for this?

I know Epplus, I’ve done some work with him, but with him you can’t export graphics.

I know that with Epplus it is possible to record and read data from the spreadsheet, and even create charts, but the problem is at the time of exporting the charts, I need to export this existing chart in an existing excel to JPG, PNG or even PDF. But preferably to JPG.

  • 1

    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?

  • 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.

  • 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!

  • Done haha, thank you

2 answers

6


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.

2

You can generate graphics using Epplus, see an example:

        //Criar o relatório
        var pieChart = (ExcelPieChart)ws.Drawings.AddChart("crtExtensionsSize", eChartType.PieExploded3D);
        pieChart.SetPosition(1, 0, 2, 0);
        pieChart.SetSize(400, 400);
        //Adicionar as séries
        pieChart.Series.Add(ExcelRange.GetAddress(3, 2, row-1, 2), ExcelRange.GetAddress(3, 1, row-1, 1));
        //Adicionar titulo e configurar o relatório
        pieChart.Title.Text = "Extension Size";
        pieChart.DataLabel.ShowCategory = true;
        pieChart.DataLabel.ShowPercent = true;
        pieChart.DataLabel.ShowLeaderLines = true;
        pieChart.Legend.Remove();

You can see more information on the site Wiki of the Epplus

  • Yes, generate graphics I know it is possible, but what I need is to export an existing graphic already in Excel, be it JPG, PDF, whatever format ( preferably in JPG ), I will put this in the question. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.