I would like to thank those who helped me
I modified a code that I found on Libre Office under VB.net
and I manage to export the data from the datagridview to a Libre Office Document Writer
I will post the VB.net code that I modified it may help another person
I now have to Add an image before this table from a BMP or Jpeg file
I don't know how to do that yet
and save the file and then print it
Code: Select all
Imports System.Data.SqlClient
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
Imports unoidl.com.sun.star.lang
Imports unoidl.com.sun.star.uno
Imports unoidl.com.sun.star.bridge
Imports uno.util
Public Class FormTestLO
Sub ExportetoLibreOffice()
Dim xContext As XComponentContext
xContext = Bootstrap.bootstrap()
Dim xFactory As XMultiServiceFactory
xFactory = DirectCast(xContext.getServiceManager(), _
XMultiServiceFactory)
'Create the Desktop
Dim xDesktop As unoidl.com.sun.star.frame.XDesktop
xDesktop = DirectCast(xFactory.createInstance("com.sun.star.frame.Desktop"), _
unoidl.com.sun.star.frame.XDesktop)
'Open a new empty writer document
Dim xComponentLoader As unoidl.com.sun.star.frame.XComponentLoader
xComponentLoader = DirectCast(xDesktop, unoidl.com.sun.star.frame.XComponentLoader)
Dim arProps() As unoidl.com.sun.star.beans.PropertyValue = _
New unoidl.com.sun.star.beans.PropertyValue() {}
Dim xComponent As unoidl.com.sun.star.lang.XComponent
xComponent = xComponentLoader.loadComponentFromURL( _
"private:factory/swriter", "_blank", 0, arProps)
Dim xTextDocument As unoidl.com.sun.star.text.XTextDocument
xTextDocument = DirectCast(xComponent, unoidl.com.sun.star.text.XTextDocument)
'Create a text object
Dim xText As unoidl.com.sun.star.text.XText
xText = xTextDocument.getText()
Dim xSimpleText As unoidl.com.sun.star.text.XSimpleText
xSimpleText = DirectCast(xText, unoidl.com.sun.star.text.XSimpleText)
'Create a cursor object
Dim xCursor As unoidl.com.sun.star.text.XTextCursor
xCursor = xSimpleText.createTextCursor()
'Inserting some Text
xText.insertString(xCursor, "Table des actes :" _
& vbLf, False)
'________________________________________________________________
' 'Create a text object
'Create instance of a text table with dgv columns and dgv rows
Dim objTextTable As Object
objTextTable = DirectCast(xTextDocument, unoidl.com.sun.star.lang.XMultiServiceFactory). _
createInstance("com.sun.star.text.TextTable")
Dim xTextTable As unoidl.com.sun.star.text.XTextTable
xTextTable = DirectCast(objTextTable, unoidl.com.sun.star.text.XTextTable)
'******************************************************
Dim RowCount0 As Integer = dgv.Rows.Count
Dim ColumnCount0 As Integer = dgv.Columns.Count
xTextTable.initialize(RowCount0 + 1, ColumnCount0)
'**********************************************************************
xText.insertTextContent(xCursor, xTextTable, False)
'Set the table background color
Dim xPropertySetTable As unoidl.com.sun.star.beans.XPropertySet
xPropertySetTable = DirectCast(objTextTable, unoidl.com.sun.star.beans.XPropertySet)
xPropertySetTable.setPropertyValue("BackTransparent", New uno.Any(False))
xPropertySetTable.setPropertyValue("BackColor", New uno.Any(&HFFFFFF))
' blanc &HFFFFFF
'Bleu clair &HCCCCFF
'Bleu foncé &H6666AA
'Get first row
Dim xTableRows As unoidl.com.sun.star.table.XTableRows
xTableRows = xTextTable.getRows()
Dim anyRow As uno.Any
anyRow = DirectCast(xTableRows, unoidl.com.sun.star.container.XIndexAccess).getByIndex(0)
'Set a different background color for the first row
Dim xPropertySetFirstRow As unoidl.com.sun.star.beans.XPropertySet
xPropertySetFirstRow = DirectCast(anyRow.Value, unoidl.com.sun.star.beans.XPropertySet)
xPropertySetFirstRow.setPropertyValue("BackTransparent", New uno.Any(False))
xPropertySetFirstRow.setPropertyValue("BackColor", New uno.Any(&HCCCCFF))
'Fill the first table row
insertIntoCell("A1", "Acte", xTextTable)
insertIntoCell("B1", "Type", xTextTable)
insertIntoCell("C1", "Prix", xTextTable)
insertIntoCell("D1", "Cod", xTextTable)
For Each row As DataGridViewRow In dgv.Rows
Dim Acte As String = row.Cells(0).Value
Dim Prix As String = row.Cells(1).Value
Dim Type As String = row.Cells(2).Value
Dim Cod As String = row.Cells(3).Value
Dim R As Integer = row.Index
R = R + 2
Dim RStr As String = R
Dim AIndex As String
Dim Bindex As String
Dim CIndex As String
Dim DIndex As String
AIndex = "A" + RStr
Bindex = "B" + RStr
CIndex = "C" + RStr
DIndex = "D" + RStr
insertIntoCell(AIndex, Acte, xTextTable)
insertIntoCell(Bindex, Prix, xTextTable)
insertIntoCell(CIndex, Type, xTextTable)
insertIntoCell(DIndex, Cod, xTextTable)
Next
End Sub
Sub insertIntoCell(ByVal sCellName As String, ByVal sText As String, _
ByVal xTable As unoidl.com.sun.star.text.XTextTable)
Dim xCell As unoidl.com.sun.star.table.XCell
xCell = xTable.getCellByName(sCellName)
Dim xSimpleTextCell As unoidl.com.sun.star.text.XSimpleText
xSimpleTextCell = DirectCast(xCell, unoidl.com.sun.star.text.XSimpleText)
Dim xCursor As unoidl.com.sun.star.text.XTextCursor
xCursor = xSimpleTextCell.createTextCursor()
Dim xPropertySetCursor As unoidl.com.sun.star.beans.XPropertySet
xPropertySetCursor = DirectCast(xCursor, unoidl.com.sun.star.beans.XPropertySet)
xPropertySetCursor.setPropertyValue("CharColor", New uno.Any(65536))
xSimpleTextCell.insertString(xCursor, sText, False)
End Sub