Excel读取图片无法读取,求解 本帖最后由 wxxloveu 于 2011-07-10 11:21:31 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 该错误的原因是item(8)中没有图片,你的row=8指的是第8张图片吗?如果是第8行的图片那就错了。如果想通过位置判断,可以找shape.BottonRightCell 呵呵,你是说这样么?貌似无法复制到图片哦, 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); 其中一个可行的方法是对sheet.shapes进行遍历,假设图片的右下角位于第八行,第1列的情况,逐个找shape..BottonRightCell.Row=8, col=1的shape就可以啦。 谢谢回复,遇到一个问题,这句话保存 Excel.Shape s = sheet1.Shapes.Item(row) as Excel.Shape;错误提示:不允许进入指定汇编中的索引。有什么好的解决方法么? 这里的row不要赋值8, item(i)的意思是表中的第i个shape,而不是单元格i的意思。 重新写了个代码,但是还是出错//初始化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等图片文件,搞不定啊搞不定啊,要抓狂了 //判断剪贴板中是否存在图片,如果存在,则将图片显示到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里可以直接黏贴到表格中,可以黏贴我要的图片,但直接在外面黏贴就会报错,未将对象引用设置到对象的实例,这是怎么回事? FileStream参考 C# 下引用FBX文件发生的错误 ······· 请教 Attempted to read or write protected memory. This is often an indication that ot 为什么我删除操作的时候程序会莫名的报错 C#如何把名片mp3,wav等格式的声音文件存储到数据库中并读出来啊 功能强大的DevExpress控件组中gridControl控件数据显示的问题 异常 datagrid固定列的问题 DataGridView中数据转化问题 求助:关于Forms认证的问题 Winform中Excel导入到指定的表中 关于c# 智能卡储物柜 读写卡程序问题
如果想通过位置判断,可以找shape.BottonRightCell
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);
其中一个可行的方法是对sheet.shapes进行遍历,假设图片的右下角位于第八行,第1列的情况,逐个找shape..BottonRightCell.Row=8, col=1的shape就可以啦。
谢谢回复,遇到一个问题,这句话保存
Excel.Shape s = sheet1.Shapes.Item(row) as Excel.Shape;
错误提示:不允许进入指定汇编中的索引。
有什么好的解决方法么?
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等图片文件,搞不定啊搞不定啊,要抓狂了
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里可以直接黏贴到表格中,可以黏贴我要的图片,但直接在外面黏贴就会报错,未将对象引用设置到对象的实例,这是怎么回事?