C# 如何在使用epPlus的excel中检查和合并两行,看下一个值是否相同
我正在EPPlus库的帮助下进行动态Excel创建,我有一个Excel,其数据如下所示:C# 如何在使用epPlus的excel中检查和合并两行,看下一个值是否相同,c#,excel,epplus,epplus-4,C#,Excel,Epplus,Epplus 4,我正在EPPlus库的帮助下进行动态Excel创建,我有一个Excel,其数据如下所示: Name EmpCode Department attendance Prashant 111 CSE 70% for Sep Prashant 111 CSE 90% for Oct XYZ 112 HR 50% for Sep XYZ 112 HR
Name EmpCode Department attendance
Prashant 111 CSE 70% for Sep
Prashant 111 CSE 90% for Oct
XYZ 112 HR 50% for Sep
XYZ 112 HR 90% for Oct
我想要的是:
如果当前EmpCode等于下一行的值,则将这两列合并,这样预期的输出将是
我敢肯定每个EMP代码只会重复两次
我尝试过的代码如下:
for (var rowNum = 1; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var row = ws.Cells[string.Format("{0}:{0}", rowNum)];
}
for(var rowNum=1;rowNum此代码仅在EMP代码重复两次时有效,但您说您非常确定它只会重复两次,所以应该可以,只是不太具有可伸缩性
在电子表格中获得数据后,必须循环数据集中的所有行。循环开始时设置当前行的范围,循环结束时设置上一行的范围。
如果设置了上一个范围,则计算每行的列,以确定是否应将单元格合并在一起
using (var p = new OfficeOpenXml.ExcelPackage(new FileInfo(@"c:\FooFolder\Foo.xlsx")))
{
ExcelWorkbook wb = p.Workbook;
ExcelWorksheet ws = wb.Worksheets[1];
//create variable for previous range that will persist through each loop
ExcelRange previousRange = null;
//set position of first column to merge
int mergecellBegin = 1;
//set position of last column to merge
int mergeCellEnd = 3;
//create variable to check the cells of your rows
bool areCellsEqual;
//iterate through each row in the dataset
for (var rowNum = 2; rowNum <= ws.Dimension.End.Row; rowNum++)
{
ExcelRange currentRange = ws.Cells[rowNum, 1, rowNum, mergeCellEnd];
//will skip if we haven't set previous range yet
if (previousRange != null)
{
//reset your check variable
areCellsEqual = true;
//check if all cells in the ranges are qual to eachother
for (int i = 1; i <= mergeCellEnd; i++)
{
//if the cells from the ranges are not equal then set check variable to false and break the loop
if (!currentRange[rowNum, i].Value.Equals(previousRange[rowNum - 1, i].Value))
{
areCellsEqual = false;
break;
}
}
//if all cells from the two ranges match, merge them together.
if (areCellsEqual)
{
//merge each cell in the ranges
for (int i = 1; i <= mergeCellEnd; i++)
{
ExcelRange mergeRange = ws.Cells[rowNum - 1, i, rowNum, i];
mergeRange.Merge = true;
}
}
}
//sets the previous range to the current range to be used in next iteration
previousRange = currentRange;
}
p.Save();
}
使用(var p=newofficeopenxml.ExcelPackage(newfileinfo(@“c:\fooofolder\Foo.xlsx”))
{
Excel工作簿wb=p.工作簿;
Excel工作表ws=wb.工作表[1];
//为上一个范围创建变量,该范围将在每个循环中保持不变
ExcelRange previousRange=null;
//设置要合并的第一列的位置
int mergecellBegin=1;
//设置要合并的最后一列的位置
int-mergeCellEnd=3;
//创建变量以检查行的单元格
布尔·阿雷塞尔;
//遍历数据集中的每一行
对于(var rowNum=2;rowNum此代码仅在EMP代码重复两次时有效,但您说您非常确定它只会重复两次,所以应该可以,只是不太可伸缩
在电子表格中获得数据后,必须循环数据集中的所有行。循环开始时设置当前行的范围,循环结束时设置上一行的范围。
如果设置了上一个范围,则计算每行的列,以确定是否应将单元格合并在一起
using (var p = new OfficeOpenXml.ExcelPackage(new FileInfo(@"c:\FooFolder\Foo.xlsx")))
{
ExcelWorkbook wb = p.Workbook;
ExcelWorksheet ws = wb.Worksheets[1];
//create variable for previous range that will persist through each loop
ExcelRange previousRange = null;
//set position of first column to merge
int mergecellBegin = 1;
//set position of last column to merge
int mergeCellEnd = 3;
//create variable to check the cells of your rows
bool areCellsEqual;
//iterate through each row in the dataset
for (var rowNum = 2; rowNum <= ws.Dimension.End.Row; rowNum++)
{
ExcelRange currentRange = ws.Cells[rowNum, 1, rowNum, mergeCellEnd];
//will skip if we haven't set previous range yet
if (previousRange != null)
{
//reset your check variable
areCellsEqual = true;
//check if all cells in the ranges are qual to eachother
for (int i = 1; i <= mergeCellEnd; i++)
{
//if the cells from the ranges are not equal then set check variable to false and break the loop
if (!currentRange[rowNum, i].Value.Equals(previousRange[rowNum - 1, i].Value))
{
areCellsEqual = false;
break;
}
}
//if all cells from the two ranges match, merge them together.
if (areCellsEqual)
{
//merge each cell in the ranges
for (int i = 1; i <= mergeCellEnd; i++)
{
ExcelRange mergeRange = ws.Cells[rowNum - 1, i, rowNum, i];
mergeRange.Merge = true;
}
}
}
//sets the previous range to the current range to be used in next iteration
previousRange = currentRange;
}
p.Save();
}
使用(var p=newofficeopenxml.ExcelPackage(newfileinfo(@“c:\fooofolder\Foo.xlsx”))
{
Excel工作簿wb=p.工作簿;
Excel工作表ws=wb.工作表[1];
//为上一个范围创建变量,该范围将在每个循环中保持不变
ExcelRange previousRange=null;
//设置要合并的第一列的位置
int mergecellBegin=1;
//设置要合并的最后一列的位置
int-mergeCellEnd=3;
//创建变量以检查行的单元格
布尔·阿雷塞尔;
//遍历数据集中的每一行
对于(var rowNum=2;rowNum)来说,需求发生轻微变化,它将如何在两列以上工作?需求发生轻微变化,它将如何在两列以上工作?