Home » excel » php – Export HighChart as an image in excel file together with the other page contents

php – Export HighChart as an image in excel file together with the other page contents

Posted by: admin March 9, 2020 Leave a comment

Questions:

Export HighChart as an image in excel file together with the other page contents like tables, text, etc. When I click the export button the whole Page content will be save as excel file via header but instead of exporting all page content to excel file it excludes the HighChart Graph. I think the solution is to export the graph as image in excel but I don’t have any idea how to that. Is there anyone know how to do it or have any idea how to solve this problem?

How to&Answers:
  1. Here is link on highcharts documentation. Thats will help u to export image and store it.

  2. a) Documentation #1
    b) Documentation #2
    That will help u with PHPExcel classs API.

  3. And finally exapmle of image paste to a sheet, using PHPExcel class: one or two;

Have more questions? See that links: one, two.
And official PHPExcel examples: here.

Good luck!

Answer:

First you have to send the svgtext and the csv text ti the server via ajax.
Then do the following:

public JsonResult ExportToImage(string base64, string graphdata)
{
try
{

            var base64String = base64.Remove(0, 1);
            var rows = graphdata.Split('\n');

        byte[] bytes = Convert.FromBase64String(base64);
        var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "Content\Images\");
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] files = di.GetFiles("*.xls")
                                 .Where(p => p.Extension == ".xls").ToArray();
            foreach (FileInfo file in files)
                try
                {
                    file.Attributes = FileAttributes.Normal;
                    System.IO.File.Delete(file.FullName);
                }
                catch { }
            using (Image image = Image.FromStream(new MemoryStream(bytes)))
        {
            image.Save(path+"output.png", ImageFormat.Jpeg);  // Or Png
        }

            var xlApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook = xlApp.Workbooks.Add();
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = xlWorkBook.Sheets[1];

            for(var y=0; y<rows.Count();y++)
            {
                var row = rows[y];
                var columValues = row.Split(',');
                for (var x = 0; x < columValues.Count(); x++)
                {
                    xlWorkSheet.Cells[y+20, x+1] = columValues[x];
                }

            }


            xlWorkSheet.Shapes.AddPicture(path + "output.png", MsoTriState.msoFalse, MsoTriState.msoCTrue, 0, 0, -1, -1);
            var fileName = string.Format("GraphDataExport{0}.xls", DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss"));
            xlWorkBook.SaveAs(path + fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal);
            xlWorkBook.Close(true);
            xlApp.Quit();

            Marshal.ReleaseComObject(xlApp);
            return Json(fileName);
        }
        catch (Exception e)
        {
            return Json(e.Message + Environment.NewLine + e.InnerException + Environment.NewLine + e.Data + Environment.NewLine);
        }
    }

Now you can do a window.location to that file