本帖最后由 wxxloveu 于 2011-07-10 11:21:31 编辑

解决方案 »

  1.   

    该错误的原因是item(8)中没有图片,你的row=8指的是第8张图片吗?如果是第8行的图片那就错了。
    如果想通过位置判断,可以找shape.BottonRightCell
      

  2.   

    呵呵,你是说这样么?貌似无法复制到图片哦,  Excel.Range range = sheet1.Cells[7, 8] as Excel.Range;  //int row = 8;
      IDataObject iData = Clipboard.GetDataObject();  //Excel.Shape s = sheet1.Shapes.Item(row) as Excel.Shape;
      //Excel.Range range = s.BottomRightCell;
      range.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
      

  3.   


    其中一个可行的方法是对sheet.shapes进行遍历,假设图片的右下角位于第八行,第1列的情况,逐个找shape..BottonRightCell.Row=8, col=1的shape就可以啦。
      

  4.   


    谢谢回复,遇到一个问题,这句话保存
     Excel.Shape s = sheet1.Shapes.Item(row) as Excel.Shape;
    错误提示:不允许进入指定汇编中的索引。
    有什么好的解决方法么?
      

  5.   

    这里的row不要赋值8, item(i)的意思是表中的第i个shape,而不是单元格i的意思。
      

  6.   

    重新写了个代码,但是还是出错//初始化excel对象
    Excel.Application excel = new Excel.Application();
    //打开xls文件(注意:后面的参数都用Type.Missing填充,表示使用参数的默认值)
    Excel.Workbook wkb = excel.Workbooks.Open(path, 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.Worksheet ws = wkb.Worksheets[1] as Excel.Worksheet;for (int row = 1; row <= ws.UsedRange.Rows.Count; row++)
    {
        for (int col = 1; col <= ws.UsedRange.Columns.Count; col++)
        {
            Range rge = ws.Cells[row, col] as Range;
            foreach (Excel.Shape sh in ws.Shapes)
            {
                Range rg = excel.get_Range(sh.TopLeftCell, sh.BottomRightCell);
                if (excel.Intersect(rge, rg, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                  Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing) != null)
                {
                    sh.CopyPicture(Appearance.Button, Excel.XlCopyPictureFormat.xlBitmap);
                    IDataObject data = Clipboard.GetDataObject();
    //这条if语出错,报未将对象引用设置到对象的实例,晕了,大侠们,帮忙啊
                    if (data.GetDataPresent(DataFormats.Bitmap))
                    {
                        System.Drawing.Image image = (System.Drawing.Image)data.GetData(DataFormats.Bitmap, true);
                        image.Save("test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }
            }
        }
    }大侠们,我只想读取Excel中第7列第8行里面的图片,然后另存为一个独立的jpg或者bmp等图片文件,搞不定啊搞不定啊,要抓狂了
      

  7.   

    //判断剪贴板中是否存在图片,如果存在,则将图片显示到pictureBox1中
    if (Clipboard.ContainsImage())
    {
     System.Drawing.Image image = Clipboard.GetImage();
     pictureBox1.Image = image;
     image.Save(string.Format(@"D:\kkkkkkkkkkkkkkkkkkk\-" + "hao" + "-.jpg")); //保存。
    }
    image1.Save(string.Format(@"D:\kkkkkkkkkkkkkkkkkkk\-" + "hao" + "-.jpg")); //保存。我靠 我靠,剪切板里明显有图片,但是就是不执行到里面去,我在excel里可以直接黏贴到表格中,可以黏贴我要的图片,但直接在外面黏贴就会报错,未将对象引用设置到对象的实例,这是怎么回事?
      

  8.   

    FileStream参考