Imports System
Imports System.Text
Imports System.IO
Imports Infragistics.Excel
Imports Infragistics.Win
Imports Infragistics.Win.UltraWinGrid
Imports Infragistics.Win.UltraWinGrid.ExcelExport
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim exporter As New ExcelExporter(Me.ultraGrid1, "temp.xls")
exporter.Export(Infragistics.Excel.WorkbookFormat.Excel97To2003, "ErrorLog.htm")
End Sub
Public Class ExcelExporter
Private grid As UltraGrid = Nothing
Private fileName As String = String.Empty
Private logStream As FileStream = Nothing
Private hasErrors As Boolean = False
Public Sub New(ByVal grid As UltraGrid, ByVal fileName As String)
MyBase.New()
Me.grid = grid
Me.fileName = fileName
End Sub
Public Function Export(ByVal workbookFormat As WorkbookFormat, ByVal log As String) As Boolean
' Clear the 'hasErrors' flag
Me.hasErrors = False
' Create the log file stream if the caller wants to log the results
If String.IsNullOrEmpty(log) = False Then Me.logStream = New FileStream(log, FileMode.Create, FileAccess.ReadWrite)
' Create a new instance of the UltraGridExcelExporter class.
Dim excelExporter As New UltraGridExcelExporter()
' Set ExportFormulas to true
ExcelExporter.ExportFormulas = True
Try
' Handle the events that are fired when a formula is exported.
AddHandler excelExporter.FormulaExporting, AddressOf Me.excelExporter_FormulaExporting
AddHandler excelExporter.FormulaExported, AddressOf Me.excelExporter_FormulaExported
AddHandler excelExporter.FormulaExportError, AddressOf Me.excelExporter_FormulaExportError
AddHandler excelExporter.InitializeSummary, AddressOf Me.excelExporter_InitializeSummary
If Not Me.logStream Is Nothing Then Me.WriteToLog("<HTML><BODY>")
' Export
excelExporter.Export(Me.grid, Me.fileName, workbookFormat)
If Not Me.logStream Is Nothing Then Me.WriteToLog("</BODY></HTML>")
Catch
Me.hasErrors = True
Finally
' Detach the event handlers
RemoveHandler excelExporter.FormulaExporting, AddressOf Me.excelExporter_FormulaExporting
RemoveHandler excelExporter.FormulaExported, AddressOf Me.excelExporter_FormulaExported
RemoveHandler excelExporter.FormulaExportError, AddressOf Me.excelExporter_FormulaExportError
RemoveHandler excelExporter.InitializeSummary, AddressOf Me.excelExporter_InitializeSummary
If Not Me.logStream Is Nothing Then
Me.logStream.Close()
Me.logStream.Dispose()
End If
end try
Return Me.hasErrors = False
End Function
' Handles the InitializeSummary event.
Private Sub excelExporter_InitializeSummary(ByVal sender As Object, ByVal e As InitializeSummaryEventArgs)
' Format the log entry
Dim logEntry As String = String.Format("Initializing summary '{0}' (Excel format string = '{1}', .NET format string = '{2}')...", e.Summary.Key, e.ExcelFormatStr, e.FrameworkFormatStr)
' Write the log entry
Me.WriteToLog(logEntry, False)
End Sub
' Handles the FormulaExporting event.
Private Sub excelExporter_FormulaExporting(ByVal sender As Object, ByVal e As FormulaExportingEventArgs)
' Export the formula.
e.Action = FormulaExportAction.ExportFormula
' Format the log entry
Dim logEntry As String = String.Format("Exporting formula '{0}' for cell '{1}' (Context = {2})", e.GridFormula, e.WorksheetCell, e.Context)
' Write the log entry
Me.WriteToLog(logEntry, False)
End Sub
' Handles the FormulaExported event.
Private Sub excelExporter_FormulaExported(ByVal sender As Object, ByVal e As FormulaExportedEventArgs)
' Format the log entry
Dim logEntry As String = String.Format("Exported formula '{0}' for cell '{1}' (Context = {2})", e.GridFormula, e.WorksheetCell, e.Context)
' Write the log entry
Me.WriteToLog(logEntry, False)
End Sub
' Handles the FormulaExportError event.
Private Sub excelExporter_FormulaExportError(ByVal sender As Object, ByVal e As FormulaExportErrorEventArgs)
' Record the error
Me.hasErrors = True
' Suppress the error for subsequent instances of this formula
e.Action = FormulaExportErrorAction.CancelAll
' Don't try to write out potentially invalid formulas
e.ApplyInvalidFormulaIfPossible = False
' Write the error to the log
Dim logEntry As String = String.Format("Error exporting grid formula '{0}' to excel formula '{1}' for cell '{2}' (Error = {3}, Description = '{4}')", e.GridFormula, e.ExcelFormula, e.WorksheetCell, e.Error, e.ErrorText)
Me.WriteToLog(logEntry, True)
End Sub
Private Sub WriteToLog(ByVal s As String)
Me.WriteToLog(s, False)
End Sub
Private Sub WriteToLog(ByVal s As String, ByVal isError As Boolean)
Dim quote As String = Chr(34).ToString()
If (isError) Then s = String.Format("{0}{1}{2}", quote + "<font color=" + quote + "#FF0000" + quote + ">", s, "</font>")
s = String.Format("{0}{1}{2}", s, "<br>", Environment.NewLine)
If Not Me.logStream Is Nothing Then
Dim encoding As ASCIIEncoding = New ASCIIEncoding()
Dim bytes As Byte() = encoding.GetBytes(s)
Me.logStream.Write(bytes, 0, bytes.Length)
End If
End Sub
End Class