Free Support Forum -

Erros in Excel Save Method

? ex
HelpLink: Nothing
InnerException: {System.NullReferenceException}
Message: “Erros in Excel Save method”
Source: “Aspose.Excel”
StackTrace: " at Aspose.Excel.Worksheets.a(UInt32 A_0, Exception A_1)
at Aspose.Excel.Worksheets.a(String A_0, SaveType A_1, FileFormatType A_2, HttpResponse A_3)
at Aspose.Excel.Excel.Save(String resultSpreadsheet, FileFormatType fileFormatType)
at Dataqtix.Excel.DTXExport.ExportDetail(DataTable& dtDetail, Boolean mbColumnHeaders, String msTitle) in C:\Files\NET\Dataqtix.Excel\Excel.vb:line 98"
TargetSite: {System.Reflection.RuntimeMethodInfo}

My code is below. This has been working in the past, but with one of the new version it broke.

Public Sub ExportDetail(ByRef dtDetail As DataTable, ByVal mbColumnHeaders As Boolean, ByVal msTitle As String)
Dim msPath As String = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), “Aspose.Excel.lic”)

Dim i As Short

Dim excel As New Aspose.Excel.Excel(msPath)
Dim sheet As Worksheet

Dim styles As styles = excel.Styles

Dim styleIndex As Integer = styles.Add

Dim sDate As Style = styles.GetAt(styleIndex)
sDate.Font.Name = “Tahoma”
sDate.Font.Size = 8
sDate.Custom = “MM-dd-yyyy”

styleIndex = styles.Add
Dim sInt As Style = styles.GetAt(styleIndex)
sInt.Font.Name = “Tahoma”
sInt.Font.Size = 8
sInt.Custom = “##,##0;-##,##0;-”

styleIndex = styles.Add
Dim sDec As Style = styles.GetAt(styleIndex)
sDec.Font.Name = “Tahoma”
sDec.Font.Size = 8
sDec.Custom = “##,##0.00;-##,##0.00;-”

styleIndex = styles.Add
Dim sStr As Style = styles.GetAt(styleIndex)
sStr.Font.Name = “Tahoma”
sStr.Font.Size = 8
sStr.Number = 0

styleIndex = styles.Add
Dim sTitle As Style = styles.GetAt(styleIndex)
sTitle.Font.Name = “Tahoma”
sTitle.Font.Size = 8
sTitle.Font.IsBold = True
sTitle.ForegroundColor = Color.Gainsboro
'sTitle.Borders(BorderType.BottomBorder).LineStyle = CellBorderType.Medium
sTitle.Number = 0

sheet = excel.Worksheets(0)
sheet.Name = msTitle

Dim mlRow As Integer
mlRow = dtDetail.Rows.Count

sheet.Cells.ImportDataTable(dtDetail, mbColumnHeaders, 0, 0)

For i = 0 To dtDetail.Columns.Count - 1
Select Case dtDetail.Columns(i).DataType.ToString
Case GetType(System.String).ToString(), GetType(System.Char).ToString()
sheet.Cells.CreateRange(1, i, mlRow, 1).Style = sStr
Exit Select
Case GetType(System.Decimal).ToString(), GetType(System.Double).ToString(), GetType(System.Single).ToString()
sheet.Cells.CreateRange(1, i, mlRow, 1).Style = sDec
Exit Select
Case GetType(System.Int16).ToString(), GetType(System.Int32).ToString(), GetType(System.Int64).ToString()
sheet.Cells.CreateRange(1, i, mlRow, 1).Style = sInt
Exit Select
Case GetType(System.DateTime).ToString()
sheet.Cells.CreateRange(1, i, mlRow, 1).Style = sDate
Exit Select
Case Else
sheet.Cells.CreateRange(1, i, mlRow, 1).Style = sStr
Exit Select
End Select

sheet.Cells.CreateRange(0, 0, 1, dtDetail.Columns.Count).Style = sTitle

For i = 0 To dtDetail.Columns.Count - 1

Dim sdlg As New SaveFileDialog
sdlg.FileName = msTitle
sdlg.Filter = “Microsoft Excel Workbook|*.xls”
excel.Save(sdlg.FileName, SaveType.Default)
Catch ex As Exception
MessageBox.Show(ex.Message, “Error”)
End Try
End Sub

I think that it has something to do with the styles.

Dear Scott,

Yes. It’s a bug about the styles. Please download Aspose.Cells for .NET (Latest Version).
Sorry for the inconvenience. And thanks for your help.