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

时间:2018-02-16 00:45:47

标签: c# excel winforms npoi

我尝试读取一个文件并将其附加到包含单元格样式的另一个文件中。所有问题似乎都源于内部的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++;
            }

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

1 个答案:

答案 0 :(得分:0)

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

var aoiNameParameterValue = AOIName.Replace("[", "[[]").Replace("%", "[%]").Replace("_", "[_]").Replace("^", "[^]");

主要新功能是

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

                }
            }

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