0

互操作和编辑现有 excel 文件有问题。Excel 应用程序不想关闭。如果我设置代码,当我将单元格编辑为注释时,其余代码将按我的需要工作 - Excel 打开并在打印对话框关闭后。

Excel.Application excelApp = null;
Excel.Workbook excelWorkbook = null;
Excel.Worksheet excelWorksheet = null;

string workbookPath = "";
if (RBType1.Checked == true) { workbookPath = Path.GetFullPath("Excel/Mesto.xls"); }
else if (RBType2.Checked == true) { workbookPath = Path.GetFullPath("Excel/Ulehly.xls"); }
else if (RBType3.Checked == true) { workbookPath = Path.GetFullPath("Excel/Spolecenstvi.xls"); }
else if (RBType4.Checked == true) { workbookPath = Path.GetFullPath("Excel/Vlastnici.xls"); }

excelApp = new Excel.Application();
excelApp.Visible = true;

excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
excelWorksheet = (Excel.Worksheet)excelWorkbook.Worksheets.get_Item(1);


excelWorksheet.Cells[4, "J"] = RequisitionNumberBox.Text;
excelWorksheet.Cells[9, "C"] = ApplicantBox.Text;
excelWorksheet.Cells[9, "G"] = StreetBox.Text;
excelWorksheet.Cells[9, "J"] = CPBoxC.Text;
excelWorksheet.Cells[10, "J"] = CBBox.Text;
excelWorksheet.Cells[11, "D"] = CTBox.Text;
excelWorksheet.Cells[11, "G"] = ContactPersonBox.Text;
excelWorksheet.Cells[14, "B"] = RepairBox.Text;
excelWorksheet.Cells[20, "B"] = ItemBox.Text;
if (PMOption1.Checked == true) { excelWorksheet.Cells[23, "D"] = "X"; }
if (PMOption2.Checked == true) { excelWorksheet.Cells[24, "D"] = "X"; }
excelWorksheet.Cells[23, "F"] = IssueDate.Text;

excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrint].Show();

excelWorkbook.Close(false, Type.Missing, Type.Missing);
excelApp.Quit();

releaseObject(excelWorksheet);
releaseObject(excelWorkbook);
releaseObject(excelApp);

releaseObject 代码(在互联网上找到)

try
{
     System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
     obj = null;
}
catch (Exception ex)
{
     obj = null;
     MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
     GC.Collect();
}

我应该如何编辑这段代码,当程序编辑单元格时它会关闭 Excel?

4

0 回答 0