Excel读取图片无法读取,求解

wxxloveu 2011-07-08 02:18:05
有谁有读取excel里面图片的代码啊?我这个读取不了,vs2005读excel2003

Excel.Application excel1 = new Excel.Application();
Excel.Workbook wkb = excel1.Workbooks.Add(Type.Missing);
Excel.Worksheet sheet1 = wkb.Worksheets[1] as Excel.Worksheet;
Excel.Range range1 = sheet1.get_Range("G5:I6",Type.Missing) as Excel.Range;//.Cells[7, 8] as Excel.Range;

int row = 8;
IDataObject iData = Clipboard.GetDataObject();

Excel.Shape s = sheet1.Shapes.Item(row) as Excel.Shape;//这一句出问题 提示错误:不允许进入指定汇编中的索引
s.CopyPicture(Appearance.Button, Excel.XlCopyPictureFormat.xlBitmap); //COPY到内存。
if (iData.GetDataPresent(DataFormats.Bitmap))
{
pictureBox1.Image = (Bitmap)iData.GetData(DataFormats.Bitmap); //从内存取值;

pictureBox1.Image.Save(string.Format(@"D:\kkkkkkkkkkkkkkkkkkk\-" + sheet1.Shapes.Count.ToString() + "-.jpg")); //保存。

}
else
{
pictureBox1.Image = null;
}
...全文
1662 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
子夜__ 2011-07-11
  • 打赏
  • 举报
回复
FileStream

参考
wxxloveu 2011-07-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ssp2009 的回复:]

应该从cell里取吧?。。
[/Quote]
呵呵,你是说这样么?貌似无法复制到图片哦,

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);
快溜 2011-07-10
  • 打赏
  • 举报
回复
应该从cell里取吧?。。
wxxloveu 2011-07-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 etudiant6666 的回复:]

该错误的原因是item(8)中没有图片,你的row=8指的是第8张图片吗?如果是第8行的图片那就错了。
如果想通过位置判断,可以找shape.BottonRightCell
[/Quote]
谢谢,图片放在第8个格子里的,我想读取第那个格子里的内容
wxxloveu 2011-07-10
  • 打赏
  • 举报
回复


//判断剪贴板中是否存在图片,如果存在,则将图片显示到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里可以直接黏贴到表格中,可以黏贴我要的图片,但直接在外面黏贴就会报错,未将对象引用设置到对象的实例,这是怎么回事?
vvvoid 2011-07-10
  • 打赏
  • 举报
回复
英语诚可贵,高数价更高,若是富二代,二者皆可抛
窗户纸 2011-07-10
  • 打赏
  • 举报
回复
该错误的原因是item(8)中没有图片,你的row=8指的是第8张图片吗?如果是第8行的图片那就错了。
如果想通过位置判断,可以找shape.BottonRightCell
wxxloveu 2011-07-10
  • 打赏
  • 举报
回复
顶一下,可能会有高手帮忙看看
wxxloveu 2011-07-10
  • 打赏
  • 举报
回复
重新写了个代码,但是还是出错

//初始化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等图片文件,搞不定啊搞不定啊,要抓狂了
wxxloveu 2011-07-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 etudiant6666 的回复:]

引用 8 楼 wxxloveu 的回复:
Excel.Shape s = sheet1.Shapes.Item(row) as Excel.Shape;
错误提示:不允许进入指定汇编中的索引。
有什么好的解决方法么?

这里的row不要赋值8, item(i)的意思是表中的第i个shape,而不是单元格i的意思。
[/Quote]
非常感谢,我要读取H7格子里的图片怎么读取呢?好像我改还是提示这样的错误,好久了,帮忙看看啊
窗户纸 2011-07-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wxxloveu 的回复:]
Excel.Shape s = sheet1.Shapes.Item(row) as Excel.Shape;
错误提示:不允许进入指定汇编中的索引。
有什么好的解决方法么?
[/Quote]
这里的row不要赋值8, item(i)的意思是表中的第i个shape,而不是单元格i的意思。
wxxloveu 2011-07-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 etudiant6666 的回复:]

引用 4 楼 wxxloveu 的回复:
引用 2 楼 etudiant6666 的回复:

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

谢谢,图片放在第8个格子里的,我想读取第那个格子里的内容


其中一个可行的方法是对sheet.shapes进……
[/Quote]

谢谢回复,遇到一个问题,这句话保存
Excel.Shape s = sheet1.Shapes.Item(row) as Excel.Shape;
错误提示:不允许进入指定汇编中的索引。
有什么好的解决方法么?
窗户纸 2011-07-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wxxloveu 的回复:]
引用 2 楼 etudiant6666 的回复:

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

谢谢,图片放在第8个格子里的,我想读取第那个格子里的内容
[/Quote]

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

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧