wordpress hit counter
Re: Help Me - SpreadsheetML - Formats - OpenXML Developer

Re: Help Me

Formats

Discussions about working with different Open XML Formats

Help Me

  • rated by 0 users
  • This post has 2 Replies |
  • 1 Follower
  • Hello, I cant effective fill my *.xlsx document.
    Public Class GenExcelDocument
    Private docPath As String
    Private currentRow As Row
    Private document As SpreadsheetDocument
    Private dicWorkSheets As New Dictionary(Of String, Worksheet)

    Public Sub New(ByVal docPath As String)
    Me.docPath = docPath
    currentRow = New Row()
    currentRow.RowIndex = 0
    End Sub

    Public Sub Open()
    document = SpreadsheetDocument.Open(docPath, True)
    End Sub

    Public Sub Close()
    For Each worksheet In dicWorkSheets.Values
    worksheet.Save()
    Next
    dicWorkSheets.Clear()
    document.Close()
    End Sub

    Private Function GetWorkSheet(ByVal sSheetName As String) As Worksheet
    If dicWorkSheets.ContainsKey(sSheetName) Then
    Return dicWorkSheets(sSheetName)
    Else
    Dim sheets As IEnumerable(Of Sheet) = _
    document.WorkbookPart.Workbook.Descendants(Of Sheet)().Where(Function(s) s.Name = sSheetName)

    If (sheets.Count() = 0) Then
    Throw New ArgumentException("The specified worksheet does not exist.")
    End If

    Dim worksheetPart As WorksheetPart = CType(document.WorkbookPart.GetPartById(sheets.First().Id), WorksheetPart)
    Dim ws As Worksheet = worksheetPart.Worksheet
    dicWorkSheets.Add(sSheetName, ws)
    Return ws
    End If
    End Function

    Public Sub InsertCellInRow(ByVal sSheetName As String, _
    ByVal uiRowIndex As UInteger, _
    ByVal sCellName As String, _
    ByVal sCellValue As String, _
    ByVal eCellValueType As CellValues)

    If currentRow.RowIndex.Value = 0 Then
    currentRow.RowIndex.Value = uiRowIndex
    End If
    If currentRow.RowIndex.Value = uiRowIndex Then
    AddCellInRow(uiRowIndex, sCellName, sCellValue, eCellValueType)
    Else
    Dim sheetData As SheetData = GetWorkSheet(sSheetName).GetFirstChild(Of SheetData)()
    sheetData.Append(currentRow)
    AddCellInRow(uiRowIndex, sCellName, sCellValue, eCellValueType)
    End If
    End Sub

    Private Sub AddCellInRow(ByVal uiRowIndex As UInteger, _
    ByVal sCellName As String, _
    ByVal sCellValue As String, _
    ByVal eCellValueType As CellValues)

    If currentRow.RowIndex.Value <> uiRowIndex Then
    currentRow = New Row()
    currentRow.RowIndex = uiRowIndex
    End If
    currentRow.Append(CreateCell(sCellName, uiRowIndex, sCellValue, eCellValueType))
    End Sub

    Private Function CreateCell(ByVal sCellName As String, _
    ByVal uiRowIndex As UInteger, _
    ByVal sCellValue As String, _
    ByVal eCellValueType As CellValues) As Cell
    Dim cellRetValue As New Cell

    cellRetValue.CellValue = New CellValue(sCellValue)
    cellRetValue.DataType = New EnumValue(Of CellValues)(eCellValueType)
    cellRetValue.CellReference = sCellName & uiRowIndex

    Return cellRetValue
    End Function
    End Class

    But I have many problem. When I write other sheet, then end row after it don't saved.
  • You are making this exercise much harder than it needs to be.

    The following article explains why your approach is troublesome and presents the overview of a more manageable alternative:

    http://openxmldeveloper.org/articles/7937.aspx

    And the following set of tutorials will show you how to solve this and other common SpreadsheetML challenges in the most simple and robust way:

    http://www.extrememl.com/index.php/knowledge-center/tutorials
  • Thanks.
    But I want write quickly code.
    It is necessary!!!
    I change code and he write very well, but I don't now it is correct code? and is it satisfy standard.
Page 1 of 1 (3 items)