gpt4 book ai didi

c# - 为 C# 互操作对象创建可重用的 Excel 样式

转载 作者:太空宇宙 更新时间:2023-11-03 10:23:20 24 4
gpt4 key购买 nike

我正在使用 C# 生成几个电子表格,并且正在尝试创建一个可重复使用的 Style可以应用于每个电子表格中的特定范围。我遇到的问题是当我的方法完成执行时,Excel 进程没有正确退出。事实上,我已经能够将问题范围缩小到我正在创建 Excel Style 的代码中。

Workbooks books = excelApplication.Workbooks;
_Workbook wBook = books.Add("");
_Worksheet wSheet = (_Worksheet)wBook.ActiveSheet;

Styles styles = wBook.Styles;
Style columnHeader = styles.Add("ColumnHeader");
columnHeader.Font.Size = 12; // if I comment this out, excel quits correctly

Marshal.ReleaseComObject(wSheet);
Marshal.ReleaseComObject(wBook);
Marshal.ReleaseComObject(books);

样式在我应用时按预期工作,但在我退出 excelApplication 时 Excel 进程没有退出。如果我注释掉 columnHeader.Font.Size = 12; 行,Excel 进程会正确退出。我错过了什么吗?

更新
我修改了 Govert 的示例 WinForms 应用程序以反射(reflect)我的类的结构,它正在创建多个电子表格。他的应用程序正常退出了Excel进程,而我修改后的版本没有:

using System;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;

namespace ExcelCOMReferenceTesting
{
public partial class Form1 : Form
{
private Excel.Application excelApplication;
private Excel.Style columnHeader;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
DoMyExcelStuff();
GarbageCleanup();
}

private void DoMyExcelStuff()
{
StartExcel();
var wBook = GenerateWorksheet();

excelApplication.Range["A1"].Value = "Name";
excelApplication.Range["A1"].Style = columnHeader;
wBook.SaveAs(@"c:\Test\tst" + DateTime.Now.ToString("mmss") + ".xlsx");

// No need for Marshal.ReleaseComObject(...)
// No need for ... = null
StopExcel();
}

private void StartExcel()
{
excelApplication = new Excel.Application();
excelApplication.Visible = false;
}

private void StopExcel()
{
excelApplication.UserControl = false;
excelApplication.Quit();
}

private Excel._Workbook GenerateWorksheet()
{
Excel.Workbooks books = excelApplication.Workbooks;
Excel.Workbook wBook = books.Add("");

Excel.Styles styles = wBook.Styles;
columnHeader = styles.Add("ColumnHeader");
columnHeader.Font.Size = 12;
columnHeader.Font.Bold = true;

return wBook;
}

private void GarbageCleanup()
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}

最佳答案

可能是对 Font/columnHeader 或样式的挂起引用,访问 Excel 互操作中的属性会导致 Excel 进程挂起;试试这个

Workbooks books = excelApplication.Workbooks;
_Workbook wBook = books.Add("");
_Worksheet wSheet = (_Worksheet)wBook.ActiveSheet;

Styles styles = wBook.Styles;
Style columnHeader = styles.Add("ColumnHeader");
Font font = columnHeader.Font;
font.Size = 12;

Marshal.ReleaseComObject(font);
Marshal.ReleaseComObject(columnHeader);
Marshal.ReleaseComObject(styles);
Marshal.ReleaseComObject(wSheet);
Marshal.ReleaseComObject(wBook);
Marshal.ReleaseComObject(books);

font = null;
columnHeader = null;
styles = null;
wSheet = null;
wBook = null;
books = null;

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

关于c# - 为 C# 互操作对象创建可重用的 Excel 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32572836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com