相关疑难解决方法(0)

如何在不安装Ms Office的情况下在C#中创建Excel(.XLS和.XLSX)文件?

如何使用C#创建Excel电子表格而无需在运行代码的计算机上安装Excel?

.net c# excel file-io

1804
推荐指数
37
解决办法
105万
查看次数

如何正确清理Excel互操作对象?

我在C#(ApplicationClass)中使用Excel互操作,并在我的finally子句中放置了以下代码:

while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet) != 0) { }
excelSheet = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Run Code Online (Sandbox Code Playgroud)

虽然这种工作,Excel.exe即使我关闭Excel后,该过程仍然在后台.只有在我的应用程序手动关闭后才会发布.

我做错了什么,或者是否有其他方法可以确保互操作对象得到妥善处理?

c# excel interop com-interop

733
推荐指数
20
解决办法
30万
查看次数

数据访问后在C#中关闭Excel应用程序进程

我正在用C#编写一个应用程序,它打开一个用于读/写操作的Excel模板文件.我想当用户关闭应用程序时,excel应用程序进程已关闭,而不保存excel文件.多次运行应用程序后,请参阅我的任务管理器.

在此输入图像描述

我使用此代码打开excel文件:

public Excel.Application excelApp = new Excel.Application();
public Excel.Workbook excelBook;
excelBook = excelApp.Workbooks.Add(@"C:/pape.xltx");
Run Code Online (Sandbox Code Playgroud)

对于数据访问,我使用以下代码:

Excel.Worksheet excelSheet = (Worksheet)(excelBook.Worksheets[1]);
excelSheet.DisplayRightToLeft = true;
Range rng;
rng = excelSheet.get_Range("C2");
rng.Value2 = txtName.Text;
Run Code Online (Sandbox Code Playgroud)

我在stackoverflow中看到类似的问题,例如这个问题这个,以及测试答案,但它不起作用.

c# excel excel-interop kill-process visual-studio-2012

78
推荐指数
5
解决办法
14万
查看次数

Interop进程后无法关闭Excel.exe

我遇到了Excel Interop的问题.

即使我重新发布实例,Excel.exe也不会关闭.

这是我的代码:

using xl = Microsoft.Office.Interop.Excel;


xl.Application excel = new xl.Application();
excel.Visible = true;
excel.ScreenUpdating = false;
if (wordFile.Contains(".csv") || wordFile.Contains(".xls"))
{
   //typeExcel become a string of the document name
   string typeExcel = wordFile.ToString();
   xl.Workbook workbook = excel.Workbooks.Open(typeExcel,
                                                oMissing,  oMissing,  oMissing,  oMissing,
                                                oMissing,  oMissing,  oMissing,  oMissing,
                                                oMissing,  oMissing,  oMissing,  oMissing,
                                                oMissing,  oMissing);
   object outputFileName = null;
   if (wordFile.Contains(".xls"))
   {
     outputFileName = wordFile.Replace(".xls", ".pdf");
   }
   else if (wordFile.Contains(".csv"))
   {
     outputFileName = wordFile.Replace(".csv", ".pdf");
   }

   workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, outputFileName, 
                                 XlFixedFormatQuality.xlQualityStandard, oMissing,
                                 oMissing, …
Run Code Online (Sandbox Code Playgroud)

c# excel-interop winforms

28
推荐指数
6
解决办法
5万
查看次数