我正在遵循这篇文章的建议并尝试释放我的所有 COM 对象,包括所有范围,同时将字符串元组列表写入 Excel 电子表格(使用他的 ComObjectManager 类)。
我有下面的解决方案,但看起来没有堆叠由
cells[j, column] = strings[i].Item1;
(等)在 ComObjectManager 对象中,因此不释放它们。我有注释掉的解决方案,它会释放对象,但我不能正确理解语法来设置单元格,因为它不会编译。我也尝试过使用
var roomNameCell = com.Get<object>(() => cells[j, column]);
对于 roomNameCell 和 areaCell,但这不会导致单元格中没有写入任何内容。
我已经研究了 MSDN 指南,但无法弄清楚如何做到这一点,请帮忙。
internal void InsertStrings(List<Tuple<string, string>> strings, Excel.Range selection, ComObjectManager com)
{
const int singleRowCell = 1;
const int columnsToArea = 2;
int firstRow = selection.Row;
int column = selection.Column;
for (int i = 0, j = firstRow; i < strings.Count; )
{
selection = com.Get<Excel.Range>(() => app.Cells[j, column]);
var mergeArea = com.Get<Excel.Range>(() => selection.MergeArea);
var mergeAreaRows = com.Get<Excel.Range>(() => mergeArea.Rows);
var cells = com.Get<Excel.Range>(() => app.Cells);
if (selection.MergeCells && mergeAreaRows.Count > singleRowCell)
{
//var roomNameCell = com.Get<Excel.Range>(() => cells[j, column]);
//var areaCell = com.Get<Excel.Range>(() => cells[j, column + columnsToArea]);
//doesn't compile - cannont explicity convert from string to Range
//roomNameCell = strings[i].Item1;
//areaCell = strings[i].Item2;
//works but the Range COM objects aren't being disposed?
cells[j, column] = strings[i].Item1;
cells[j, column + columnsToArea] = strings[i].Item2;
++i;
}
j += mergeAreaRows.Count;
}
}