使用包含 CellStyling 的 NPOI 将一个电子表格附加到另一个电子表格

Appending one spreadsheet to another with NPOI with CellStyling included

我正在尝试读取一个文件并将其附加到包含单元格样式的另一个文件中。所有问题似乎都源于内部 foreach 循环,我已经在给出错误的行上加了星号。 此代码的问题在于:

'System.ArgumentException' 类型的未处理异常发生在 NPOI.OOXML.dll 附加信息:此样式不属于提供的 Workbook Stlyes Source。您是否正在尝试将一个工作簿的样式分配给另一个工作簿的单元格?

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

这应该做的是通读页脚电子表格中的所有单元格并将它们写到报价单上。 foreach 循环的前两行工作正常,所以我可以在页脚中写入文本,但我找不到在复制时保留样式的方法。

我尝试将 foreach 循环设置为

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

但这只会产生空单元格。 任何帮助将不胜感激, 谢谢

如果以后有人需要这个 这可以正常工作:(footerHeight 和 footerWidth 是要附加的电子表格的尺寸)

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

                }
            }

主要的新功能是

currentStyle.CloneStyleFrom(footerCell.CellStyle);

显然 CellStyles 非常复杂,因此需要一个特殊的命令来跨工作簿进行复制。