如何将Excel中的图表导出为图形

时间:2010-03-22 20:28:56

标签: java .net excel charts

我有一系列Excel电子表格,每个电子表格至少有一页数据和一页从数据创建的图表。我需要捕获(不从数据重新生成)现有图表作为Web友好图像。这可能通过Java或.Net吗?我知道POI的东西(Java)不会这样做(或者我告诉过,我自己没有尝试过)。

3 个答案:

答案 0 :(得分:2)

您是否尝试过使用Chart.Export方法?

帮助中的示例是:

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

从内存来看,我认为你也可以导出到PNG。

答案 1 :(得分:1)

图表图像不存储在工作簿中,因此您需要一个可以呈现Excel兼容图表的组件。

SpreadsheetGear for .NET将允许您加载Excel工作簿,可选择插入新值/公式/格式/等...,计算,然后从一系列单元格或图表中获取图像。

如果您想亲自试用,可以查看一些示例here并下载免费试用here

免责声明:我拥有SpreadsheetGear LLC

答案 2 :(得分:1)

您可以使用J XL或Aspose Cells转换或复制图表(图表)(Aspose不是免费的)。

这是将Excel图表提取到图片

的代码段
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);
    }

}

我使用了J excel