Tag: 互操作

我是否需要在foreach循环中释放excel工作表对象?

我在我的网站项目中使用C#中的Excel互操作来parsingExcel文件并将其转换为文本文件。 示例代码如下。 Excel.Application excel=new Excel.Application(); Excel.Workbooks wbs=excel.Workbooks; Excel.Workbook wb=wbs.Open(…); Try { … Foreach(Excel.Worksheet sheet in wb.Sheets) { … Marshal.RealseComObject(sheet); sheet=null; //compiler error } … } Catch() { } Finally { wb.Close(false, missing, missing); while (Marshal.ReleaseComObject(wb)!=0) { } wb=null; while (Marshal.ReleaseComObject(wbs)!=0) { } wbs=null; excel.Quit(); while (Marshal.ReleaseComObject(excel)!=0) { } excel=null; GC.Collect(); GC.WaitForPendingFinalizers(); } 我的问题是我需要在foreach循环中释放工作表对象吗? 我看到一些关于用excel工作表进行编程的示例代码,但都没有。 如果我确实需要放弃对象,在我的foreach循环中,我无法将表单设置为null,是否意味着我不应该使用foreach?

“移动图表”不会在Excel Interop对象模型中创build新的工作表

要看到我遇到的问题,您必须使用Excel工作簿,它有一些特殊的东西。 因此,拥有Excel 2007/2010,在任何工作表上创build任何图表。 现在在创build的图表上单击鼠标右键,然后select“移动图表”菜单项。 现在你有两个select:“对象在”和“新表”。 select“新build工作表”选项并为新工作表指定一些名称。 例如将其命名为“AAA”。 结果你现在有了新的选项卡,选项卡的名字是“AAA”,看起来都是正确的。 现在到我的问题:)使用Microsoft.Office.Interop.Excel.dll打开此文档,并尝试find名称为“AAA”的工作表对象。 你不会find它。 现有工作表的数量等于Excel中工作表的数量,然后再执行这个神奇的“移动图表”操作。 所以,我假设MS有一些“使它成为一个选项卡”属性,可以为图表对象设置。 所以它看起来像Excel中的工作表,但它不是。 我的问题是如何获得互操作对象模型中的这些tab对象的集合? 为什么我需要这个? 如果它很重要 – 我有一些基于docx模板创builddocx报告的应用程序。 该模板可以包含不同的命令,告诉从其他文件中取出一些对象。 这些文件可能是一个Excel文件,这些对象可能是一个图表。 该模板由用户编辑一次,然后多次使用。 用户通常会打开Excel文件,并通过它来确定他应该写在这些命令。 而且,如果他在“AAA”名称的选项卡上看到某个图表,他将编写命令,要求从“AAA”工作表中取出图表…并且由于不存在这样的工作表,因此我的程序在此处失败。 所以,通过“标签式”的对象集合迭代肯定会帮助我在这里。 最好的问候,阿尔乔姆

C#Excel格式条件 – 无效的参数exception

我正在尝试在Excel中对偶数/奇数行进行着色。 这是我的代码: FormatCondition format = (FormatCondition)(range.FormatConditions.Add(XlFormatConditionType.xlExpression, XlFormatConditionOperator.xlEqual, "=MOD(ROW(),2) = 0")); Interior interior = format.Interior; interior.Color = ColorTranslator.ToOle(Color.Red); 当我尝试debugging,得到一个错误,说:“无效的参数。HRESULT返回0x80070057(E_INVALIDARG)”(对不起,不能发布图像,我没有那么多的声誉) 是的,我尝试从框架清除ASP.NET Temprory文件,但它并没有为我工作。 我试过这样的: FormatCondition format = (FormatCondition)(range.FormatConditions.Add(XlFormatConditionType.xlExpression, XlFormatConditionOperator.xlEqual, "=MOD(ROW(),2) = 0", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)); 我试图everthing我发现,但结果并没有改变VS每次给我“无效的参数”错误。 我该怎么办?

如何将excel列从一个文件复制到另一个?

我使用Excel插件应用程序来创build可以从一个Excel文件复制列到另一个function。 这是迄今为止的代码,但是当我渲染应用程序时,程序会输出一个空白的book.xls文件。 private void button1_Click(object sender, EventArgs e) { Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Excel.Workbook xlWorkBook; Excel.Workbook xlWorkBook2; Excel.Worksheet xlWorkSheet; Excel.Worksheet xlWsheet2; Excel.Range xlSourceRange; Excel.Range xlSourceRange1; Excel.Range xlDestRange; Excel.Range xlDestRange1; xlWorkBook = xlApp.Workbooks.Open("C:/../../../../../../../Test.xls"); xlWorkBook2 = xlApp.Workbooks.Open("C:/../../../../../../../Book1.xls"); //~~> Display Excel xlApp.Visible = true; //~~> Set the source worksheet xlWorkSheet = xlWorkBook.Sheets["Sheet1"]; //~~> Set the destination worksheet […]

Excel图表着色3D条形图

使用Excel和Interop对3D条形图进行着色不起作用: 创build图表: chartRange = xlsSheet.Range[xlsSheet.Cells[1, 1], xlsSheet.Cells[array.GetLength(0), array.GetLength(1)]]; chartPage.SetSourceData(chartRange, Excel.XlRowCol.xlRows); chartPage.ChartType = Excel.XlChartType.xl3DColumn; chartPage.Location(Excel.XlChartLocation.xlLocationAsNewSheet, oOpt); 改变颜色: Excel.Series series = (Excel.Series)chartPage.SeriesCollection(1); Excel.Point pt = series.Points(2); pt.Format.Fill.ForeColor.RGB = (int)Excel.XlRgbColor.rgbPink; 问题:图表内没有任何变化,但也没有错误。 只是在酒吧上显示这个随机的颜色。

使用Interop创buildExcel图表 – 更新到Office 2013时出错

我们有一个应用程序,可以根据这些数据导出包含数据和graphics的工作表。 在Office 2010上一切正常。当有些用户开始将他们的Office软件包升级到2013版本时,应用程序停止工作。 debugging我可以得到确切的一点是错误正在发生。 SeriesCollection series = chartPage.SeriesCollection(); Series serie = series.NewSeries(); // Error happens here! 错误是:返回参数有一个无效的types! 我错过了什么? 谢谢你的帮助!

从HRESULT:0x800A03ECexception,同时用interop打开文件时,错误消息Excel无法使用可用的资源继续执行此任务

我收到一条错误消息,说Excel无法用可用的资源继续执行此任务,然后从HRESULT:0x800A03EC发生exception,我认为这与我的Excel应用程序有关,我只尝试使用一次,然后使用对于我的应用程序中的所有操作,这是我的实例声明: namespace VID { public partial class VID_MAIN_FORM : Form { [DllImport("user32.dll")] static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId); public static Excel.Application xlApp = new Excel.Application(); System.Threading.ManualResetEvent _busy = new System.Threading.ManualResetEvent(false); Dictionary<string,Tuple<string, Dictionary<string, string>, List<Tuple<string, string>>,string>> FinalReportErrorList = new Dictionary<string, Tuple<string, Dictionary<string, string>,List<Tuple<string,string>>,string>>(); Tuple<string, Dictionary<string, string>,List<Tuple<string,string>>,string> Creator_Error; Dictionary<string,Tuple<string, string, string,Dictionary<string,Tuple<string,string>>,string>> SFinalReportErrorList = new Dictionary<string, […]

与Windows 2008 R2服务器上的Microsoft.Office.Interop.Excel程序集问题

我们的任务是以编程方式运行excelmacros。 在本地环境中,当我们按照以下步骤时,它工作正常: 1。 Microsoft.Office.Interop.Excel.Application appExcel = new Microsoft.Office.Interop.Excel.Application(); 2。 Microsoft.Office.Interop.Excel.Workbook workBook = appExcel.Workbooks.Open("FilePath", 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);` 3 appExcel.Run("Macro name", "$W$5", 1, 0, "$Q$11:$Q$112", 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, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, […]

如何使用PdfReport生成.xlsx文件

我正在使用PdfReport库来生成报告作为.pdf文件。 PdfReport是一个build立在Epplus和iTextsharp上的库,这就是为什么我们select这个库,因为我们想要生成PDF和EXCEL文件。 目前我们能够生成似乎无法生成Excel文件的PDF文件。 谷歌与这个图书馆没有太大的关系。 PdfReport 这是我的代码 public class IListPdfReport { public IPdfReportData CreatePdfReport(DataSet mTables) { return new PdfReport().DocumentPreferences(doc => { doc.RunDirection(PdfRunDirection.LeftToRight); doc.Orientation(PageOrientation.Portrait); doc.PageSize(PdfPageSize.A4); doc.DocumentMetadata(new DocumentMetadata { Author = "Vahid", Application = "PdfRpt", Keywords = "IList Rpt.", Subject = "Test Rpt", Title = "Test" }); doc.Compression(new CompressionSettings { EnableCompression = true, EnableFullCompression = true }); doc.PrintingPreferences(new […]

加载Excel XML并转换为Excel 2003二进制格式

我目前使用Excel Interop以xml格式加载xls文件,然后在VB.Net中保存function以将加载的对象保存为Biff8 xls格式。 但是,这只能在桌面上完成,如果我需要在服务器上完成这项工作,我将需要在那里安装Microsoft Office,这不是一个推荐的解决scheme。 这是可能的任何其他替代库? Excel.Application i = new Excel.Application(); i.DisplayAlerts = false; Excel.Workbook w = i.Workbooks.Open("C:\\Temp\\TempXml.xls"); w.SaveAs("C:\\Temp\\Temp1.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8); i.Workbooks.Close(); w.Close();