2021年3月31日星期三

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:

enter image description here

This is what I'm getting:

enter image description here

I have tried using the Remove method but that does not seem to work either.

Thanks for the help.

https://stackoverflow.com/questions/66897181/openxml-clonenode-method-cause-data-to-shift-column-to-the-right-in-c-sharp April 01, 2021 at 09:05AM

没有评论:

发表评论