C# 释放所有COM对象时写入Excel单元格
我遵循建议,尝试释放所有COM对象,包括所有范围,同时将字符串元组列表写入Excel电子表格(使用他的ComObjectManager类) 我有下面的解决方案,但看起来不是堆叠C# 释放所有COM对象时写入Excel单元格,c#,excel,office-interop,C#,Excel,Office Interop,我遵循建议,尝试释放所有COM对象,包括所有范围,同时将字符串元组列表写入Excel电子表格(使用他的ComObjectManager类) 我有下面的解决方案,但看起来不是堆叠 cells[j, column] = strings[i].Item1; (等)在ComObjectManager对象中,因此不释放它们。我有注释掉的解决方案,可以释放对象,但我一定不能正确理解语法来设置单元格,因为它不会编译。我也尝试过使用 var roomNameCell = com.Get<object&
cells[j, column] = strings[i].Item1;
(等)在ComObjectManager对象中,因此不释放它们。我有注释掉的解决方案,可以释放对象,但我一定不能正确理解语法来设置单元格,因为它不会编译。我也尝试过使用
var roomNameCell = com.Get<object>(() => cells[j, column]);
var roomNameCell=com.Get(()=>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;
}
}
内部void InsertStrings(列表字符串、Excel.Range选择、ComObject Manager com)
{
const int singleRowCell=1;
const int columnsToArea=2;
int firstRow=selection.Row;
int column=selection.column;
for(int i=0,j=firstRow;iapp.Cells[j,column]);
var mergeArea=com.Get(()=>selection.mergeArea);
var mergeAreaRows=com.Get(()=>mergeArea.Rows);
var cells=com.Get(()=>app.cells);
if(selection.MergeCells&&mergeAreaRows.Count>singleRowCell)
{
//var roomNameCell=com.Get(()=>cells[j,column]);
//var areaCell=com.Get(()=>cells[j,column+columnsToArea]);
//不编译-无法从字符串显式转换为范围
//roomNameCell=strings[i].Item1;
//areaCell=字符串[i]。项2;
//可以正常工作,但不处理范围COM对象?
单元格[j,列]=strings[i].Item1;
单元格[j,column+columnsToArea]=字符串[i]。项2;
++一,;
}
j+=mergeAreaRows.Count;
}
}
我明白了,我只需要像这样设置Values属性:
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]);
roomNameCell.Value = strings[i].Item1;
areaCell.Value = strings[i].Item2;
++i;
}
j += mergeAreaRows.Count;
}
}
内部void InsertStrings(列表字符串、Excel.Range选择、ComObject Manager com)
{
const int singleRowCell=1;
const int columnsToArea=2;
int firstRow=selection.Row;
int column=selection.column;
for(int i=0,j=firstRow;iapp.Cells[j,column]);
var mergeArea=com.Get(()=>selection.mergeArea);
var mergeAreaRows=com.Get(()=>mergeArea.Rows);
var cells=com.Get(()=>app.cells);
if(selection.MergeCells&&mergeAreaRows.Count>singleRowCell)
{
var roomNameCell=com.Get(()=>cells[j,column]);
var areaCell=com.Get(()=>cells[j,column+columnsToArea]);
roomNameCell.Value=strings[i].Item1;
areaCell.Value=strings[i].Item2;
++一,;
}
j+=mergeAreaRows.Count;
}
}
有一个隐藏的范围,你看不见。只是不要试着写那样的代码,这就像是恢复了C和显式内存管理糟糕的旧时代。这也很有效。如果垃圾收集器出现问题,并且由于某种原因您无法修复它,那么在完成后调用GC.Collect()+GC.WaitForPendingFinalizers()。根据我所能收集到的信息,除非我想导致内存泄漏,否则我别无选择,只能显式释放COM对象?