简体   繁体   English

VB.NET 使用Microsoft OpenXML 保存excel 文件

[英]VB.NET use Microsoft OpenXML to save excel file

it's my first approach to openxml and I can't get it to work.这是我使用 openxml 的第一种方法,我无法让它工作。 Hi want to create an xls file, write a value to specific cells and save it.您好想创建一个 xls 文件,将值写入特定单元格并保存。 This is the code I wrote for now, it creates the file but doesn't write any value at all.这是我现在写的代码,它创建文件但根本不写入任何值。 What I'm missing?我缺少什么? Thanks谢谢

EDIT: as asked I addedd InsertCellInWorksheet function编辑:按照我的要求添加 InsertCellInWorksheet function

    Sub Main()
        CreateSpreadsheetWorkbook("IMPOVO.XLS")
        Dim spreadSheet As SpreadsheetDocument = SpreadsheetDocument.Open("IMPOVO.XLS", True)
        Using (spreadSheet)
    
            Dim worksheetPart As WorksheetPart = spreadSheet.WorkbookPart.WorksheetParts.First
            Dim cell As Cell = InsertCellInWorksheet("A", 1, worksheetPart)
    
            ' Set the value of cell A1.
            cell.CellValue = New CellValue("Hi World")
            cell.DataType = New EnumValue(Of CellValues)(CellValues.SharedString)
    
            ' Save the new worksheet.
            worksheetPart.Worksheet.Save()
            
        End Using
    End Sub
    
    Public Sub CreateSpreadsheetWorkbook(ByVal filepath As String)
        'Create a spreadsheet document by supplying the filepath.
        'By default, AutoSave = true, Editable = true, and Type = xlsx.
        Dim spreadsheetDocument As SpreadsheetDocument =
        SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook)
        
        'Add a WorkbookPart to the document.
        Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart
        workbookpart.Workbook = New Workbook
        
        'Add a WorksheetPart to the WorkbookPart.
        Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)()
        worksheetPart.Worksheet = New Worksheet(New SheetData())
        
        'Add Sheets to the Workbook.
        Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets())
        
        'Append a new worksheet and associate it with the workbook.
        Dim sheet As Sheet = New Sheet
        sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart)
        sheet.SheetId = 1
        
        sheets.Append(sheet)
        
        workbookpart.Workbook.Save()
        
        'Close the document.
        spreadsheetDocument.Close()
    End Sub

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

        row.InsertBefore(newCell, refCell)
        worksheet.Save()

        Return newCell
    End If
End Function

This is just a suggestion, but try explicitly closing the document at the end of the Using block like your code does in the last instruction in the CreateSpreadsheetWorkbook() method to see if that resolved the issue.这只是一个建议,但请尝试像您的代码在 CreateSpreadsheetWorkbook() 方法的最后一条指令中所做的那样在 Using 块的末尾显式关闭文档,以查看是否解决了问题。 You may also need to explicitly save the spreadsheet object at the end of the Using block.您可能还需要在 Using 块的末尾明确保存电子表格 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM