2011-06-10 47 views

回答

6

刚刚离开的CellValue为空,实例化一个新CellFormula像这样:

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

文档在Excel

+1

我们怎样才能做到不打开Excel。 – AjayR 2013-10-08 02:14:08

0

这个来自附帮助文档打开时,细胞值来计算到用于Microsoft Office帮助文件的Open XML SDK 2.0,并对添加公式进行了一些修改。

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 

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

+0

我得到此代码的构建错误。 CellValues。[Formula]和newCell.Formula不包含公式方法/ Props。你在使用OpenXML2.0吗? – eschneider 2012-07-25 16:24:11

+0

我正在使用版本:2.0.5022.0 – eschneider 2012-07-25 16:26:27

+0

@eschneider更新了数据类型和公式,请立即尝试。 – 2012-07-25 17:00:59

0

您可以设置模板Excel公式和编写代码来重新计算它们:

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

它不起作用。 – AjayR 2013-10-08 02:14:58

+0

我在excel单元格中编写我的所有公式,然后使用此代码并运行。 – 2013-11-25 05:54:08

相关问题