用于将Excel文件中的图表转换为图像文件的Java代码示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用于将Excel文件中的图表转换为图像文件的Java代码示例相关的知识,希望对你有一定的参考价值。
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.
//Create a new Workbook. Workbook workbook = new Workbook(); //Get the first worksheet. Worksheet sheet = workbook.getWorksheets().get(0); //Set the name of worksheet sheet.setName("Data"); //Get the cells collection in the sheet. Cells cells = workbook.getWorksheets().get(0).getCells(); //Put some values into a cells of the Data sheet. cells.get("A1").setValue("Region"); cells.get("A2").setValue("France"); cells.get("A3").setValue("Germany"); cells.get("A4").setValue("England"); cells.get("A5").setValue("Sweden"); cells.get("A6").setValue("Italy"); cells.get("A7").setValue("Spain"); cells.get("A8").setValue("Portugal"); cells.get("B1").setValue("Sale"); cells.get("B2").setValue(70000); cells.get("B3").setValue(55000); cells.get("B4").setValue(30000); cells.get("B5").setValue(40000); cells.get("B6").setValue(35000); cells.get("B7").setValue(32000); cells.get("B8").setValue(10000); //Create chart intchartIndex = sheet.getCharts().add(ChartType.COLUMN, 12, 1, 33, 12); Chart chart = sheet.getCharts().get(chartIndex); //Set properties of chart title chart.getTitle().setText("Sales By Region"); chart.getTitle().getTextFont().setBold(true); chart.getTitle().getTextFont().setSize(12); //Set properties of nseries chart.getNSeries().add("Data!B2:B8", true); chart.getNSeries().setCategoryData("Data!A2:A8"); //Set the fill colors for the series's data points (France - Portugal(7 points)) ChartPointCollectionchartPoints = chart.getNSeries().get(0).getPoints(); ChartPoint point = chartPoints.get(0); point = chartPoints.get(1); point = chartPoints.get(2); point = chartPoints.get(3); point = chartPoints.get(4); point = chartPoints.get(5); point = chartPoints.get(6); //Set the legend invisible chart.setShowLegend(false); //Get the Chart image ImageOrPrintOptionsimgOpts = new ImageOrPrintOptions(); imgOpts.setImageFormat(ImageFormat.getPng()); //Save the chart image file.
以上是关于用于将Excel文件中的图表转换为图像文件的Java代码示例的主要内容,如果未能解决你的问题,请参考以下文章