【发布时间】:2021-12-03 12:38:27
【问题描述】:
我有两个 excel 文件,我想提取它们之间的差异以放入另一个 excel 文件。
为了找到差异,我使用每个文件中的特定参考列进行比较。
使用 NPOI 库,我将每个 excel 中的数据存储在两个二维数组中,通过将引用列放在一维数组中并像下面这样比较它们,我能够找到两者之间的差异
//copying the values in the references columns to unidimensional arrays
for (int j = 0; j<firstArray.length; j++)
firstArray[j] = firstArray2d[j, 8];
for (int j = 0; j<secondArray.length; j++)
secondArray[j] = secondArray2d[j, 8];
//below I extract the difference between the unidimensional arrays
IEnumerable<string> firstArrayOnly = firstArray.Except(secondArray);
为了将差异复制到一个新文件,我尝试先将其放入第三个二维数组,然后写入 excel,但我很难将差异复制到第三个二维数组
我尝试了以下
string [] diff = new string[firstArrayOnly.Count()];
int cont = 0;
foreach (var n in firstArrayOnly)
{
diff[cont]=n;
cont++;
}
//until here works fine, the problem comes below
string[,] data = new string [firstArrayOnly.Count(),9];
for (int j = 0; j < firstArray2d.GetLength(0); j++)
{
string aux = diff[j];
if (firstArray2d[i,8] == aux)
for (int x = 0; x < firstArray2dGetLength(1); x++)
data[i,x] = firstArray2d[i,j];
}
O 不断让索引越界
【问题讨论】:
标签: c# arrays excel multidimensional-array npoi