9/05/2011

VB.Net : ส่งข้อมูลจาก ListView ออก OpenOffice Calc

How to export data in ListView to OpenOffice cale ?
You use code. in button click.


       Dim Result(-1) As Object
        Dim CurrLine As String = String.Empty
        Dim myDoc As Object, firstSheet As Object
        Dim oSM
        Dim oDesk As Object
        Dim arg(-1) As Double
        ' Connect file and open OpenOffice
        oSM = CreateObject("com.sun.star.ServiceManager")
        oDesk = oSM.createInstance("com.sun.star.frame.Desktop")
        ' Open calc
        myDoc = oDesk.loadComponentFromURL("private:factory/scalc", "_blank", 0, arg)
        firstSheet = myDoc.Sheets.getByIndex(0) 'index sheet insert data.
        ' Write Header Column in ListView to cell Calc.
        For columnIndex As Int32 = 0 To ListView_Sales.Columns.Count - 1
            CurrLine &= (String.Format("{0};", ListView_Sales.Columns(columnIndex).Text))
        Next
        Dim myArrayFirstRow As String()
        myArrayFirstRow = Split(CurrLine, ";")
        firstSheet.getCellRangeByName("A1").String = (myArrayFirstRow(0))
        firstSheet.getCellRangeByName("B1").String = (myArrayFirstRow(1))
        firstSheet.getCellRangeByName("C1").String = (myArrayFirstRow(2))
        firstSheet.getCellRangeByName("D1").String = (myArrayFirstRow(3))
        firstSheet.getCellRangeByName("E1").String = (myArrayFirstRow(4))
        firstSheet.getCellRangeByName("F1").String = (myArrayFirstRow(5))
        firstSheet.getCellRangeByName("G1").String = (myArrayFirstRow(6))
        firstSheet.getCellRangeByName("H1").String = (myArrayFirstRow(7))
        firstSheet.getCellRangeByName("I1").String = (myArrayFirstRow(8))
        firstSheet.getCellRangeByName("J1").String = (myArrayFirstRow(9))
        Randomize()
        ' Insert subItem in Calc.
        Dim line As Integer = 1
        Dim column As Integer = 0
        For Each item As ListViewItem In ListView_Sales.Items
            For Each subItem As ListViewItem.ListViewSubItem In item.SubItems
                firstSheet.getCellByPosition(column, line).String = subItem.Text
                column = column + 1
            Next
            column = 0  ' Reset Column before new line.
            line = line + 1 ' New line after insert data in row ListView finish.
        Next
        MsgBox("สงข้อออก Open Office เรียบร้อย ")
    End Sub


example VB.net export data in ListView to OpenOffice Calc.
Download

http://www.mediafire.com/?x9qjrd5n7u7jytl


No comments:

Post a Comment