2011-06-10 5 views

답변

6

그냥과 같이 새로운 CellFormula을 널 (null)로 CellValue을두고 인스턴스화 :

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

문서를 엑셀

+1

우리는 어떻게 엑셀을 열지 않고 할 수 있습니다. – AjayR

0

이 부착 된 도움말 문서에서 오는 열 때 셀 값이 계산됩니다 수식 추가에 대한 일부 수정 사항이있는 Microsoft Office 도움말 파일 용 Open XML SDK 2.0

Main()한 장 빈 Excel 문서를 찾아 A3를 셀에 SUM() 공식을 추가합니다.

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

+0

버전 : 2.0.5022.0 – eschneider

+0

@eschneider 업데이트 된 데이터 형식 및 수식입니다. 지금 사용해보십시오. –

0

당신은 템플릿 엑셀에서 공식을 설정하고이를 다시 계산이 코드를 작성할 수 있습니다

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

작동하지 않습니다. – AjayR

+0

내 모든 수식을 Excel 셀에 쓰고이 코드를 사용하고 작동합니다. –

관련 문제