C#Excel检查单元格在VSTO中包含一个名称(C# Excel Check cell contains a name in VSTO)
我试图检查单元格是否包含名称或是否为命名范围,如果没有,我将指定一个名称。 这是我的代码:
if (cell.Name.Name == null) { Globals.ThisWorkbook.Names.Add("Temp", cell); } else { // Move on }
但是,上面的代码将抛出
COMException
。 相反,我试图通过这样做来绕过它:try { if (cell.Name.Name == null) { } } catch (COMException) { Globals.ThisWorkbook.Names.Add("Temp", cell); }
第二个代码片段有效,但我的电子表格受到严重影响。 操作从约80毫秒到1700毫秒。 这可能看起来不多,但我正在循环范围选择。
错误消息是:
System.Runtime.InteropServices.COMException was unhandled by user code HResult=-2146827284 Message=Exception from HRESULT: 0x800A03EC Source="" ErrorCode=-2146827284 StackTrace: at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) at Microsoft.Office.Interop.Excel.Range.get_Name() at ExcelTemplate1.Sheet1.button2_Click(Object sender, EventArgs e) in c:\Users\User\Desktop\ExcelTemplate1\ExcelTemplate1\Sheet1.cs:line 116 at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) InnerException:
问题是,有没有更好的方法来检查单元命名范围?
I am trying to check if a cell contains name or is a named range, if not, I will assign a name. Here is my code:
if (cell.Name.Name == null) { Globals.ThisWorkbook.Names.Add("Temp", cell); } else { // Move on }
However, the above code will throw a
COMException
. Instead, I tried to get around it by doing this:try { if (cell.Name.Name == null) { } } catch (COMException) { Globals.ThisWorkbook.Names.Add("Temp", cell); }
The second code snippet worked but my spreadsheet is taking a serious performance hit. The operation went from ~80 ms to 1700 ms. This may not seem much but I am looping over a range selection.
The error message was:
System.Runtime.InteropServices.COMException was unhandled by user code HResult=-2146827284 Message=Exception from HRESULT: 0x800A03EC Source="" ErrorCode=-2146827284 StackTrace: at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) at Microsoft.Office.Interop.Excel.Range.get_Name() at ExcelTemplate1.Sheet1.button2_Click(Object sender, EventArgs e) in c:\Users\User\Desktop\ExcelTemplate1\ExcelTemplate1\Sheet1.cs:line 116 at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) InnerException:
Question is, is there a better way to check for cell named range?
满意答案
您收到错误,因为cell.Name本身引发异常,更不用说cell.Name.Name。
Globals.ThisWorkbook.Names.Item(1)
为您提供工作簿中定义的第一个名称。 它给出的值如=Sheet1!$E$10:$F$12
和=Sheet1!$C$5
来表示命名范围所指的内容。
Globals.ThisWorkbook.Names.Item(1).Name
将为您提供范围的实际名称。 例如,在你的情况下Temp
。 这是一种检查方法。其他方式,在您的代码中,您正在检查名称是否为空,如果是,您是否再次分配它。 多次运行此代码没有任何害处:
Globals.ThisWorkbook.Names.Add("Temp", cell);
您可以跳过检查并改为运行此语句。 如果名称已经存在,它将覆盖。 大不了!!!
You are getting an error because cell.Name itself raises exception, let alone cell.Name.Name.
Globals.ThisWorkbook.Names.Item(1)
gives you first of the names defined in your workbook. It gives values like=Sheet1!$E$10:$F$12
and=Sheet1!$C$5
to indicate what that named range refers to.
Globals.ThisWorkbook.Names.Item(1).Name
shall give you actual name of the range. e.g.Temp
in your case. That is one way to check.Other way, in your code you are checking if the Name is null and if it is you are assigning it again. There is no harm in running this code multiple number of times:
Globals.ThisWorkbook.Names.Add("Temp", cell);
You can skip the check and run this statement instead. If the name is already there, it'll overwrite. Big deal!!!
相关问答
更多Excel VSTO如何工作?(How does Excel VSTO Work?)
从Excel加载项vsto访问特定的单元格(access specific cell from excel add-in vsto)
如何使用C#在Excel工作表中获取当前或集中的单元格值(How to get current or focussed cell value in Excel worksheet using C#)
VSTO c#检查单元是否为空(VSTO c# Check if a cell is null)
C#Excel检查单元格在VSTO中包含一个名称(C# Excel Check cell contains a name in VSTO)
C#vsto拖放到单元格,在删除之前获取单元格对象(C# vsto drag drop to cell, get the cell object before dropping)
使用VSTO C#在Excel单元格下拉列表中列表项是否有任何限制?(Is there any limit on list items in Excel cell dropdown using VSTO C#?)
C#Excel VSTO - 取消选择所有单元格(C# Excel VSTO - Deselect all cells)
在C#vsto Excel中粘贴特殊功能(Paste Special in C# vsto Excel)
如何使用VSTO从excel中获取单元格值?(How to best fetch a cell value from excel using VSTO?)
相关文章
更多POI 操作Excel公式
这种的EXCEL表格 怎么来解析??
JXLS根据excel模板生成EXCEL并下载
Becoming a data scientist
jxl解析excel并导入数据库
lucene读取word,excel,pdf
POI给单元格数据添加超链接
使用POI操作Excel和Word
POI Cells单元格数据处理
java+jsp来获取excel的数据?
最新问答
更多Python / IPython奇怪的不可重现列表索引超出范围错误(Python/IPython strange non reproducible list index out of range error)
故事板从表格单元中延伸出来并显示其详细信息披露按钮(storyboard segue from a tablecell an its Detail Disclosure Button)
我们可以将Gherkin功能文件与testcomplete集成(Can we integrate Gherkin feature files with testcomplete)
TrustAllCertificatesCallback被忽略(TrustAllCertificatesCallback is ignored)
返回嵌套元素的索引(Return index of nested element)
在数组中重新编号元素的有效方法(Efficient way of re-numbering elements in an array)
Express app定义的`static`文件夹无法正常工作(Express app defined `static` folder not working properly)
Javascript错误:未捕获TypeError:无法读取null的属性'value'[duplicate](Javascript error: Uncaught TypeError: Cannot read property 'value' of null [duplicate])
使用node.js和socket.io每秒广播一次(Using node.js and socket.io to broadcast every second)
如何让XMLSerializer将命名空间添加到嵌套对象中的属性?(How do I get the XMLSerializer to add namespaces to attributes in nested objects?)
Copyright ©2023 656463.com All Rights Reserved.滇ICP备2022006988号-50
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们,本站将尽快处理。谢谢合作!