Tag: excel interop

使用C#Interop.Excel删除NamesManager中的重复名称

我正在面临一些问题,从C#代码中删除名称NAmesManager。 根据我的要求,我需要从名称pipe理器中删除所有的无效名称。 我正在检查名称的值,并删除它,如果值是“#REF!”。 这是我正在使用的代码 foreach (Name RangeName in namesManager2) { if(RangeName.Value.Contains("#REF!")) { RangeName.Delete(); } } 代码工作正常,但有一些奇怪的情况下,有两个名称相同的名称,但范围是不同的。 考虑将单元格1命名为“TESTNAME”,范围为“Workbook”,单元格2也命名为“TESTNAME”,范围为“Sheet1”。 引用Cell2的名称具有有效值。 所以当我循环如果名称与“#REF!” 以上代码遇到的值是删除这两个名字。 我想保留名称的有效值,但只删除无效的名称。 有人可以build议如何做到这一点?

Excel应用程序单例 – 只使用一个Excel应用程序实例而不退出的可能威胁?

我正在使用Excel进行报告的WPF应用程序。 每当我需要Excel应用程序实例生成新报表时,需要花费太多时间来打开新的Excel应用程序。 为什么不保留一个Excel应用程序实例作为一个单例打开只为我的应用程序? 当我的应用程序closures时退出此Excel单身人士? 我认为,Excel应用程序实例保持打开的风险很小。 或者这样做有没有隐藏的威胁? 我的代码: private static Microsoft.Office.Interop.Excel.Application _ExcelApp = null; public static Microsoft.Office.Interop.Excel.Application ExcelApp { get { return _ExcelApp; } private set { _ExcelApp = value; } } public static void QuitExcel() { if (ExcelReports.ExcelApp != null) { ExcelReports.ExcelApp.Quit(); ExcelReports.ExcelApp = null; } } public static void StartExcel() { try { ExcelReports.ExcelApp […]

从HRESULTexception:0x800A03EC错误创buildExcel文件时

我创build了Windows服务,需要每2小时创build一次Excel。 但是它给了我一个错误如下 Exception from HRESULT: 0x800A03EC 第一次创buildexcel文件,但是第二次出错。 尝试了很多东西,但失败了 请帮助我。 Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005. 这个错误也是在第一个错误发生之后。 public void WriteExcel() { string ExcelGen = "ExcelGen"; try { string fileNm = DateTime.Now.ToString("dd-MM-yyyy_HH") + ".xls"; string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads\\" + fileNm; string ServiceDbName = ConfigurationManager.AppSettings["ServiceDBName"].ToString(); […]

在c#中的Excel过程

我在使用Excel InterOp的excel进程ID查杀中遇到问题 第一种情况,当我打开excel文件,而我的程序访问Excel中的数据时,我打开excel文件的过程是访问程序的过程。 所以当我杀死进程ID,另一个我使用程序打开的Excel将被杀死。 第二种情况,如果我不杀死excel进程ID,当我尝试使用用户在我的程序访问之前打开的进程时,当我的程序工作,如果用户closuresexcel我的程序将终止。 有没有一种方法使用新的进程,只限制我的工作簿使用,如果其他excel已经打开,我怎么能让他们通过另一个进程打开? 这个我的代码, public static void getExceltable() { Microsoft.Office.Interop.Excel.Application m_app; m_app = new Microsoft.Office.Interop.Excel.Application(); m_app.DisplayAlerts = false; object misValue = System.Reflection.Missing.Value; string filename = Core.Class1.importPath; Workbook workbook=m_app.Workbooks.Open(filename,Type.Missing, Type.Missing,Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing,Type.Missing,Type.Missing,Type.Missing ,Type.Missing,Type.Missing,Type.Missing, Type.Missing); Worksheet sheet = m_app.Sheets[1]; //// My Excel Process //// workbook.Close(); int id; GetWindowThreadProcessId(m_app.Hwnd, out id); Process excelProcess = […]

Office Interop读取不正确的单元格值

尝试遍历Excel电子表格中的行和单元格,删除空行。 我正在使用下面的例程来做到这一点。 foreach(Range row in sheet.UsedRange.Rows) { for (int i = 0; i < row.Columns.Count; i++) { Range cell = row.Cells[1, i + 1]; if (cell.Value == null || String.IsNullOrEmpty(cell.Value.ToString())) { cell.Delete(); } } } 哪些工作正常的前两行。 然而,它似乎去干掉。 第三行是完全空的。 然而,当它遍历列时,当这个循环到达“I”列时,它在那里读取一个值。 该值实际上在第4行“J”列中。 之后,它变得越来越糟糕,缺less整行,并从它find的行读取不正确的值。 我对此感到困惑。 有什么明显的,我错过了?

在其他工作簿上运行Excelmacros

我正试图从另一个工作簿上的一个工作簿运行VBAmacros。 我正在使用Microsoft.Office.Interop.Excel(或等价物)对象(如果要使用它,请将引用(COM)添加到Microsoft Excel 12.0对象库)以编辑和使用C#可执行文件中的Excel文档。 例如,文档1中有一些数据。 文档2包含用于格式化文档1的macros。 文件一是每天新的,所以我不能在那里存储macros。 我试图使用Excel.Application.Run()在文档1上的文档2中运行macros。 我使用的示例macros很简单(存储在Microsoft Excel对象:ThisWorkbook中): Sub Test() Sheets("Sheet1").Select Range("A1").Value = 32 End Sub 我需要这个在不同的工作簿上运行。 我可以使用以下代码在相同的工作簿上运行它: Application xlApp = new Application(); //Excel app Workbook xlWbk = null; try { xlWbk = xlApp.Workbooks.Open(DocumentTwoLocation); xlApp.Run("'" + DocumentTwoLocation + "'!" + MacroName); //MacroName example: ThisWorkbook.Test } finally { if (xlWbk != null) try { […]

Excel应用程序在设置宽度方面有一个例外

我想通过WPF项目来控制Excel应用程序。 var xla = new Microsoft.Office.Interop.Excel.Application(); xla.Width = 400; // This line is throwing the below exception. 它几次工作正常,几天后,同一行给出了一个例外如下所示。 注销并login到系统暂时修复问题。 System.Runtime.InteropServices.COMException was unhandled HResult=-2146827284 Message=Exception from HRESULT: 0x800A03EC Source=Microsoft.Office.Interop.Excel ErrorCode=-2146827284 StackTrace: at Microsoft.Office.Interop.Excel.ApplicationClass.set_Width(Double RHS) at EmbedExcel1.Form1.button1_Click(Object sender, EventArgs e) in d:\Test Projects\EmbedExcel1\EmbedExcel1\Form1.cs:line 27 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons […]

Excel Interop – 如何search格式的单元格?

我看到Excel互操作程序有Find方法,但我不知道如何使用它。 我想在范围(Microsoft.Office.Interop.Excel.Range)中search具有特定颜色的单元格。 在Excel中,你会这样做: Ctrl + F打开“查找和replace”对话框。 保留文本框“查找内容:”为空。 点击button[选项>>]或Alt + T 单击button[Format …]或Alt + M 转到选项卡字体 – >单击颜色选项(Alt + C)并select颜色。 点击确定,然后点击[查找下一个] 我只是想用Excel Interop来编程。 谢谢阅读 :)

检索Excel工作表单元格值

我正在使用c#读取excel表单的特定单元格的值。 但根据我的代码,我没有得到任何价值.. 我的代码获取单元格值是: foreach (DataRow dr in sourceds.Tables[0].Rows) { while (clmn < dr.ItemArray.Count()) { value = ((Microsoft.Office.Interop.Excel.Range)ws.Cells[row,clmn]).Text.ToString(); ws.Cells[row, clmn] = value; ws.Cells[row, clmn] = value; clmn++; } row++; } wb.Save(); 在这里,我正在读取其他工作表的单元格,并希望将该值插入到其他工作表中。 但我没有得到任何“价值”的价值..

如何使用NetOffice重命名Excel工作表?

我正在尝试使用NetOffice在我的c#应用程序中创buildexcel工作簿,并且遇到了一个尝试重命名工作表和在单元格范围上使用sorting的障碍。 以下代码行无法被NetOffice识别,我正努力寻找一种方法来重命名工作表,并使用NetOffice对单元格范围进行分类。 workbook.Worksheets[sheetCountPlusONe].name = "Event " + sheetCountPlusONe.ToString() + " Results"; 而这也是行不通的。 这是我声明和sorting一系列单元格的地方: Excel.Range valueRange; Excel.Range placeRange; placeRange = worksheet.get_Range("A14", "A" + (14 + (registrationForm.numberofCompetitors – 1)).ToString()); valueRange = worksheet.get_Range("A14", "K"+(14+(registrationForm.numberofCompetitors-1)).ToString()); valueRange.Sort(valueRange.Columns[3, Type.Missing], Excel.XlSortOrder.xlDescending, Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortOrder.xlAscending, Excel.XlYesNoGuess.xlGuess, Type.Missing, Type.Missing, Excel.XlSortOrientation.xlSortColumns, Excel.XlSortMethod.xlPinYin, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal, Excel.XlSortDataOption.xlSortNormal); placeRange.Sort(placeRange.Columns[1, Type.Missing], Excel.XlSortOrder.xlAscending, Type.Missing, Type.Missing, Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortOrder.xlAscending, […]