使用C#在Excel中查找绘制的箭头

我想知道是否有一种方法可以在使用C#的Excel电子表格中查找绘制的箭头。 例如,如果有从A2到A6绘制的箭头,C#程序是否可以打开该excel文件,并search箭头,然后返回单元格A2和A6或其他types的内容。 如果这听起来像一个愚蠢的问题,但道歉,这是一个“pipe理问题”,我似乎无法find答案。 谢谢大家。

当您在Excel中谈论​​箭头形状时,则有28种types的箭形。 看下面的图片

在这里输入图像说明

整个列表可以在这里find

所以当你通过形状循环时,你将不得不考虑所有这些。 另外理查摩根在评论中提到的是正确的。 您将不得不使用TopLeftCell.AddressBottomRightCell.Address来查找范围。

你的代码看起来像这样。 (顺便说一下,我正在使用Interop来自动化Excel)

假设我们的工作表是这样的。 现在我们不想要红色形状,因为它们不是箭形。

在这里输入图像说明

我的假设

C:\有一个名为Shapes.xlsx的工作簿,我们必须从中检索信息

代码 (在VS 2010和Excel 2010中尝试和testing)

 using System; using System.Windows.Forms; using System.IO; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application xlexcel; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlexcel = new Excel.Application(); xlexcel.Visible = true; // Open a File xlWorkBook = xlexcel.Workbooks.Open("C:\\Shapes.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); // Set Sheet 1 as the sheet you want to work with xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); foreach (Microsoft.Office.Interop.Excel.Shape Shp in xlWorkSheet.Shapes) { // Disclaimer: I am a vb.net guy so if you feel the syntax of switch should be written // in some other way then feel free to edit it :) In VB.Net, I could write // Case 33 To 60 instead of writing so many cases. switch ((int)Shp.AutoShapeType) { case 33: case 34: case 35: case 36: case 37: case 38: case 39: case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59: case 60: MessageBox.Show("Shape Found in Cell Range from " + Shp.TopLeftCell.Address + " to " + Shp.BottomRightCell.Address); break; } } //Once done close and quit Excel xlWorkBook.Close(false, misValue, misValue); xlexcel.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlexcel); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Unable to release the Object " + ex.ToString()); } finally { GC.Collect(); } } } } 

这些是我得到的3个消息框

在这里输入图像说明