我想在C#中使用EPPlus合并多个Excel文件。
我按照以下步骤进行操作:
我按照以下步骤进行操作:
using (MemoryStream protocolStream = new MemoryStream())
{
ExcelPackage pck = new ExcelPackage();
HashSet<string> wsNames = new HashSet<string>();
foreach (var file in files)
{
ExcelPackage copyPck = new ExcelPackage(new FileInfo(file));
foreach (var ws in copyPck.Workbook.Worksheets)
{
string name = ws.Name;
int i = 1;
while (!wsNames.Add(ws.Name))
name = ws.Name + i++;
ws.Name = name;
var copiedws = pck.Workbook.Worksheets.Add(name);
copiedws.WorksheetXml.LoadXml(ws.WorksheetXml.DocumentElement.OuterXml);
}
}
pck.SaveAs(protocolStream);
protocolStream.Position = 0;
using (FileStream fs = new FileStream(resultFile, FileMode.Create))
protocolStream.CopyTo(fs);
}
但是在pck.SaveAs(protocolStream)
中,我遇到了以下错误:
System.ArgumentOutOfRangeException
在 System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) 在 System.Collections.Generic.List
1.get_Item(Int32 index) 在 OfficeOpenXml.ExcelStyleCollection
1.get_Item(Int32 PositionID)
我也尝试使用Worksheet.Copy方法,但是这样会导致样式丢失。
pck.SaveAs(protocolStream)
。 - AlteGurke