Tag: excel

VBA和“类不支持自动化或不支持预期的接口”

我有ComVisible类的.NET程序集。 几天前(我可以通过git确切地发现那个时刻)发生了一些事情–Excel不再看到这个类了。 所以: 我有接口 [ComVisible(true)] [InterfaceType(ComInterfaceType.InterfaceIsDual)] public interface MyInterface { … } 和class级 [ClassInterface(ClassInterfaceType.None)] [ComVisible(true)] [ProgId(“MyClass.Id”)] public class MyClass { … } Excel通过创建实例 Dim c as MyClass Set c = New MyClass 早期的一切都像魅力,但删除一些方法(从界面和类)后Excel很疯狂 – 它向我显示错误Class doesn’t support Automation or does not support expected interface 。 哪里可以成问题? 我查了几次。 删除某些方法后发生错误 这个类是部分的(可能问题出在这里?) 早些时候一切都工作..

EPPLUS:DataValidation列表的长度不能超过255个字符

这个问题在另一篇文章的基础上回答: 这里但是对于我的情况,我无法将validation值硬编码到工作表中,我根据单元格的内容从数据库中提取它们,并且需要做一个每行4列的单独validation。 有没有办法实现这一目标? 先感谢您。 // Data Validations // // Product Validation // for (int i = 2; i o.TTSTicketNumber == ws.Cells[i, 3].Value.ToString()).Single(); var prodIds = prodExtQueryable.Where(p => p.ZoneId == ticketEntity.ZoneId && p.TicketTypeId == ticketEntity.TicketTypeId); if (ticketEntity != null) { var prodIdsList = new List(); foreach (var prodId in prodIds) { prodIdsList.Add(prodId.ProductId); } var ProductList = […]

如何模拟Excel VSTO插件中的行?

我试图在一个新的Range行中放置一个模拟的Range (包含值的单元格)。 但是当我尝试从Range访问特定元素时,会抛出exception。 我已经尝试了一切,有没有人知道我在这里做错了什么? 例外 消息:测试方法xxx.MockUtilsTest.MockRowsTest抛出exception:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法将带有[]的索引应用于类型为“Castle.Proxies.RangeProxy”的表达式 测试 [TestMethod] public void MockRowsTest() { var row1 = MockUtils.MockCells(“test_row_1”, “test_row_1”); var row2 = MockUtils.MockCells(“test_row_2”, “test_row_2”); var range = MockUtils.MockRows(row1, row2); Assert.IsNotNull(range); Assert.AreEqual(2, range.Count); Assert.IsNotNull(range.Rows); Assert.AreEqual(2, range.Rows.Count); Assert.AreSame(row1, range.Rows[1].Cells[1]); // exception is thrown here Assert.AreSame(row2, range.Rows[2].Cells[1]); Assert.AreEqual(“test_row_1”, range.Rows[1].Cells[1].Value2); Assert.AreEqual(“test_row_2”, range.Rows[2].Cells[1].Value2); } MockUtils public static Range MockCellValue2(Object value) { var […]

传递VBA类的实时实例时,C#ITypeInfo.GetContainingTypeLib失败

所以我已经尝试在VBA类实例上调用ITypeInfo,虽然看起来很有希望但我想看看我是否可以获得对其包含项目的引用,类似于类型库。 我认为ITypeInfo.GetContainingTypeLib可能有用,但它抛出一个exception,表明VBA不会合作。 任何人都对VBA如何以不同于标准COM规范的方式做出任何想法? C#类库代码在这里。 注册COM interop并在AssemblyInfo.cs中设置COMVisible(true)以使其可从VBA访问。 下面给出的VBA客户端代码。 using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; namespace TypeLibraryInspector { [ComImport()] [Guid(“00020400-0000-0000-C000-000000000046”)] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IDispatch { [PreserveSig] int GetTypeInfoCount(out int Count); [PreserveSig] int GetTypeInfo ( [MarshalAs(UnmanagedType.U4)] int iTInfo, [MarshalAs(UnmanagedType.U4)] int lcid, out System.Runtime.InteropServices.ComTypes.ITypeInfo typeInfo ); //void GetTypeInfo(int typeInfoIndex, int lcid, [MarshalAs(UnmanagedType.CustomMarshaler, // MarshalTypeRef = typeof(System.Runtime.InteropServices.CustomMarshalers.TypeToTypeInfoMarshaler))] out Type typeInfo); […]

C#中的RTD服务器 – 从哪里开始

我在Excel中有RTD服务器,它从thinkorswim应用程序中提取数据。 我想将数据提取到C#中。 我的编程经验有限,因此在C#中阅读有关RTD服务器实现的多个教程,但其中大部分都是针对我的两个高级教程。 所以我想知道是否有人可以提出一些介绍性的阅读材料。 以下是我尝试实现的示例代码: http://awkwardcoder.com/2014/01/24/excel-rtd-client-in-c/ 我将其复制并粘贴到Visual Studio(VS)中并调用类RtdClient.cs。 VS立即突出显示以下行: ComImport, TypeLibType((short)0x1040) MarshalAs MethodImpl 并写入无法找到的类型和命名空间。 我错过了任何参考资料吗? 我添加了关于COM类型Tos.RTD的参考,但它没有帮助。 同样在注册表中我找到了带有Tos.RTD和CLSID的文件夹。 我假设CSLID指向COM类型? 在VS Tos.RTD有几个接口。 在上面的链接中,我没有看到这些接口的方法的实现。 怎么了? 我还通过反思阅读了RTD,我知道第二个链接依赖于reflection。 这两种方法的优点/缺点是什么? 哪一个在概念上更有意义? 你似乎我迷路了,所以任何建议都会非常感激。

在ClosedXml中“无法将主题颜色转换为颜色”

我试图使用ClosedXml读取Excel中单元格的填充背景颜色。 我正在使用此示例代码,并且能够无问题地读取Excel文档的内容,但无法将单元格的填充 BackgroundColor读作hex值。 我能够看到在BackgroundColor下定义的ThemeColor和ThemeTint属性,但是没有找到将它们转换为System.Color或hex值的方法。 这是我的代码: // Get all categories while (!categoryRow.Cell(coCategoryId).IsEmpty()) { IXLCell categoryName = categoryRow.Cell(coCategoryName); categories.Add(categoryName.GetString() + ” ” + XLColor.FromTheme(categoryName.Style.Fill.BackgroundColor.ThemeColor, categoryName.Style.Fill.BackgroundColor.ThemeTint).Color.ToHex()); categoryRow = categoryRow.RowBelow(); } 似乎XLColor.FromTheme方法总是抛出exception“无法将主题颜色转换为颜色”。 有没有人知道从ThemeColor和ThemeTint值获取System.Color的另一种方法? 更新: 我没有提到我已经尝试使用BackgroundColor的Color属性,遗憾的是这没有正确填充,如果你在调试器中查看它,那么你会看到这个属性抛出了我用方法XLColor获得的exception。 FromTheme 。 所以这肯定看起来像ClosedXml中的一个错误。 有谁知道解决方法?

在Excel工具栏中添加自定义图像到按钮

我正在开发一个微软excel插件并添加了一个按钮,但我需要添加自定义图像。 如何实现这一目标

如何在C#中获取Excel 2003单元格的屏幕X和Y

在编写C#Excel 2003加载项时,如何在Excel 2003中找到单元格的绝对位置(例如,相对于屏幕[s])。 范围的Top和Left属性(例如ActiveCell)似乎相对于左上角单元格给出X和Y. Window.Left和Top给出了窗口的X和Y,但我找不到一种方法来获得中间位的大小(由工具栏等组成)。 这里的目的是显示与所选单元格相关的WPF表格,并且与其相邻。 我觉得我在这里遗漏了一些基本的东西。 任何帮助非常感谢!

如何以编程方式将XML数据导入Excel文件?

以下代码应该可以帮助我从Excel导出XML数据: Excel.Application application = new Excel.Application(); application.Workbooks.Open(excelFilepath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); string data = null; application.ActiveWorkbook.XmlMaps[1].XmlExport(out data); 现在我正在寻找可以帮助我将XML数据导入Excel的代码,就像我通过单击“Developer”选项卡然后“Import XML”手动导入文件一样。 有人能告诉我怎么做吗?

从VBA调用Rest API – “与服务器的连接exception终止”

我正在尝试从Excel VBA RESTfull API。 我已经在C#中使用了一个工作版本: //Request Auth Token var client = new RestClient(“https://api.xxx.com/exp/oauth2/v1/access_token_cors”); var request = new RestRequest(Method.POST); request.AddHeader(“Content-Type”, “application/x-www-form-urlencoded”); request.AddParameter(“application/x-www-form-urlencoded”, “response_type=token&grant_type=client_credentials&client_id=1234&client_secret=1234&scope=”, ParameterType.RequestBody); IRestResponse response = client.Execute(request); 需要将此代码移植到VBA。 我写: Set MyRequest = CreateObject(“WinHttp.WinHttpRequest.5.1”) MyRequest.Open “POST”, “https://api.xxx.com/exp/oauth2/v1/access_token_cors”, False MyRequest.setRequestHeader “Content-Type”, “application/x-www-form-urlencoded” PostData = “””application/x-www-form-urlencoded””, “”response_type=token&grant_type=client_credentials&client_id=1234&client_secret=1234&scope=””” MyRequest.send (PostData) 当我运行VBA版本时,我收到错误“与服务器的连接exception终止”。发送行 由于它在C#中工作,因此不能成为防火墙或服务器问题。 我能做些什么才能让它发挥作用? 我搜索过类似的问题,但没有一个适用于我的情况。