简体   繁体   中英

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. It works completely correctly till it gets to the last line of the excel file. Here is the code that writes the excel file:

    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. I have it purposely set to false or else it just appends twice at the end of the excel file.

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.

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. Worked like a charm!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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