Tag: interop

如何使用Excel Interop获取CustomDocumentProperties?

下面的代码用于获取Excel工作簿的自定义文档属性。 var xlApp = Globals.ThisAddIn.Application; // This works in VSTO Excel Add-in var xlApp = new global::Microsoft.Office.Interop.Excel.Application(); // This doesn't work anywhere xlApp.Visible = true; global::Microsoft.Office.Interop.Excel.Workbook workbook = xlApp.Workbooks.Open(file, false, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, false, Type.Missing, Type.Missing); global::Microsoft.Office.Core.DocumentProperties properties = workbook.CustomDocumentProperties; // Exception occurs here global::Microsoft.Office.Core.DocumentProperty property = properties["propertyname"]; […]

将Excel单元格中的文本与新行alignmentc#

当单元格有两行时,如何alignmentExcel中的文本? 我试过这个: for (int j = 1; j <= RowsCount; j++) { for (int i = 1; i <= 18; i++) { ws.Cells[j, i].Style.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; ws.Cells[j, i].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; } } 但是,这只适用于单元格有1行,就像ws.get_range [] …

将Excel的Range.Value2转换为string

我从C#应用程序中的Excel工作表中导入一些值。 一列基本上是一个文本,但可以包含任何值。 我使用了以下内容: Range range = … // getting this from Excel, works fine string myString = (string)range.Value2; 当单元格包含文本时,这是工作。 但是当它包含例如123时,debugging器显示范围为123.0 .Value2和转换为string失败,exception。 我想知道,如何编写各种单元格的最普遍的转换。 至less是string和整数,可能是浮动内容。

获取debugging模式下的工作表数 – MS Excel PIA

我无法访问Sheets .Count属性。 我正在使用Excel Interop。 我在debugging模式,我想这个: ?xlSheets.Count 这导致: (1):错误BC30456:“计数”不是“表格”的成员。 我不知道什么是错的,因为我在MSDN中看到有这样的属性 ! 这效果很好: ?xlSheets(1).Name 。 但Count失败…有可能得到床单的数量? 这些人有一个类似的问题 – 他们想.Worksheets.Add(.Worksheets.Sheets.Count) 。 最后他们没有得到计数 ,他们去了.Worksheets.Add(After:=.Worksheets(3)) … 更新: 令我非常高兴的是,经过进一步尝试/试验之后,很明显, 在debugging模式下, 只有在代码中没有这样的行时, Sheets.Count才能工作 。 在debugging此代码时,我可以访问Sheets.Count ,因为代码中存在此行。 Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1 Dim xlApp As Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkbooks As Excel.Workbooks Dim xlSheets As Excel.Sheets Private Sub btnCreate_Click(ByVal […]

在Excel中打开.dat(制表符分隔的文件),另存为.xls

我试图在Excel中打开一个.dat文件,它是制表符分隔的,然后把它保存为一个.xls文件。 到目前为止,我可以读取文件并将其保存为.xls文件,但由于某些原因,所有值都位于第一列,而不是制表符分隔它们。 (如果我在Excel中打开文件而没有使用我的程序,它将完全用制表符分隔) 这些是我用来打开和重新保存文件的两行代码。 xlWorkBook = xlApp.Workbooks.Open(f, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkBook.SaveAs(filename + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

Windows应用程序通过Interop.Excel库问题访问xls数据

我有一个.NET C#Windows应用程序,我需要读取一些Excel数据。 我已经开始编写一些unit testing..第一个刚刚trys实例化工作簿之一,它是抛出这个错误… System.Runtime.InteropServices.COMException:System.Runtime.InteropService.COMException:检索具有CLSID {GUID}的组件的COM类工厂失败,原因如下:80040154未注册的类{从HRESULTexception:0x80040154(REGDB_E_CLASSNOTREG)) 公共工作簿RetrieveWorkbook(){返回新的工作簿();这个应用程序真的很简单,一个方法。 } 和一个调用这个函数的unit testing。 代码是使用Microsoft.Office.Interop.Excel。 我进入了dcomconfiguration工具,并validation访问似乎是正确的。 我错过了什么? 有什么想法吗?

使用ASP.NET自动化Excel

背景 我们正在使用ASP.NET 2.0开发一些内部实用程序。 其中之一是从数据库中提取一些信息,并build立一个Excel工作簿,其中包含许多基于查询数据的电子数据表。 问题 概念validation原型(从数据库查询单个项目并打开Excel以将数据添加到工作表的简单ASP.NET页面)在开发机器上本地运行时运行良好,可以愉快地创build和显示Excel电子表格按照要求。 但是,在我们的服务器上运行时,尝试实例化Excel时出现以下错误。 无法将“Microsoft.Office.Interop.Excel.ApplicationClass”types的COM对象转换为接口types“Microsoft.Office.Interop.Excel._Application”。 此操作失败,因为具有IID“{000208D5-0000-0000-C000-000000000046}”的接口的COM组件上的QueryInterface调用由于以下错误而失败:没有此类接口支持(exception来自HRESULT:0x80004002(E_NOINTERFACE)) 。 解? 我们使用PIA for Excel 2003,并且在服务器上安装了Excel 2003和PIA。 任何人都可以解释为什么这不起作用,或给我们一些提示,我们可以跟踪这个问题? 感谢您提供的任何帮助。

在.NET中查看Excel是否处于单元格编辑模式

我有一个用VB.NET编写的应用程序,通过互操作与Excel交互。 我最终遇到了单元格编辑模式的已知问题(请参阅MSDN和一些背景的计算器)。 我一直在尝试将build议的代码转换为VB.NET,但不断收到以下错误: Reference required to assembly 'office, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' containing the type 'Microsoft.Office.Core.CommandBars'. Add one to your project. (BC30652) – E:\ … .vb:3471 原来的C#代码(从前面提到的文章)如下 private bool IsEditMode() { object m = Type.Missing; const int MENU_ITEM_TYPE = 1; const int NEW_MENU = 18; // Get the "New" menu item. CommandBarControl oNewMenu = Application.CommandBars["Worksheet Menu […]

Excel Interop – 取消select

我在Excel工作表中复制和插入行,如下所示: while (rowsToAdd > 0) { // copy the existing row insertionCell.EntireRow.Copy(Type.Missing); // location of the new row Range newRow = insertionCell.EntireRow.get_Offset(1, 0).EntireRow; // insert the new row newRow.Insert(XlInsertShiftDirection.xlShiftDown, Type.Missing); rowsToAdd–; } 我遇到的问题是,有时我在原本复制的行周围留下一个select框。 有没有办法可以取消select框(你通常用Escape键做的方式?)

这怎么可能不相等?

我的目标:从Excel Range提取一个值,并validation这些单元格的值是否在此范围内相同; 当一个单元格的值不同于另一个时,我需要返回null。 这是一段代码: internal object GetValueFromCells(string start, string end, Formats format) { // Verifying for empty or null parameters here and throwing accordingly… try { Range cells = Excel.get_Range(start, end) as Range; object value = null; bool sameValue = false; foreach(Range cell in cells) { // This condition block shall execute only once, since […]