我正在使用Visual Studio 2010(VB.Net)和Open XML SDK 2.0。如何將公式插入到Excel 2010工作表中?當我這樣做時,我也希望將單元格的CellValue屬性設置爲DBNull或EmptyString,以強制Excel在用戶打開工作區時重新計算單元格。使用Open XML如何將公式插入到Excel 2010工作表中?
回答
剛剛離開的CellValue爲空,實例化一個新CellFormula像這樣:
Cell cell = new Cell()
{
CellReference = "A3",
DataType = new EnumValue<CellValues>(CellValues.Number),
CellFormula = "SUM(A1:A2)"
};
文檔在Excel
這個來自附幫助文檔打開時,細胞值來計算到用於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
請注意,此方法假定您在公式中引用的每個單元格都具有正確標記的引用。
我得到此代碼的構建錯誤。 CellValues。[Formula]和newCell.Formula不包含公式方法/ Props。你在使用OpenXML2.0嗎? – eschneider 2012-07-25 16:24:11
我正在使用版本:2.0.5022.0 – eschneider 2012-07-25 16:26:27
@eschneider更新了數據類型和公式,請立即嘗試。 – 2012-07-25 17:00:59
您可以設置模板Excel公式和編寫代碼來重新計算它們:
spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = True spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = True
它不起作用。 – AjayR 2013-10-08 02:14:58
我在excel單元格中編寫我的所有公式,然後使用此代碼並運行。 – 2013-11-25 05:54:08
- 1. 如何將日期插入到Open XML工作表?
- 2. 如何以編程方式將XML插入到Excel工作表中?
- 3. 如何使用PowerShell插入Excel公式?
- 4. C#:Excel工作表到數據透視表使用Open XML?
- 5. 如何將變量工作表名稱插入多個Excel公式中?
- 6. Open XML SDK 2.0可以按名稱訪問Excel 2010工作表
- 7. 使用Sahi將數據寫入Excel工作表Open Source
- 8. 如何插入(綁定)DataTable到Excel工作簿(VS Excel 2010工作簿項目)
- 9. 使用VBA將數據表格Excel插入到Access 2010中
- 10. 如何使用POI API從Excel工作表中刪除公式?
- 11. 使用vba Excel插入公式
- 12. Excel工作表公式
- 13. Excel VBA:如何將公式插入到單元格區域?
- 14. 使用Open XML在Excel中插入指定單元格
- 15. 使用Open Xml SDK和LINQ在Excel中插入文本
- 16. excel vba - 使用變量插入公式
- 17. 如何將用戶表單嵌入到Excel工作表中?
- 18. 使用xml映射和Open XML將Excel表導出爲xml
- 19. 在Excel中使用VBA插入公式不起作用
- 20. 使用Vb.Net Com插件將多行插入到Excel工作表中
- 21. 使用LINQ和Open XML(Excel)
- 22. Excel 2010:在公式中輸入撇號?
- 23. 如何在excel中使用vba插入公式
- 24. 將Long Excel工作表公式轉換爲VBA公式
- 25. 使用OleDbConnection將行插入現有的Excel工作表中
- 26. 如何使用Visual Basic將數據插入到Open Office Base?
- 27. 使用Excel將工作表保存爲CSV公式完好
- 28. 如何使用Open XML刷新Excel 2010的數據透視表高速緩存
- 29. 插入從另一個Excel工作表中的Excel工作表
- 30. 如何使用VBA將Microsoft Word公式導入Microsoft Excel?
我們怎樣才能做到不打開Excel。 – AjayR 2013-10-08 02:14:08