Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以编程方式(C#)将Excel转换为图像列表_C#_Excel_Vba_Com Interop_Openxml Sdk - Fatal编程技术网

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

以编程方式(C#)将Excel转换为图像列表,c#,excel,vba,com-interop,openxml-sdk,C#,Excel,Vba,Com Interop,Openxml Sdk,如何将Microsoft excel(.xlsx)文件转换为PPT 我想要的算法 以编程方式(c#)在Excel文件中获取图表。并保存在图像列表中 目前,我正在使用Microsoft Interop库,即使我不喜欢我没有任何免费的替代方案来完成此任务 因此,我目前的工作方法如下: 使用Microsoft Interop打开Excel文件 在excel中查找所有图表 使用该图表上的CopyPicture()将数据复制到剪贴板 一旦列表中有了图像,我们就可以将其添加到新的powerpoint文件中

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

我想要的算法

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

目前,我正在使用Microsoft Interop库,即使我不喜欢我没有任何免费的替代方案来完成此任务

因此,我目前的工作方法如下:

使用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);
                }
            }
公共列表图表图像;
公共列表图表图像数据文件;
公共列表ChartImageSBitMacSource;
公共void InsertChartIntoChartlist()
{
尝试
{
//创建PowerPoint的实例。
powerpointApplication=new pptNS.Application();
//创建一个Excel实例。
excelApplication=new xlNS.Application();
//打开包含带有图表的工作表的Excel工作簿
//数据。
excelWorkBook=excelApplication.Workbooks.Open(paramWorkbookPath,
paramMissing,paramMissing,paramMissing,
paramMissing,paramMissing,paramMissing,
paramMissing,paramMissing,paramMissing,
paramMissing,paramMissing,paramMissing,
paramMissing,paramMissing);
//获取包含图表的工作表。
targetSheet=(xlNS.Worksheet)(excelWorkBook.Worksheets[2]);
//获取图纸的ChartObjects集合。
chartObjects=(xlNS.chartObjects)(targetSheet.chartObjects(paramMissing));
foreach(chartObjects中的xlNS.ChartObject项)
{
////获取要复制的图表。
existingChartObject=(xlNS.ChartObject)(项);
//将图表从Excel工作表复制到剪贴板。
现有ChartObject.CopyPicture(xlNS.xlPictureApearance.xlPrinter,xlNS.XlCopyPictureFormat.xlPicture);
if(剪贴板.ContainsData(System.Windows.DataFormats.EnhancedMetafile))
{
图元文件图元文件=剪贴板.GetData(System.Windows.DataFormats.EnhancedMetafile)作为图元文件;
//保存(文件名);
添加(图元文件);
}
else if(剪贴板.ContainsData(System.Windows.DataFormats.Bitmap))
{
BitmapSource BitmapSource=Clipboard.GetData(System.Windows.DataFormats.Bitmap)作为BitmapSource;
ChartImageSBitMacSource.Add(位图源);
}
}
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
}
谢谢


最后,对于那些否决这一观点的人:有一天,我们都在学习,作为一个初学者,如何做一些事情肯定与编程有关,不管一个人有多新;通过粘贴到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);
    }
}
以下是解决方案:

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

创建名为ImageWithImageName的类来保存带有名称的图像

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

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

仅供参考:我们可以通过开放xml为什么COM互操作来创建PPT,将图像添加到PPT中

快乐编码 兰吉思

 public List<ImageWithImageName> ChartImages;
            // 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();
        }
    }