SpreadsheetGear - 将工作表从一个工作簿复制到另一个工作簿时出现问题

SpreadsheetGear - Issue with copying sheets from one Workbook to another

我想合并多个工作簿中的所有 sheet 并将它们保存在一个 excel 文件中。我正在尝试将所有 sheet 复制到一个工作簿中,然后将工作簿保存到一个文件中。问题是 - 只有最后一个 sheet 保存在文件中。下面是一些示例代码。

public static void ExportSheets(string documentFilePath, List<IWorkbook> workbooks)
        {
            if (workbooks.Count > 1)
            {
                var destinationSourceBook = workbooks[0];
                for (int i = 1; i < workbooks.Count; i++)
                {
                    var sourceWorkBook = workbooks[i];
                    for (int j = 0; j < sourceWorkBook.Sheets.Count; j++)
                    {
                        sourceWorkBook.Sheets[j].CopyAfter(destinationSourceBook.Sheets[destinationSourceBook.Sheets.Count - 1]);
                    }
                }
                destinationSourceBook.SaveAs(documentFilePath, FileFormat.OpenXMLWorkbook);
            }
            else if (workbooks.Count == 1)
            {
                workbooks[0].SaveAs(documentFilePath, FileFormat.OpenXMLWorkbook);
            }
        }

调试时,我可以看到 sheet 被添加到 destinationSourceBook.Sheets 集合中,但只有最后一个 sheet 出现在保存的 excel 中文件。正确的做法是什么?

谢谢

实际上添加了 sheet,但问题是 Sheet 选项卡不可见(在 excel - 文件 -> 选项 -> 高级 -> 显示选项-> 显示 sheet 选项卡复选框)。解决方案是将 Workbook.WindowInfo.DisplayWorkbookTabs 设置为 true。

        public static void ExportSheets(string documentFilePath, List<IWorkbook> workbooks)
        {
            if (workbooks.Count > 0)
            {
                var destinationWorkbook = workbooks[0];

                for (var i = 1; i < workbooks.Count; i++)
                {
                    var sourceWorkBook = workbooks[i];
                    for (var j = 0; j < sourceWorkBook.Sheets.Count; j++)
                    {
                        sourceWorkBook.Sheets[j].CopyAfter(destinationWorkbook.Sheets[destinationWorkbook.Sheets.Count - 1]);
                    }
                }
                destinationWorkbook.WindowInfo.DisplayWorkbookTabs = true;
                destinationWorkbook.SaveAs(documentFilePath, FileFormat.OpenXMLWorkbook);
            }
        }