【问题标题】:Using Open XML how do you insert a formula into an Excel 2010 worksheet?如何使用 Open XML 将公式插入 Excel 2010 工作表?
【发布时间】:2011-06-10 16:09:50
【问题描述】:

我正在使用 Visual Studio 2010 (VB.Net) 和 Open XML SDK 2.0。如何将公式插入 Excel 2010 工作表?当我这样做时,我还希望将单元格的 CellValue 属性设置为 DBNull 或 EmptyString 以强制 Excel 在用户打开工作簿时重新计算单元格。

【问题讨论】:

    标签: vb.net excel openxml openxml-sdk


    【解决方案1】:

    只需将 CellValue 保留为 null,然后像这样实例化一个新的 CellFormula:

    Cell cell = new Cell()
    {
      CellReference = "A3",
      DataType = new EnumValue<CellValues>(CellValues.Number),
      CellFormula = "SUM(A1:A2)"
    };
    

    在 Excel 中打开文档时将计算单元格值

    【讨论】:

    • 不打开excel怎么办。
    【解决方案2】:

    这来自附加到 Open XML SDK 2.0 for Microsoft Office 帮助文件的帮助文档,并进行了一些修改以添加公式。

    Main() 找到一个带有 一张 的空白 Excel 文档,并将 SUM() 公式添加到单元格 A3

    Sub Main()
        Dim outputFilePath = "C:\Book1.xlsx"
        Dim doc As SpreadsheetDocument = SpreadsheetDocument.Open(outputFilePath, True)
        Dim workbookPart As WorkbookPart = doc.WorkbookPart
        Dim worksheetPart As WorksheetPart = workbookPart.WorksheetParts.First()
    
        InsertCellInWorksheet("A", 3, worksheetPart)
    End Sub
    
    ' Given a column name, a row index, and a WorksheetPart, inserts a cell into the worksheet. 
    ' If the cell already exists, return it. 
    Private Function InsertCellInWorksheet(ByVal columnName As String, ByVal rowIndex As     UInteger, ByVal worksheetPart As WorksheetPart) As Cell
        Dim worksheet As Worksheet = worksheetPart.Worksheet
        Dim sheetData As SheetData = worksheet.GetFirstChild(Of SheetData)()
        Dim cellReference As String = (columnName + rowIndex.ToString())
    
        ' If the worksheet does not contain a row with the specified row index, insert one.
        Dim row As Row
        If (sheetData.Elements(Of Row).Where(Function(r) r.RowIndex.Value = rowIndex).Count() <> 0) Then
            row = sheetData.Elements(Of Row).Where(Function(r) r.RowIndex.Value = rowIndex).First()
        Else
            row = New Row()
            row.RowIndex = rowIndex
            sheetData.Append(row)
        End If
    
        ' If there is not a cell with the specified column name, insert one.  
        If (row.Elements(Of Cell).Where(Function(c) c.CellReference.Value = columnName + rowIndex.ToString()).Count() > 0) Then
            Return row.Elements(Of Cell).Where(Function(c) c.CellReference.Value = cellReference).First()
        Else
            ' Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
            Dim refCell As Cell = Nothing
            For Each cell As Cell In row.Elements(Of Cell)()
                If (String.Compare(cell.CellReference.Value, cellReference, True) > 0) Then
                    refCell = cell
                    Exit For
                End If
            Next
    
            Dim newCell As Cell = New Cell
            newCell.CellReference = cellReference
            newCell.CellFormula = New CellFormula("SUM(A1:A2)")
    
            row.InsertBefore(newCell, refCell)
            worksheet.Save()
    
            Return newCell
        End If
        End Function
    

    请注意,此方法假定您在公式中引用的每个单元格都有正确标记的引用。

    【讨论】:

    • 我收到此代码的构建错误。 CellValues.[Formula] 和 newCell.Formula 不包含公式方法/道具。你在用 OpenXML2.0 吗?
    • 我使用的是版本:2.0.5022.0
    • @eschneider 更新了数据类型和公式,请立即尝试。
    • @eschneider 基本上归结为:newCell.CellFormula = New CellFormula("SUM(A1:A2)")
    • @eschneider 我添加了来自Main() 的调用代码。确保您在 C:\Book1.xlsx 有一个有效的 Excel 文档,其中包含 一个 表。
    【解决方案3】:

    您可以在模板excel中设置公式并编写此代码以重新计算它们:

    spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = True spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = True
    

    【讨论】:

    • 我在 excel 单元格中编写所有公式,然后使用此代码并运行。
    猜你喜欢
    • 2021-03-10
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多