首页 > 解决方案 > openxml clonenode方法导致数据在C#中向右移动列

问题描述

我正在编写一个应用程序,它根据特定标准从数据库中获取数据,然后将其导出到 Excel 文件。它完全正常工作,直到它到达 excel 文件的最后一行。这是编写excel文件的代码:

    if (recordCounter == currentRecordList.Count() && previousRegionName == regionName)
            {
                rowInSheet1.Append(
                  excelController.ConstructCell(regionName, CellValues.String, 2), 
                  excelController.ConstructCell(merchant, CellValues.String, 2),
                  excelController.ConstructCell(currentRecord.TotalFee.ToString("N2"), CellValues.String, 1),
                  excelController.ConstructCell(currentRecord.Currency, CellValues.String, 2),
                  excelController.ConstructCell(currentRecord.DateProcessed.ToString("MMM dd, yyyy"), CellValues.String, 2),
                  excelController.ConstructCell(description, CellValues.String, 2)
                );
                sheetData1.Append(rowInSheet1);


                rowInSheet1.Append(
                  excelController.ConstructCell("TOTALS", CellValues.String, 6),
                  excelController.ConstructCell(" ", CellValues.String, 2), 
                  excelController.ConstructCell(currentRecord.TotalFeeAmount.ToString("N2"), CellValues.String, 6), 
                  excelController.ConstructCell(currentRecord.Currency, CellValues.String, 6), 
                  excelController.ConstructCell(" ", CellValues.String, 2),
                  excelController.ConstructCell(" ", CellValues.String, 2)

                );
           
                sheetData1.Append(rowInSheet1.CloneNode(false));
            }
            else if (recordCounter == currentRecordList.Count())
            {
                rowInSheet1.Append(
                  excelController.ConstructCell(regionName, CellValues.String, 2), 
                  excelController.ConstructCell(merchant, CellValues.String, 2), 
                  excelController.ConstructCell(currentRecord.TotalFee.ToString("N2"), CellValues.String, 1), 
                  excelController.ConstructCell(currentRecord.Currency, CellValues.String, 2), 
                  excelController.ConstructCell(currentRecord.DateProcessed.ToString("MMM dd, yyyy"), CellValues.String, 2),
                  excelController.ConstructCell(description, CellValues.String, 2)
                );

                sheetData1.Append(rowInSheet1);

                rowInSheet1.Append(
                  excelController.ConstructCell("TOTALS", CellValues.String, 6), 
                  excelController.ConstructCell(" ", CellValues.String, 2), 
                  excelController.ConstructCell(currentRecord.TotalFeeAmount.ToString("N2"), CellValues.String, 6), 
                  excelController.ConstructCell(currentRecord.Currency, CellValues.String, 6), 
                  excelController.ConstructCell(" ", CellValues.String, 2), 
                  excelController.ConstructCell(" ", CellValues.String, 2)

                );
                sheetData1.Append(rowInSheet1);
            }
            else
            {
                if ((previousCurrency == null && previousRegionName == null) || (previousCurrency == currentCurrency && previousRegionName == regionName))
                {
                    rowInSheet1.Append(
                      excelController.ConstructCell(regionName, CellValues.String, 2),
                      excelController.ConstructCell(merchant, CellValues.String, 2), 
                      excelController.ConstructCell(currentRecord.TotalFee.ToString("N2"), CellValues.String, 1),
                      excelController.ConstructCell(currentRecord.Currency, CellValues.String, 2), 
                      excelController.ConstructCell(currentRecord.DateProcessed.ToString("MMM dd, yyyy"), CellValues.String, 2), 
                      excelController.ConstructCell(description, CellValues.String, 2)
                    );
                    sheetData1.Append(rowInSheet1);
                    previousCurrency = currentCurrency;
                    previousTotal = currentTotal;
                    previousRegionName = regionName;

                }
                else
                {
                    rowInSheet1.Append(
                      excelController.ConstructCell("TOTALS", CellValues.String, 6), 
                      excelController.ConstructCell(" ", CellValues.String, 2), 
                      excelController.ConstructCell(previousTotal.ToString("N2"), CellValues.String, 6),
                      excelController.ConstructCell(previousCurrency, CellValues.String, 6), 
                      excelController.ConstructCell(" ", CellValues.String, 2),
                      excelController.ConstructCell(" ", CellValues.String, 2)

                    );
                    sheetData1.Append(rowInSheet1);

                    Row emptyRow;
                    emptyRow = new Row();
                    emptyRow.Append(
                      excelController.ConstructCell(" ", CellValues.String, 2), 
                      excelController.ConstructCell(" ", CellValues.String, 2), 
                      excelController.ConstructCell(" ", CellValues.String, 2), 
                      excelController.ConstructCell(" ", CellValues.String, 2),
                      excelController.ConstructCell(" ", CellValues.String, 2), 
                      excelController.ConstructCell(" ", CellValues.String, 2)
                    );
                    sheetData1.Append(emptyRow);

                    Row row;
                    row = new Row();
                    row.Append(
                      excelController.ConstructCell(regionName, CellValues.String, 2), 
                      excelController.ConstructCell(merchant, CellValues.String, 2),
                      excelController.ConstructCell(currentRecord.TotalFee.ToString("N2"), CellValues.String, 1), 
                      excelController.ConstructCell(currentRecord.Currency, CellValues.String, 2), 
                      excelController.ConstructCell(currentRecord.DateProcessed.ToString("MMM dd, yyyy"), CellValues.String, 2), 
                      excelController.ConstructCell(description, CellValues.String, 2)
                    );
                    sheetData1.Append(row);

                    previousCurrency = currentCurrency;
                    previousTotal = currentTotal;
                    previousRegionName = regionName;
                }
                recordCounter++;
            }

        }

在第一个 if 语句中使用了克隆节点方法。我故意将其设置为 false,否则它只会在 excel 文件的末尾附加两次。

这是我的预期结果:

在此处输入图像描述

这就是我得到的:

在此处输入图像描述

我曾尝试使用 Remove 方法,但这似乎也不起作用。

谢谢您的帮助。

标签: c#openxml

解决方案


我通过实例化不同的行来解决这个问题

            rowInSheet1 = new Row();
            firstRow = new Row();

            if (recordCounter == currentRecordList.Count() && previousRegionName == regionName)
            {
                firstRow.Append(
                  excelController.ConstructCell(regionName, CellValues.String, 2), 
                  excelController.ConstructCell(merchant, CellValues.String, 2),
                  excelController.ConstructCell(currentRecord.TotalFee.ToString("N2"), CellValues.String, 1),
                  excelController.ConstructCell(currentRecord.Currency, CellValues.String, 2),
                  excelController.ConstructCell(currentRecord.DateProcessed.ToString("MMM dd, yyyy"), CellValues.String, 2),
                  excelController.ConstructCell(description, CellValues.String, 2)
                );
                sheetData1.Append(firstRow);


                rowInSheet1.Append(
                  excelController.ConstructCell("TOTALS", CellValues.String, 6),
                  excelController.ConstructCell(" ", CellValues.String, 2), 
                  excelController.ConstructCell(currentRecord.TotalFeeAmount.ToString("N2"), CellValues.String, 6), 
                  excelController.ConstructCell(currentRecord.Currency, CellValues.String, 6), 
                  excelController.ConstructCell(" ", CellValues.String, 2),
                  excelController.ConstructCell(" ", CellValues.String, 2)

                );
                sheetData1.Append(rowInSheet1);
                //sheetData1.Append(rowInSheet1.CloneNode(false));
            }

我使用了这个 firstRow 而不是使用 clonenode 方法。像魅力一样工作!


推荐阅读