Java Code Samples for Converting Charts in Excel Files to Image Files


/ Published in: Java
Save to your folder(s)

The following technical tip show how developers can Convert Chart in excel file to Image in java using Aspose.Cells component. Charts are visually appealing and make it easy for users to see comparisons, patterns, and trends in data. For instance, rather than analyzing columns of worksheet numbers, a chart shows at a glance whether sales are falling or rising, or how actual sales compare to projected sales. People are frequently asked to present statistical and graphical information in an easy to understand and an easy to maintain manner. A picture helps. Sometimes, charts are needed in an application or web pages. Or it might be needed needed for a Word document, a PDF file, a PowerPoint presentation or some other application. In each case, you want to render the chart as an image so that you can use it elsewhere. Aspose.Cells for Java has supported converting charts in Excel files to image files since release 2.1.2.


Copy this code and paste it in your HTML
  1. //Create a new Workbook.
  2. Workbook workbook = new Workbook();
  3.  
  4. //Get the first worksheet.
  5. Worksheet sheet = workbook.getWorksheets().get(0);
  6.  
  7. //Set the name of worksheet
  8. sheet.setName("Data");
  9.  
  10. //Get the cells collection in the sheet.
  11. Cells cells = workbook.getWorksheets().get(0).getCells();
  12.  
  13. //Put some values into a cells of the Data sheet.
  14. cells.get("A1").setValue("Region");
  15. cells.get("A2").setValue("France");
  16. cells.get("A3").setValue("Germany");
  17. cells.get("A4").setValue("England");
  18. cells.get("A5").setValue("Sweden");
  19. cells.get("A6").setValue("Italy");
  20. cells.get("A7").setValue("Spain");
  21. cells.get("A8").setValue("Portugal");
  22. cells.get("B1").setValue("Sale");
  23. cells.get("B2").setValue(70000);
  24. cells.get("B3").setValue(55000);
  25. cells.get("B4").setValue(30000);
  26. cells.get("B5").setValue(40000);
  27. cells.get("B6").setValue(35000);
  28. cells.get("B7").setValue(32000);
  29. cells.get("B8").setValue(10000);
  30.  
  31. //Create chart
  32. intchartIndex = sheet.getCharts().add(ChartType.COLUMN, 12, 1, 33, 12);
  33. Chart chart = sheet.getCharts().get(chartIndex);
  34.  
  35. //Set properties of chart title
  36. chart.getTitle().setText("Sales By Region");
  37. chart.getTitle().getTextFont().setBold(true);
  38. chart.getTitle().getTextFont().setSize(12);
  39.  
  40. //Set properties of nseries
  41. chart.getNSeries().add("Data!B2:B8", true);
  42. chart.getNSeries().setCategoryData("Data!A2:A8");
  43.  
  44. //Set the fill colors for the series's data points (France - Portugal(7 points))
  45. ChartPointCollectionchartPoints = chart.getNSeries().get(0).getPoints();
  46.  
  47. ChartPoint point = chartPoints.get(0);
  48. point.getArea().setForegroundColor(Color.getCyan());
  49.  
  50. point = chartPoints.get(1);
  51. point.getArea().setForegroundColor(Color.getBlue());
  52.  
  53. point = chartPoints.get(2);
  54. point.getArea().setForegroundColor(Color.getYellow());
  55.  
  56. point = chartPoints.get(3);
  57. point.getArea().setForegroundColor(Color.getRed());
  58.  
  59. point = chartPoints.get(4);
  60. point.getArea().setForegroundColor(Color.getBlack());
  61.  
  62. point = chartPoints.get(5);
  63. point.getArea().setForegroundColor(Color.getGreen());
  64.  
  65. point = chartPoints.get(6);
  66. point.getArea().setForegroundColor(Color.getMaroon());
  67.  
  68. //Set the legend invisible
  69. chart.setShowLegend(false);
  70.  
  71. //Get the Chart image
  72. ImageOrPrintOptionsimgOpts = new ImageOrPrintOptions();
  73. imgOpts.setImageFormat(ImageFormat.getPng());
  74.  
  75. //Save the chart image file.
  76. chart.toImage(new FileOutputStream("D:\\Files\\MyChartImage.png"), imgOpts);

URL: http://www.aspose.com/docs/display/cellsjava/Converting+Chart+to+Image

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.