【问题标题】:EPPlus - Error when copying worksheet with conditional formattingEPPlus - 使用条件格式复制工作表时出错
【发布时间】:2018-02-03 13:02:17
【问题描述】:

我正在使用 EPPlus 4.1,最后一个稳定版本(也尝试了 4.5.0.1-beta,结果相同)。

我正在尝试将几个 excel 文件(使用 EPPlus 创建)合并到具有多个工作表的单个 excel 文件中。一些工作表可能包含条件格式,我设置如下:

'Using columnCells As ExcelRange = workSheetData.Cells(headerLevels + 2, c + 1, workSheetData.Dimension.End.Row + 1, c + 1) 'headerLevels + 2
'    Dim cond1 = columnCells.ConditionalFormatting.AddGreaterThan()
'    cond1.Formula = "0.0"
'    cond1.Style.Fill.BackgroundColor.Color = ColorTranslator.FromHtml("#CBDEB7") 'light green

'    Dim cond2 = columnCells.ConditionalFormatting.AddLessThan()
'    cond2.Formula = "0.0"
'    cond2.Style.Fill.BackgroundColor.Color = ColorTranslator.FromHtml("#F1CDB1") 'light red
'End Using

Dim cfAddress As New ExcelAddress(headerLevels + 2, c + 1, workSheetData.Dimension.End.Row + 1, c + 1)
Dim cfRule1 = workSheetData.ConditionalFormatting.AddGreaterThan(cfAddress)
cfRule1.Formula = "0.0"
cfRule1.Style.Fill.BackgroundColor.Color = ColorTranslator.FromHtml("#CBDEB7") 'light green

Dim cfRule2 = workSheetData.ConditionalFormatting.AddLessThan(cfAddress)
cfRule2.Formula = "0.0"
cfRule2.Style.Fill.BackgroundColor.Color = ColorTranslator.FromHtml("#F1CDB1") 'light red

这两种方法产生相同的结果并且对源文件工作正常。

然后,我通过将工作表复制到新文件中来合并这些文件,如下所示

Dim fileInfoMaster As New System.IO.FileInfo(filePath)
Using _pckMaster As New ExcelPackage(fileInfoMaster)

    For i As Integer = 0 To masterWorkbookParams.ListOfFileNames.Count() - 1
        filePath = folderPath & masterWorkbookParams.ListOfFileNames(i) & ".xlsx"
        If System.IO.File.Exists(filePath) Then
            Dim fileInfo As New System.IO.FileInfo(filePath)
            Using _pck As New ExcelPackage(fileInfo)

                Dim NewWorksheet As ExcelWorksheet = _pckMaster.Workbook.Worksheets.Add(masterWorkbookParams.ListOfSheetNames(i), _pck.Workbook.Worksheets(1))

            End Using
        End If
    Next i

    _pckMaster.Save()
End Using

_pckMaster.Save() 行产生错误。 当我在调试器中检查该行之前源文件的工作表和目标文件的工作表时,我看到了以下图片:

源文件:

目标文件:

我们将副本添加到新文件后,工作表中的 ConditionalFormatting 节点出现异常。如果源文件没有任何条件格式,则将工作表的副本从一个工作簿添加到新工作簿并按预期保存该文件。

有人遇到同样的问题吗?它是一个错误吗?还是我在这里做错了什么?

ConditionalFormatting '(New System.Linq.SystemCore_EnumerableDebugView(Of OfficeOpenXml.ExcelWorksheet)(_pckMaster.Workbook.Worksheets).Items(1)).ConditionalFormatting' 引发了类型为 'System.ArgumentOutOfRangeException' OfficeOpenXml.ConditionalFormatting.ExcelConditionalFormattingCollection { System.ArgumentOutOfRangeException}

堆栈跟踪

  • System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument 参数,ExceptionResource 资源)
  • OfficeOpenXml.ConditionalFormatting.ExcelConditionalFormattingRule..ctor(eExcelConditionalFormattingRuleType type, ExcelAddress address, Int32 priority, ExcelWorksheet worksheet, XmlNode itemElementNode, XmlNamespaceManager namespaceManager)
  • OfficeOpenXml.ConditionalFormatting.ExcelConditionalFormattingGreaterThan..ctor(ExcelAddress address, Int32 priority, ExcelWorksheet worksheet, XmlNode itemElementNode, XmlNamespaceManager namespaceManager)
  • OfficeOpenXml.ConditionalFormatting.ExcelConditionalFormattingRuleFactory.Create(eExcelConditionalFormattingRuleType type, ExcelAddress address, Int32 priority, ExcelWorksheet worksheet, XmlNode itemElementNode)
  • OfficeOpenXml.ConditionalFormatting.ExcelConditionalFormattingCollection..ctor(ExcelWorksheet 工作表)
  • OfficeOpenXml.ExcelWorksheet.get_ConditionalFormatting()

【问题讨论】:

  • 您可能需要检查您没有复制重复的条件格式规则
  • @YahyaHussein 是的,我还尝试为只有一个条件规则的单个单元格设置条件格式。结果是一样的,把这张表复制到一个新的工作簿后,保存那个文件就出错了。
  • @AlexeyPolyanichko 您找到解决方案了吗?我现在遇到了同样的问题,似乎找不到解决方法。
  • @Kevin 不,问题仍然存在(

标签: .net epplus conditional-formatting worksheet


【解决方案1】:

对于任何感兴趣的人,我在最新的 EPPlus 版本中对此进行了更正。请参阅 Github 上的问题 #464。也作为问题 #266 提及。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2019-02-16
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多