Hi.
I am evaluating JExcel for my company, and one of our requirements is that we need to be able to programatically create Excel charts.
Does the current version of JExcel support creating Excel charts?
Thank you.
Louis
Hi Louis,
Thanks for your interest in our products.
Using MS Excel stubs you can creating the charts. As an example you can try using ShapeSample.java from JExcel package samples.
Please contact us if you have any further questions.
Sincerely,
Igor Novikov
Thank you for your reply Igor.
I will have a look at the sample and ask if I have any more questions.
Louis
Here for someone's benefit is a working example of creating an Excel chart on a worksheet:
private void buildGraph()
{
try {
worksheet.getOleMessageLoop().doInvokeAndWait(new Runnable()
{
public void run()
{
createChart(worksheet);
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void createChart(Worksheet worksheet)
{
_Worksheet ws = worksheet.getPeer();
IUnknown disp = ws.chartObjects(Variant.createUnspecifiedParameter(),new Int32(0));
ChartObjects objects = new ChartObjectsImpl(disp);
DoubleFloat left = new DoubleFloat(500);
DoubleFloat top = new DoubleFloat(100);
DoubleFloat width = new DoubleFloat(300);
DoubleFloat height = new DoubleFloat(300);
ChartObject chartObject = objects.add(left, top,width, height);
chartObject.setName(new BStr("normalDistChart"));
_Chart chart = chartObject.getChart();
com.jniwrapper.win32.excel.Range valuesRange = ws.getRange(new Variant("distributionRange"),Variant.createUnspecifiedParameter() );
chart.setSourceData(valuesRange, new Variant(2));
chart.setChartType(new XlChartType(4));
chart.setHasLegend(new Int32(0), new VariantBool(true));
chart.getLegend(new Int32(0)).setPosition(new XlLegendPosition(XlLegendPosition.xlLegendPositionBottom));
chart.setHasTitle(new Int32(0), new VariantBool(true));
chart.getChartTitle(new Int32(0)).setText(new BStr("Normal Distribution Demo"));
Series series = new SeriesImpl(chart.seriesCollection(new Variant(1), new Int32(0)));
series.setName(new BStr("Cumulative"));
series = new SeriesImpl(chart.seriesCollection(new Variant(2), new Int32(0)));
series.setName(new BStr("NonCumulative"));
}
Thanks
Louis
Hi Louis,
Thank you for the sample. I think we can use it to create Excel charts sample for JExcel package.
Sincerely,
Igor Novikov
Hi, I'm trying to use your sample code in my project, but alot of the classes don't seem to be in any of the jars i have. Which libraries are need to use the sample?
More concretely, I can't find Worksheet, ChartObject, ChartObjects, ChartObjectsImpl, XlLegendPosition, SeriesImpl,
Any help would be excellent, thanks,
David
Hi David,
To use these classes you need sustituting jexcel.jar by jexcel-full.jar which contains full set of MS Excel stubs. This library can be downloaded from our web site:
http://teamdev.com/jexcel/downloads.jsf
Regard,
Igor Novikov