简体   繁体   English

openxml clonenode方法导致数据在C#中向右移动列

[英]openxml clonenode method cause data to shift column to the right in C#

I am writing an application that grabs data from a database based on certain criteria and then exports it to an excel file.我正在编写一个应用程序,它根据特定标准从数据库中获取数据,然后将其导出到 excel 文件。 It works completely correctly till it gets to the last line of the excel file.它完全正常工作,直到它到达 excel 文件的最后一行。 Here is the code that writes the excel file:这是编写 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++;
            }

        }

The clone node method is being used within the first if statement.在第一个 if 语句中使用了克隆节点方法。 I have it purposely set to false or else it just appends twice at the end of the excel file.我故意将其设置为 false,否则它只会在 excel 文件的末尾附加两次。

This is my expected result:这是我的预期结果:

在此处输入图像描述

This is what I'm getting:这就是我得到的:

在此处输入图像描述

I have tried using the Remove method but that does not seem to work either.我曾尝试使用 Remove 方法,但这似乎也不起作用。

Thanks for the help.谢谢您的帮助。

I fixed this issue by just instantiating a different row我通过实例化不同的行来解决这个问题

            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));
            }

I used this firstRow instead of using the clonenode method.我使用了这个 firstRow 而不是使用 clonenode 方法。 Worked like a charm!像魅力一样工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM