将Excel中的指定区域 复制到 剪贴板,然后根据剪贴板 生成图片。 
   执行这一句总是报参数无效的错误:System.Drawing.Imaging.Metafile metafile = new System.Drawing.Imaging.Metafile(alloc, true);
代码如下:
static void copyToJpg()
        {
            //初始化excel对象
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            //打开xls文件
            excel.Workbooks.Open(@"d:\tt.xls", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);            //excel.Workbooks.Open(@"C:\Tt.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
            //选定到第2行第2列所在的单元格
            //Excel.Range r = (Excel.Range)excel.Cells[10, 2];
            Microsoft.Office.Interop.Excel.Range r = (Microsoft.Office.Interop.Excel.Range)excel.get_Range("A1", "C11");
            r.Select();
            //将单元格复制到剪贴板中
            r.CopyPicture(Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap);
            //退出excel
            excel.Quit();
          
            OpenClipboard(IntPtr.Zero);            uint format = 14/*CF_ENHMETAFILE*/;
            IntPtr alloc = GetClipboardData(format);
            System.Drawing.Imaging.Metafile metafile = new System.Drawing.Imaging.Metafile(alloc, true);
            CloseClipboard();            Bitmap b = new Bitmap(metafile);
            b.Save(@"d:\test.bmp");
        }        [DllImport("user32.dll")]
        private static extern bool OpenClipboard(IntPtr hWndNewOwner);        [DllImport("user32.dll")]
        private static extern bool EmptyClipboard();        [DllImport("user32.dll")]
        private static extern IntPtr GetClipboardData(uint uFormat);        [DllImport("user32.dll")]
        private static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);        [DllImport("user32.dll")]
        private static extern bool CloseClipboard();

解决方案 »

  1.   

            static void copyToJpg()
            {
                //初始化excel对象
                Excel.Application excel = new Excel.Application();
                //打开xls文件
                excel.Workbooks.Open(@"d:\tt.xls", System.Type.Missing,...);            //选定到第2行第2列所在的单元格
                Excel.Range r = (Microsoft.Office.Interop.Excel.Range)excel.get_Range("A1", "C11");
                r.Select();
                //将单元格复制到剪贴板中
                r.CopyPicture(Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen,    Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap);
                //退出excel
                excel.Quit();
              
                OpenClipboard(IntPtr.Zero);            uint format = 14/*CF_ENHMETAFILE*/;
                IntPtr alloc = GetClipboardData(format);
                System.Drawing.Imaging.Metafile metafile = new System.Drawing.Imaging.Metafile(alloc, true);
                CloseClipboard();            Bitmap b = new Bitmap(metafile);
                b.Save(@"d:\test.bmp");
            }本来是格式化后的,贴上来没有保留原格式。
      

  2.   

    IntPtr alloc = GetClipboardData(format);得到的alloc 为 0。
      

  3.   

    GetClipboardData(format)
    好像.net直接有cliboard用法,自己去查下