Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用Open XML如何将公式插入Excel 2010工作表?_Vb.net_Excel_Openxml_Openxml Sdk - Fatal编程技术网

Vb.net 使用Open XML如何将公式插入Excel 2010工作表?

Vb.net 使用Open XML如何将公式插入Excel 2010工作表?,vb.net,excel,openxml,openxml-sdk,Vb.net,Excel,Openxml,Openxml Sdk,我正在使用VisualStudio2010(VB.Net)和OpenXMLSDK2.0。如何将公式插入Excel 2010工作表?执行此操作时,我还希望将单元格的CellValue属性设置为DBNull或EmptyString,以强制Excel在用户打开workbok时重新计算单元格。只需将CellValue保留为null,然后实例化一个新的单元格公式,如下所示: Cell cell = new Cell() { CellReference = "A3", DataType = new

我正在使用VisualStudio2010(VB.Net)和OpenXMLSDK2.0。如何将公式插入Excel 2010工作表?执行此操作时,我还希望将单元格的CellValue属性设置为DBNull或EmptyString,以强制Excel在用户打开workbok时重新计算单元格。

只需将CellValue保留为null,然后实例化一个新的单元格公式,如下所示:

Cell cell = new Cell()
{
  CellReference = "A3",
  DataType = new EnumValue<CellValues>(CellValues.Number),
  CellFormula = "SUM(A1:A2)"
};
Cell Cell=new Cell()
{
CellReference=“A3”,
数据类型=新的枚举值(CellValues.Number),
CellFormula=“总和(A1:A2)”
};

在Excel中打开文档时,将计算单元格值。该值来自附加到Open XML SDK 2.0 for Microsoft Office帮助文件的帮助文档,并对添加公式进行了一些修改

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
Sub-Main()
Dim outputFilePath=“C:\Book1.xlsx”
Dim doc As SpreadsheetDocument=SpreadsheetDocument.Open(outputFilePath,True)
将workbookPart作为workbookPart标注=doc.workbookPart
Dim worksheetPart As worksheetPart=workbookPart.WorksheetParts.First()
插入工作表(“A”,3,工作表部分)
端接头
'给定列名、行索引和工作表部件,将单元格插入工作表中。
'如果单元格已存在,请返回它。
专用函数InsertCellInWorksheet(ByVal columnName作为字符串,ByVal rowIndex作为UInteger,ByVal工作表部件作为工作表部件)作为单元格
将工作表调整为工作表=工作表零件工作表
将sheetData设置为sheetData=工作表.GetFirstChild(属于sheetData)()
Dim cellReference作为字符串=(columnName+rowIndex.ToString())
'如果工作表不包含具有指定行索引的行,请插入一行。
将行变暗为行
如果(sheetData.Elements(行的).Where(函数(r)r.RowIndex.Value=RowIndex).Count()为0),则
row=sheetData.Elements(行中的)。其中(Function(r)r.RowIndex.Value=RowIndex.First()
其他的
行=新行()
row.RowIndex=RowIndex
sheetData.Append(行)
如果结束
'如果没有具有指定列名的单元格,请插入一个。
如果(单元格的行元素).Where(函数(c)c.CellReference.Value=columnName+rowIndex.ToString()).Count()>0),则
返回row.Elements(单元格的).Where(函数(c)c.CellReference.Value=CellReference).First()
其他的
'单元格必须按照CellReference的顺序排列。确定插入新单元格的位置。
Dim refCell As Cell=无
将每个单元格作为行中的单元格。元素(单元格的)()
如果(String.Compare(cell.CellReference.Value,CellReference,True)>0,则
refCell=单元格
退出
如果结束
下一个
Dim newCell As Cell=新单元格
newCell.CellReference=CellReference
newCell.CellFormula=新的CellFormula(“总和(A1:A2)”)
行.InsertBefore(newCell,refCell)
工作表。保存()
返回新单元格
如果结束
端函数

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

您可以在模板excel中设置公式,并编写此代码以重新计算它们:

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

我得到了这段代码的生成错误。CellValues.[Formula]和newCell.Formula不包含公式方法/道具。您使用的是OpenXML2.0吗?我使用的是版本:2.0.5022。0@eschneider更新了数据类型和公式,请现在再试。@eschneider基本上所有的问题都是:
newCell.CellFormula=newcellFormula(“SUM(A1:A2)”)
@eschneider我从
Main()
添加了我的调用代码。请确保您在
C:\Book1.xlsx
中有一个有效的Excel文档,并且只有一张工作表。不打开Excel我们怎么办。我将所有公式都写在Excel单元格中,然后使用此代码工作。