2011-06-10 28 views

Respuesta

6

acaba de salir de la CellValue como nulo, e instanciar un nuevo CellFormula así:

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

El valor células se computará cuando se abra el documento en Excel

+1

¿Cómo podemos hacerlo sin necesidad de abrir Excel. – AjayR

0

Esto viene del documento de ayuda adjunta al archivo de ayuda de Open XML SDK 2.0 para Microsoft Office, con algunas modificaciones para agregar una fórmula.

Main() encuentra un documento de Excel en blanco con una hoja y agrega la fórmula SUM() a los teléfonos 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 

Tenga en cuenta que este método supone que cada celda que hace referencia en la fórmula tiene una referencia correctamente etiquetada.

+0

Obtengo errores de compilación para este código. CellValues. [Formula] y newCell.Formula no contienen un método de fórmula/Props. ¿Estás usando OpenXML2.0? – eschneider

+0

Estoy usando la versión: 2.0.5022.0 – eschneider

+0

@eschneider Tipo de datos y fórmula actualizados, por favor intente ahora. –

0

puede establecer fórmula en la plantilla de Excel y escribir el código para volver a calcular ellas:

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

no funciona. – AjayR

+0

Escribo mi fórmula en las celdas de Excel y luego uso este código y funciona. –

Cuestiones relacionadas