Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
910
Formatting currency in exported spreadsheet.
posted

I am checking the value of each cell in the exporter event. I've defined different columns as different data types. For some reason, though, the code

e.CurrentWorksheet.Rows(e.CurrentRowIndex).Cells(e.CurrentColumnIndex).CellFormat.FormatString = "$#,##0.00"

doesn't seem to be working. In debug, I see it enter 

Case "Currency"
       Dim exportvalue As Decimal
       If Decimal.TryParse(e.GridValue.ToString, exportvalue) Then
       e.ExportValue = exportvalue
       e.CurrentWorksheet.Rows(e.CurrentRowIndex).Cells(e.CurrentColumnIndex).CellFormat.FormatString = "$#,##0.00"
End If

However, a number like '65.0' will remain like that in the spreadsheet, instead of looking like '$65.00".

Is the cell not updated yet? Do Ihave to do the formatting later, after the fact, and if so, how and where - how can I manipulate the spreadsheet after it's exported (office is not on this server), or is this the right place, and I'm just doing something wrong?

Thanks.

Here's the entire exporter event routine.

Private Sub UltraGridExcelExporter1_CellExporting(sender As Object, e As ExcelExport.CellExportingEventArgs) Handles UltraGridExcelExporter1.CellExporting


Dim colptr As Integer
Dim skipit As Boolean = False
Try
      colptr = e.GridColumn.Index
Catch ex As Exception
     skipit = True
End Try
If Not (skipit) Then
     Dim datatype As String
     Try
          datatype = datatypeline.Split(Param_Delim)(colptr + 1)
     Catch ex As Exception
     End Try
     Try
     Select Case datatype
     Case "Int16"
          Dim exportvalue As Int16
          If Int16.TryParse(e.GridValue.ToString, exportvalue) Then
               e.ExportValue = exportvalue
          End If
     Case "Int32"
          Dim exportvalue As Int32
          If Int32.TryParse(e.GridValue.ToString, exportvalue) Then
               e.ExportValue = exportvalue
          End If
     Case "Int64"
          Dim exportvalue As Int64
          If Int64.TryParse(e.GridValue.ToString, exportvalue) Then
               e.ExportValue = exportvalue
          End If
     Case "Decimal"
          Dim exportvalue As Decimal
          If Decimal.TryParse(e.GridValue.ToString, exportvalue) Then
               e.ExportValue = exportvalue
          End If
     Case "Double"
          Dim exportvalue As Double
          If Double.TryParse(e.GridValue.ToString, exportvalue) Then
               e.ExportValue = exportvalue
          End If
     Case "Currency"
          Dim exportvalue As Decimal
          If Decimal.TryParse(e.GridValue.ToString, exportvalue) Then
               e.ExportValue = exportvalue
               e.CurrentWorksheet.Rows(e.CurrentRowIndex).Cells(e.CurrentColumnIndex).CellFormat.FormatString = "$#,##0.00"
          End If
     Case "Date"
          Dim exportvalue As Date
          If Date.TryParse(e.GridValue.ToString, exportvalue) Then
               e.ExportValue = exportvalue
          End If
     Case "DateTime"
          Dim exportvalue As DateTime
          If DateTime.TryParse(e.GridValue.ToString, exportvalue) Then
               e.ExportValue = exportvalue
          End If
     Case Else
     End Select
Catch ex As Exception
End Try
End If

End Sub

Parents
  • 34430
    Verified Answer
    Online posted

    Hello Samuel,

    It appears from the code that you have provided that you are performing your cell formatting in the CellExporting event, which may be slightly too early. Rather than doing this, I would recommend doing your formatting in the CellExported event, as this event will fire after the cell has finished exporting and allow you to format the already-existing value in that cell.

    Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Software Developer

Reply Children
No Data