Search code examples
c#excelwinformsnpoi

Appending one spreadsheet to another with NPOI with CellStyling included


I'm trying to read one file and append it onto another with the cell styling included. All problems seem to be stemming from the inner foreach loop and I have asterisked the line giving the error. The problem with this code is that:

An unhandled exception of type 'System.ArgumentException' occurred in NPOI.OOXML.dll Additional information: This Style does not belong to the supplied Workbook Stlyes Source. Are you trying to assign a style from one workbook to the cell of a differnt workbook?

 private void AddCellFooter(ref ISheet quoteSheet,string brandName, int lastRowIndex, ref IWorkbook quoteWorkbook )
    {
        FileStream sw = null;
        if (File.Exists("Quote Templates\\" + brandName + "MasterFooter.xlsx"))
        {
            IRow currentRow;
            ICell currentCell;
            ICellStyle currentStyle;
            int cellIndex;
            sw = File.OpenRead("Quote Templates\\" + brandName + "MasterFooter.xlsx");
            IWorkbook footerWorkBook = WorkbookFactory.Create(sw);
            ISheet footerSheet = footerWorkBook.GetSheet("Sheet1");
            foreach (IRow footerRow in footerSheet)
            {
                cellIndex = 0;
                currentRow = quoteSheet.CreateRow(lastRowIndex);
                foreach (ICell footerCell in footerRow)
                {
                    currentCell = currentRow.CreateCell(cellIndex,footerCell.CellType);
                    currentCell.SetCellValue(footerCell.StringCellValue);
                    currentStyle = quoteWorkbook.CreateCellStyle();
                    currentStyle = footerCell.CellStyle;
                    ******currentCell.CellStyle = currentStyle;******
                    cellIndex++;
                }
                lastRowIndex++;
            }
            sw.Close();
        }
    }

What this is supposed to do is read through all the cells in the footer spreadsheet and write them onto the quotesheet. The first two lines of the foreach loop work fine so I can write the text in the footer but I can't find a way to preserve stylings when copying it across.

I have tried setting the foreach loop to just be

foreach (ICell footerCell in footerRow)
            {
                currentCell = currentRow.CreateCell(cellIndex,footerCell.CellType);
                currentCell = footerCell;
                cellIndex++;
            }

But that just produced empty cells. Any help would be greatly appreciated, Thanks


Solution

  • If anyone needs this in the future this works correctly: (footerHeight and footerWidth are the dimensions of the spreadsheet to be appended)

    for (int i = 0; i < footerHeight; i++)
                {
                    currentRow = quoteSheet.CreateRow(i + lastRowIndex);
                    for (int j = 0; j < footerWidth; j++)
                    {
                        CellType ct = footerSheet.GetRow(i).GetCell(j)?.CellType ?? CellType.Blank;
                        if (ct != CellType.Unknown)
                        {
                            ICell footerCell = footerSheet.GetRow(i).GetCell(j);
                            switch (ct)
                            {
                                case CellType.Unknown:
                                    break;
                                case CellType.Numeric:
                                    currentCell = currentRow.CreateCell(j, CellType.Numeric);
                                    currentCell.SetCellValue(footerCell.NumericCellValue);
                                    break;
                                case CellType.String:
                                    currentCell = currentRow.CreateCell(j, CellType.String);
                                    currentCell.SetCellValue(footerCell.StringCellValue);
                                    break;
                                case CellType.Formula:
                                    break;
                                case CellType.Blank:
                                    currentCell = currentRow.CreateCell(j, CellType.String);
                                    currentCell.SetCellValue("");
                                    break;
                                case CellType.Boolean:
                                    break;
                                case CellType.Error:
                                    break;
                                default:
                                    break;
                            }
                            currentStyle = quoteWorkbook.CreateCellStyle();
                            if (footerCell != null)
                            {
                                currentStyle.CloneStyleFrom(footerCell.CellStyle);
                            }
                            currentCell.CellStyle = currentStyle;
                        }
    
                    }
                }
    

    With the main new feature being

    currentStyle.CloneStyleFrom(footerCell.CellStyle);
    

    As apparently CellStyles are quite complex so a special command is required to copy across workbooks.