Visual Studio 2010 (VB.Net) 및 Open XML SDK 2.0을 사용하고 있습니다. 수식을 Excel 2010 워크 시트에 어떻게 삽입합니까? 이 작업을 수행 할 때 셀의 CellValue 속성을 DBNull 또는 EmptyString으로 설정하여 사용자가 워크 북을 열 때 Excel에서 셀을 다시 계산하도록해야합니다.Open XML 사용 수식을 Excel 2010 워크 시트에 어떻게 삽입합니까?
답변
그냥과 같이 새로운 CellFormula을 널 (null)로 CellValue을두고 인스턴스화 :
Cell cell = new Cell()
{
CellReference = "A3",
DataType = new EnumValue<CellValues>(CellValues.Number),
CellFormula = "SUM(A1:A2)"
};
문서를 엑셀
이 부착 된 도움말 문서에서 오는 열 때 셀 값이 계산됩니다 수식 추가에 대한 일부 수정 사항이있는 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
이 방법은 수식에서 참조하는 각 셀에 올바르게 레이블 된 참조가 있다고 가정합니다.
이 코드의 빌드 오류가 발생합니다. CellValues. [Formula] 및 newCell.Formula에는 수식 메서드/Props가 포함되어 있지 않습니다. OpenXML2.0을 사용하고 있습니까? – eschneider
버전 : 2.0.5022.0 – eschneider
@eschneider 업데이트 된 데이터 형식 및 수식입니다. 지금 사용해보십시오. –
당신은 템플릿 엑셀에서 공식을 설정하고이를 다시 계산이 코드를 작성할 수 있습니다
spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = True spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = True
작동하지 않습니다. – AjayR
내 모든 수식을 Excel 셀에 쓰고이 코드를 사용하고 작동합니다. –
- 1. Open XML 워크 시트에 날짜를 삽입하는 방법은 무엇입니까?
- 2. ASP.NET OleDb Excel 워크 시트에 연결
- 3. Excel 파일의 모든 워크 시트에 양식을 표시하십시오.
- 4. Open XML Excel 셀 서식
- 5. ClosedXML. 기존 Excel 워크 시트에 DataTable 추가
- 6. 변수 워크 시트 이름을 여러 Excel 계산식에 어떻게 삽입합니까?
- 7. 수식을 통한 Excel 동적 하이퍼 링크
- 8. C# Excel 워크 시트 액세스
- 9. python 스크립트에서 수식을 어떻게 실행합니까?
- 10. Excel 논리 수식을 계산하십시오.
- 11. Open XML Excel 파일을 HTML로 변환
- 12. javascript에서 Office open XML Excel 파일 생성
- 13. Excel 시트에 C#
- 14. 워크 시트에 그려진 레이블에 액세스
- 15. open xml excel 자리 표시 자에 실제 값 삽입
- 16. 변환하기 전에 어떻게 xsl : param을 xsl 스타일 시트에 추가/삽입합니까?
- 17. 자바를 사용하여 Excel 스프레드 시트에 매크로 추가
- 18. excel : 행 업데이트 수식을 삽입하십시오.
- 19. VBA Excel : 워크 시트에 확인란이 너무 많습니다. 코드가 작동하지 않습니다.
- 20. VSTO를 사용하여 C# 클래스 파일의 Excel 워크 시트에 액세스
- 21. 여러 Excel 워크 시트에 HTML 테이블 가져 오기
- 22. 여러 SUMIF로 Excel 수식을 간단하게 작성
- 23. Excel 스프레드 시트에 개체 목록?
- 24. Excel 시트에 눈금 선 추가
- 25. Excel 시트에 C# IList 콘텐츠?
- 26. XSLT 시트에 XML 문자열로드
- 27. Excel VBA : 결과를 새 워크 시트에 복사하여 클라이언트 ID별로 송장 합계
- 28. Open XML SDK : Excel 셀의 일부 서식 지정
- 29. Office Open XML 형식 파일 생성
- 30. XML 내보내기 -> Excel
우리는 어떻게 엑셀을 열지 않고 할 수 있습니다. – AjayR