如何使用Open XML如何将公式插入Excel 2010工作表?

时间:2021-01-21 07:16:07

I'm using Visual Studio 2010 (VB.Net) and Open XML SDK 2.0. How do you insert a formula into an Excel 2010 worksheet? When I do this I also wish to set the CellValue property of the cell to a DBNull or EmptyString to force Excel to recalculate the cell when the user opens the workbok.

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

3 个解决方案

#1


6  

Just leave the CellValue as null, and instantiate a new CellFormula like so:

只需将CellValue保留为null,并实例化一个新的CellFormula,如下所示:

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

The cells value will be computed when the document is opened in Excel

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

#2


0  

This comes from the Help Document attached to the Open XML SDK 2.0 for Microsoft Office help file, with some modification for adding a formula.

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

Main() finds a blank Excel document with one sheet and adds the SUM() formula to cell A3.

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

Note that this method assumes each cell you reference in the formula has a correctly labeled reference.

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

#3


0  

you can set formula in template excel and write this code to recalculate them:

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

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

#1


6  

Just leave the CellValue as null, and instantiate a new CellFormula like so:

只需将CellValue保留为null,并实例化一个新的CellFormula,如下所示:

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

The cells value will be computed when the document is opened in Excel

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

#2


0  

This comes from the Help Document attached to the Open XML SDK 2.0 for Microsoft Office help file, with some modification for adding a formula.

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

Main() finds a blank Excel document with one sheet and adds the SUM() formula to cell A3.

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

Note that this method assumes each cell you reference in the formula has a correctly labeled reference.

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

#3


0  

you can set formula in template excel and write this code to recalculate them:

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

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