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

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

我的预定algorithm

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

目前,我正在使用Microsoft Interop库,即使通过我不喜欢我没有任何自由的select这个任务

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

使用Microsoft Interop打开Excel文件;

在Excel中查找所有图表

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

一旦我们在列表中的图像,我们可以添加到新的PPT文件创build新的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); } } 

谢谢

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

这里是解决scheme:

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

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

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

用法

  public List<ImageWithImageName> ChartImages; 

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

公共无效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(); } } 

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

仅供参考:我们可以通过Open xml将图像添加到PPT为什么COM Interop创build一个PPT

快乐编码兰吉斯

我能够得到代码复制图表; 通过粘贴到Word文档进行testing。 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); } }