以编程方式(C#)将Excel转换为图像列表

时间:2016-05-24 09:25:36

标签: c# excel vba com-interop openxml-sdk

如何将Microsoft Excel(.xlsx)文件转换为PPT

我想要的算法

以编程方式(c#)在Excel文件中

获取图表。并保存在图像列表

目前我正在使用Microsoft Interop Libraries,即使我不喜欢我没有任何免费替代此任务

所以我目前的解决方案如下:

使用Microsoft Interop打开Excel文件;

查找excel中的所有图表

使用该图表上的CopyPicture(),它将数据复制到剪贴板。

一旦我们在列表中有图像,我们就可以将其添加到新的powerpoint文件中 创建新的PPT文件

请告诉我如何添加到剪贴板到图片列表

public List<Image> Chartimages;
public List<Metafile> ChartimagesMetafile;

public List<BitmapSource> ChartimagesBitmapSource;

    public void InsertChartIntoChartlist()
            {
                try
                {
                    // Create an instance of PowerPoint.
                    powerpointApplication = new pptNS.Application();

                    // Create an instance Excel.
                    excelApplication = new xlNS.Application();

                    // Open the Excel workbook containing the worksheet with the chart

                    // data.
                    excelWorkBook = excelApplication.Workbooks.Open(paramWorkbookPath,
                                                         paramMissing, paramMissing, paramMissing,
                                                         paramMissing, paramMissing, paramMissing,
                                                         paramMissing, paramMissing, paramMissing,
                                                         paramMissing, paramMissing, paramMissing,
                                                         paramMissing, paramMissing);

                    // Get the worksheet that contains the chart.
                    targetSheet = (xlNS.Worksheet)(excelWorkBook.Worksheets[2]);

                    // Get the ChartObjects collection for the sheet.

                    chartObjects = (xlNS.ChartObjects)(targetSheet.ChartObjects(paramMissing));

                    foreach (xlNS.ChartObject item in chartObjects)
                    {
                        //    // Get the chart to copy.
                        existingChartObject = (xlNS.ChartObject)(item);

                        // Copy the chart from the Excel worksheet to the clipboard.

                        existingChartObject.CopyPicture(xlNS.XlPictureAppearance.xlPrinter, xlNS.XlCopyPictureFormat.xlPicture);

                        if (Clipboard.ContainsData(System.Windows.DataFormats.EnhancedMetafile))
                        {
                            Metafile metafile = Clipboard.GetData(System.Windows.DataFormats.EnhancedMetafile) as Metafile;
                            //  metafile.Save(fileName);
                            ChartimagesMetafile.Add(metafile);
                        }
                        else if (Clipboard.ContainsData(System.Windows.DataFormats.Bitmap))
                        {
                            BitmapSource bitmapSource = Clipboard.GetData(System.Windows.DataFormats.Bitmap) as BitmapSource;

                            ChartimagesBitmapSource.Add(bitmapSource);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

谢谢

最后,对于那些贬低这一点的人:我们都在学习有一天,作为一个初学者,如何做一些事情绝对是编程相关的,无论有多新人。

2 个答案:

答案 0 :(得分:1)

这是解决方案:

以编程方式在Excel文件中获取图表(C#)。并保存在图像列表

创建名为ImageWithImageName的类以保存图像以及名称

class ImageWithImageName
    {
       public Image ChartimagesBitmapSource;
        public string Filename;
        public ImageWithImageName(Image pramChartimagesBitmapSource, string pramFilename)
        {
            ChartimagesBitmapSource = pramChartimagesBitmapSource;
            Filename = pramFilename;
        }
    }

用法

 public List<ImageWithImageName> ChartImages;

创建将所有图表添加到ChartImages列表数组的方法

public void InsertChartIntoExcel()         {             尝试             {

            // Create an instance Excel.
            excelApplication = new xlNS.Application();

            // Open the Excel workbook containing the worksheet with the chart
            // data.
            excelWorkBook = excelApplication.Workbooks.Open(paramWorkbookPath,
                            paramMissing, paramMissing, paramMissing,
                            paramMissing, paramMissing, paramMissing,
                            paramMissing, paramMissing, paramMissing,
                            paramMissing, paramMissing, paramMissing,
                            paramMissing, paramMissing);

            // Get the worksheet that contains the chart.
            targetSheet = (xlNS.Worksheet)(excelWorkBook.Worksheets[2]);

            // Get the ChartObjects collection for the sheet.
            chartObjects = (xlNS.ChartObjects)(targetSheet.ChartObjects(paramMissing));

            // Create a PowerPoint presentation.
            //pptPresentation = powerpointApplication.Presentations.Add(
            //                    Microsoft.Office.Core.MsoTriState.msoTrue);

            int i = 1;
            foreach (xlNS.ChartObject item in chartObjects)
            {
                //    // Get the chart to copy.
                existingChartObject = (xlNS.ChartObject)(item);

                string chartname = item.Name;

                existingChartObject.CopyPicture(xlNS.XlPictureAppearance.xlScreen, xlNS.XlCopyPictureFormat.xlBitmap);

// contains in Clipboard so extract from clipboard 

                if (Clipboard.ContainsImage())
                {


                    var image = Clipboard.GetData(System.Windows.DataFormats.Bitmap) as Image;
                    if (image != null)
                    {
                        ChartImages.Add(new ImageWithImageName(image, chartname + ".png"));
                    }
                }



                i++;
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {


            // Release the Excel objects.
            targetSheet = null;
            chartObjects = null;
            existingChartObject = null;

            // Close and release the Excel Workbook object.
            if (excelWorkBook != null)
            {
                excelWorkBook.Close(false, paramMissing, paramMissing);
                excelWorkBook = null;
            }

            // Quit Excel and release the ApplicationClass object.
            if (excelApplication != null)
            {
                excelApplication.Quit();
                excelApplication = null;
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }

现在我在图像列表中有图像,所以现在我们可以为这个图像创建一个新的Powerpoint文件

仅供参考:我们可以通过打开xml 将图像添加到PPT,以便COM Interop创建PPT

快乐的编码 Ranjith

答案 1 :(得分:0)

我能够获得复制图表的代码;通过粘贴到Word文档中进行测试。 Powerpoint部分应该落实到位。

public void InsertChartIntoChartlist()
{
    try
    {
        // Create an instance of PowerPoint.
        var powerpointApplication = new Microsoft.Office.Interop.PowerPoint.Application();

        // Create an instance Excel.
        var excelApplication = new Microsoft.Office.Interop.Excel.Application();

        // Open the Excel workbook containing the worksheet with the chart data.
        var excelWorkBook = excelApplication.Workbooks.Open(@"C:\Book1.xlsx");

        // Get the worksheet that contains the chart.
        var targetSheet = excelWorkBook.Worksheets[2];

        // Get the ChartObjects collection for the sheet.
        var chartObjects = targetSheet.ChartObjects(Type.Missing);

        foreach (Microsoft.Office.Interop.Excel.ChartObject item in chartObjects)
        {
            item.Copy();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}