Tag: interop

当调用AddIns.Item 时,索引无效(从HRESULTexception:0x8002000B(DISP_E_BADINDEX)))

我调用Excel.AddIns.Item[0]时收到无效索引COMexception 。 Excel.AddIns.Count工作正常,返回4.但我不明白为什么Excel.AddIns.Item[0]无法返回第一个Excel.AddIn对象。 API似乎也没有太多的说。 http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.addins.item 谁能帮忙? 谢谢。

C#Excel互操作 – 如何testing互操作对象是否仍在工作并执行任务?

我正在循环浏览几个追踪的excel文件的目录,并尝试一次刷新一个excel文件。 我不断收到这个错误,指出刷新操作仍然在文件A上运行,例如,FileB正在尝试启动刷新操作。 循环是快速的,不知何故,我必须等待文件A上的先前刷新操作才能开始刷新文件B. 未处理的exception:System.Runtime.InteropServices.COMException:消息filter指示应用程序正忙。 (来自HRESULTexception:0x8001010A(RPC_E_SERVERCALL_RETRYLATER))在Microsoft.Office.Interop.Excel._Workbook.RefreshAll()在RefreshExcelFiles.Program.RefreshFile(string文件名) 这是我的代码。 在开始处理循环中的新文件之前,如何等待刷新操作完成? 或者,在开始新文件之前,等待wb对象的编组释放操作完成? using System; using System.Configuration; using System.IO; using System.Runtime.InteropServices; using Microsoft.Office.Interop.Excel; namespace RefreshExcelFiles { internal class Program { private static string _fileDirectory; private static Application _excelApp; private static void Main(string[] args) { _fileDirectory = ConfigurationManager.AppSettings["FileDirectory"]; _excelApp = new Application(); _excelApp.DisplayAlerts = false; Console.WriteLine("starting to refresh files"); int i […]

为什么我得到一个内存不足错误做ASP .NET Excel互操作?

这是工作 ..我把处理代码移动到finally块,现在每次都失败。 我有一个testing电子表格,有4个logging,6列长。 这里是我使用它的代码。这是IIS 5(我的电脑)和IIS 6(Web服务器)上的ASP .Net 3.5。 它会在catch之前的下一行发生:“values =(object [,])range.Value2;” 出现以下错误: 11/2/2009 8:47:43 AM :: Not enough storage is available to complete this operation. (Exception from HRESULT: 0x8007000E (E_OUTOFMEMORY)) 有任何想法吗? build议? 我从代码项目中得到了大部分代码,所以我不知道这是否是使用Excel的正确方法。 感谢您的任何帮助,您可以提供。 这是我的代码: Excel.ApplicationClass app = null; Excel.Workbook book = null; Excel.Worksheet sheet = null; Excel.Range range = null; object [,] values = […]

创build和保存一个Excel文件

我有以下代码,在我的C#代码后面创build一个新的Excel文件。 当我尝试保存文件时,我希望用户select保存的位置。 在方法#1中,我可以使用工作簿SaveCopyAs保存文件,而不会提示用户input位置。 这将一个文件保存到C:\ Temp目录。 方法#2将文件保存在我的Users \ Documents文件夹中,然后提示用户select位置并保存第二个副本。 如何从“用户\文档”文件夹中删除保存的第一个副本? Excel.Application oXL; Excel._Workbook oWB; Excel._Worksheet oSheet; Excel.Range oRng; try { //Start Excel and get Application object. oXL = new Excel.Application(); oXL.Visible = false; //Get a new workbook. oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value)); oSheet = (Excel._Worksheet)oWB.ActiveSheet; // ***** oSheet.Cells[2, 6] = "Ship To:"; oSheet.get_Range("F2", "F2").Font.Bold = true; oSheet.Cells[2, 7] […]

加载所有COMtypes的Exceldynamic?

我想探索这个dynamic使用Excel的path。 我想在我的C#应用​​程序中使用Excel,而不包括dll和东西。 如果所需的Excel版本已安装并运行,我将首先检查。 首先,我想获得所有我需要的types,但是我无法得到它们: // works Type typeExcel = Type.GetTypeFromProgID("Excel.Application"); object excel = Activator.CreateInstance(typeExcel); object workbooks = typeExcel.InvokeMember("Workbooks", BindingFlags.GetProperty, null, excel, null); // this doesn't work, returns null Type typeWorkbooks = Type.GetTypeFromProgID("Excel.Workbooks"); 没有正确的types,我不能调用成员。 那么我做错了什么? 如何加载我需要的所有types,并知道它们在那里? 我目前的Excel版本是2003年。 原因如下:如果我包含目标系统上没有安装的COM库,我的应用程序不会启动。 如果我dynamic加载它们,我可以检查它们的存在并通知用户有关缺less的function。

为什么excel保持开放?

可能重复: 如何正确清理C#中的Excel互操作对象 我有这个函数来计算一些数据的线性趋势: private string Trend(object conocido_y, object conocido_x, object nueva_matriz_x) { string result = String.Empty; try { Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); result = ((Array)xlApp.WorksheetFunction.Trend(conocido_y, conocido_x, nueva_matriz_x, true)).GetValue(1).ToString(); xlApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlApp = null; } catch (System.Runtime.InteropServices.COMException ex) { DError.ReportarError(ex, false); } catch (Exception ex) { DError.ReportarError(ex); } return result; } 结果很好,但excel应用程序不closures,如果我打开任务pipe理器的过程仍在运行,为什么?

该命令至less需要两行源数据

我得到这个错误: This command requires at least two rows of source data. You cannot use the command on a selection in only one row. Try the following: – If you're using an advanced filter, select a range of cells that contains at least two rows of data. Then click the Advanced Filter command again. – I […]

Excel互操作对象不被释放

我正在使用下面的方法。 在这个代码块执行后,我发现Microsoft Excel仍然通过检查任务pipe理器在后台运行。 编辑 :完全一个 Microsoft Excel后台进程仍然生活。 我刚刚添加了这个function,因为当我第一次开始使用Excel和Interop COM对象时,有时很多进程在函数执行完成后仍然保持运行。 如何确保在执行当前函数后Excel不在后台运行? 我用全局范围声明这些variables,因为几个函数最终需要它们: private Excel.Application xls = null; private Excel.Workbooks workBooks = null; private Excel.Workbook workBook = null; private Excel.Sheets sheets = null; private Excel.Worksheet workSheet = null; 此方法用于检查给定工作簿中是否存在工作表: private bool sheetExists(string searchString) { xls = new Excel.Application(); workBooks = xls.Workbooks; workBook = workBooks.Open(workbookPath); // <- Where […]

以编程方式创buildExcel实例并加载所有加载项

我正在.NET应用程序中创build一个Microsoft Excel的实例(如果它很重要的话),我就像这样embedded它: var excelType = Type.GetTypeFromProgID("Excel.Application"); var excelObj = Activator.CreateInstance(excelType); excelType.InvokeMember("Visible", BindingFlags.SetProperty, null, excelObj, new object[] { true }, ComCulture); var excelHwnd = new IntPtr((int)excelType.InvokeMember("Hwnd", BindingFlags.GetProperty, null, excelObj, new object[0], ComCulture)); var managedWindowHwnd = new WindowInteropHelper(Application.Current.MainWindow).Handle; SetParent(excelHwnd, managedWindowHwnd); MoveWindow(excelHwnd, 0, 0, Convert.ToInt32(Width), Convert.ToInt32(Height), true); 除Excel以外的所有作品都不会加载安装的加载项。 如果我通过开始菜单运行Excel,它将加载加载项。 我想我需要指定一些参数到Excel来启用加载项加载。 但是我不知道怎么样…

(object )range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault)导致转换错误

错误: Cannot convert type 'string' to 'object[*,*]' 这是我得到的错误。 有人能给我一些指针,以便我可以避免它? 谢谢。 注意: 有趣的是, (object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault) 当range.Count == 1时只会产生这个bug。 当count等于或大于2时,它工作正常。 示例代码: object[,] arrValue; //global variable private string[] createFormulaCollection() { ArrayList s = new ArrayList(); try { //look at the currently active excel sheet //iterate through cells (not Null) and find the one contains IES(…) //save it into the […]