none
windows c# console app, try to use ms-interop.excel and clipboard, got some problem RRS feed

  • Question

  • What I want to do is to develop a windows console app which load a excel file, get specified range and export to a jpg/png image file. I try to use Microsoft.Office.Interop.Excel and Clipboard to do this, but the copyPicture method which copy excel range to clipboard does not work.

    I am using windows10, visual studio 2017, and all of the references what I "using" have been added in to my project.

    Here is my code:

    Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet;

    xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\Public\Documents\testex.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); Excel.Range rg = xlWorkSheet.Range["A1:B2"]; rg.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlBitmap); if (Clipboard.GetDataObject() != null) { IDataObject data = Clipboard.GetDataObject(); if (data.GetDataPresent(DataFormats.Bitmap)) { Image image = (Image)data.GetData(DataFormats.Bitmap, true); image.Save(@"C:\Users\Public\Documents\testexcel.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } }

    In my code, everything can run. But Clipboard.GetDataObject() got a null.

    Saturday, October 26, 2019 1:28 PM

Answers

All replies