开发者

How do I export charts from Excel as graphics

I've got a series of Excel spreadsheets, each with at least one page of data and one page of a chart created 开发者_如何学运维from the data. I need to capture ( not regenerate from the data ) the existing chart as a web friendly image. Is this possible via Java or .Net? I know the POI stuff (Java) won't do it (or so I'm told, haven't tried it myself).


Have you tried using the Chart.Export Method?

The example in help is:

Worksheets("Sheet1").ChartObjects(1).Chart. Export _
FileName:="current_sales.gif", FilterName:="GIF"

From memory, I think you can export to PNG as well.


Chart images are not stored in workbooks, so you need a component which can render Excel compatible charts.

SpreadsheetGear for .NET will let you load Excel workbooks, optionally plug in new values / formulas / formats / etc..., calculate, and then get an image from a range of cells or from a chart.

You can see some samples here and download the free trial here if you want to try it yourself.

Disclaimer: I own SpreadsheetGear LLC


You can convert or copy charts(graphs) using J XL or Aspose Cells(Aspose is not free).

This is the code snippet to extract excel chart to image

public class ExportChartToImage
{
    public static void main(String[] args) throws Exception
    {
        //Start Excel
        Application excelApp = new Application();
        excelApp.setVisible(true);

        //Create test workbook
        Workbook workbook = excelApp.createWorkbook("/home/tejus/Desktop/Chart Test");

        //Get the first (and the only) worksheet
        final Worksheet worksheet1 = workbook.getWorksheet(1);

        //Fill-in the first worksheet with sample data
        worksheet1.getCell("A1").setValue("Date");
        worksheet1.getCell("A2").setValue("March 1");
        worksheet1.getCell("A3").setValue("March 8");
        worksheet1.getCell("A4").setValue("March 15");

        worksheet1.getCell("B1").setValue("Customer");
        worksheet1.getCell("B2").setValue("Smith");
        worksheet1.getCell("B3").setValue("Jones");
        worksheet1.getCell("B4").setValue("James");

        worksheet1.getCell("C1").setValue("Sales");
        worksheet1.getCell("C2").setValue("23");
        worksheet1.getCell("C3").setValue("17");
        worksheet1.getCell("C4").setValue("39");

        excelApp.getOleMessageLoop().doInvokeAndWait(new Runnable()
        {
            public void run()
            {
                final Variant unspecified = Variant.createUnspecifiedParameter();
                final Int32 localeID = new Int32(LocaleID.LOCALE_SYSTEM_DEFAULT);

                Range sourceDataNativePeer = worksheet1.getRange("A1:C4").getPeer();
                _Worksheet worksheetNativePeer = worksheet1.getPeer();

                IDispatch chartObjectDispatch = worksheetNativePeer.chartObjects(unspecified, localeID);

                ChartObjectsImpl chartObjects = new ChartObjectsImpl(chartObjectDispatch);
                ChartObject chartObject = chartObjects.add(new DoubleFloat(100), new DoubleFloat(150), new DoubleFloat(300), new DoubleFloat(225));

                _Chart chart = chartObject.getChart();
                chart.setSourceData(sourceDataNativePeer, new Variant(XlRowCol.xlRows));

                BStr fileName = new BStr("/home/tejus/Desktop/chart.gif");
                Variant filterName = new Variant("gif");
                Variant interactive = new Variant(false);

                chart.export(fileName, filterName, interactive);

                chart.setAutoDelete(false);
                chart.release();

                chartObject.setAutoDelete(false);
                chartObject.release();

                chartObjects.setAutoDelete(false);
                chartObjects.release();

                chartObjectDispatch.setAutoDelete(false);
                chartObjectDispatch.release();
            }
        });

        System.out.println("Press 'Enter' to terminate the application");
        System.in.read();

        //Close the MS Excel application.
        boolean saveChanges = false;
        workbook.close(saveChanges);
        boolean forceQuit = true;
        excelApp.close(forceQuit);
    }

}

i used J excel

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜