Search code examples
vb.netexport-to-excelxlsxclosedxml

ClosedXML Export to xlsx error: 'Unable to set cell value to ' + uniqueidentifier


I'm looking to create a simple windows service to export data from SQL tables, through a .NET datatable, directly to an Excel xlsx file.

Whilst testing ClosedXML, I found that when the 1st column is a uniqueidentifier the ClosedXML.dll errors

"An unhandled exception of type 'System.ArgumentException' occurred in ClosedXML.dll

Additional information: Unable to set cell value to [insert uniqueidentifier here]"

Is there any easy way around this?

Sample Code:

Private Sub ExportToxlsxUsingClosedXML

    Dim appPath As String = Directory.GetCurrentDirectory()
    Dim filename As String = appPath & "\mytest_" & Format(Now, "yyyyMMddHHmmss") & ".xlsx"

    Dim SQL_Query As String = "select * FROM [mydb].[dbo].[mytable] where myDate >= '2017-01-01' "

    Dim myTable As DataTable = CLS_SQL.SQL_Retrieve(SQL_Query)

    Dim wb As XLWorkbook = New XLWorkbook
    wb.Worksheets.Add(myTable, "MyData")
    wb.SaveAs(filename)

End Sub

Solution

  • It appears from your findings that ClosedXML does not work with the uniqueidentifier type.

    However, as it definitely works with string types, you can cast the appropriate column to a VARCHAR(36) in the SQL query.

    It would then be possible to parse it to a Guid in .NET if it was needed as such.