0
好,所以我有这个功能,在插入行然后将数据放入这些行内工作正常。Excel互操作 - 插入行和重复
public void inputRowData(string[] data, int rds)
{
int bestRow = getRowByRDS(rds);
string val = getValueOfCell(bestRow, 6);
if (val == null || val.Equals(""))
{
shiftRows(bestRow, data.Length-1);
string[] formatedData = formatOutput(bestRow, data);
// transform formated data into string[,]
string[][] splitedData = formatedData.Select(s => s.Split('\t')).ToArray();
var colCount = splitedData.Max(r => r.Length);
var excelData = new string[splitedData.Length, colCount];
for (int i = 0; i < splitedData.Length; i++)
{
for (int j = 0; j < splitedData[i].Length; j++)
{
excelData[i, j] = splitedData[i][j];
}
}
oSheet.get_Range("A" + bestRow.ToString()).Resize[splitedData.Length, colCount].Value = excelData;
}
else
{
Console.WriteLine("Line has some information already, skipping 1 more");
shiftRows(bestRow, data.Length + 1);
}
}
现在我如果你看看我发现这是由最后一行中的Excel,将int值在特定列确定的“bestRow”的代码是在这里展示:
private int getRowByRDS(int id)
{
int bestfit = -1;
Boolean foundOne = false;
Microsoft.Office.Interop.Excel.Range usedRange = oSheet.UsedRange;
for (int i = 2; i < usedRange.Rows.Count; i++)
{
string val = getValueOfCell(i, 3);
if (val == null)
continue;
int rds = int.Parse(val);
Console.WriteLine(val + " " +i);
if (rds == id)
{
bestfit = i;
foundOne = true;
}
else
if (foundOne)
return bestfit;
}
return bestfit;
}
会发生什么,当它在inputRowData方法中的一组数据上完成时,它将移动到具有不同rds值的另一组数据。它会尝试调用getRowByRDS,但会引发错误。
在调试时,它看起来好像没有更新工作表......所以假设第一组10个字符串被插入到第900行,并且如果下一组假设在其之后立即开始,那么bestRow将返回901而不是911.
的错误是
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in TTE Tool.exe
两个问题:** 1)**您可以验证所有单元格正在更新吗?有可能你的阵列没有排队,也不是所有的单元格都在变化。 ** 2)**如果这是好的,我的下一个猜测是,这与使用'UsedRange'有关。这是可能的(不知道有多大可能),这个属性在更改单元格后没有正确更新。要测试这个,使用'.End()'方法之一来获取你正在使用的数据块。或者如果数据是连续的(空白),使用'CurrentRegion'。 –