Search code examples
c#libreoffice-calcuno

Is there a way to load a System.Data.DataTable into a LibreOffice Calc sheet without going cell by cell?


With Excel, and using C#, you can load the entire System.Data.Datatable into the sheet by calling

mySheet.Cells("A1").LoadFromDataTable(myDataTable, True)

where mySheet is an OfficeOpenXml.ExcelWorksheet object.

I was wondering if there is something similar with unoidl.com.sun.star.sheet.XSpreadsheet and LibreOffice Calc

I checked the methods available for the unoidl.com.sun.star.sheet.XSpreadsheet class but could not find something. But maybe there is a better and fastest way to populate a LibreOffice Calc sheet with the data from a DataTable object than going cell by cell


Solution

  • Use setDataArray to insert a two-dimensional array. The Developer's Guide shows an example in Java.

    // --- Cell range data ---
    com.sun.star.sheet.XCellRangeData xData = (com.sun.star.sheet.XCellRangeData)
        UnoRuntime.queryInterface(com.sun.star.sheet.XCellRangeData.class, xCellRange);
    
    Object[][] aValues =
    {
        {new Double(1.1), Integer.valueOf(10)},
        {new Double(2.2), ""},
        {new Double(3.3), "Text"}
    };
    
    xData.setDataArray(aValues);
    

    To convert System.Data.Datatable to an array, try the following code from Datatable to Multidimensional Array.

    dt.AsEnumerable().Select(row => row.ItemArray).ToArray();