Difficulty Saving existing .XLS

Feb 6, 2011 at 4:22 PM

As a preface -- I'm very new to VB.net.  So, take that as you will.

I'm running into some errors saving an existing XLS file.  I'm using the "save" code from Hunter Beanland's ExcelNPOI wrapper:

 

Public Function Save(ByVal XlsFilePath As String) As Boolean
        Try
            Dim fs = New FileStream(XlsFilePath, FileMode.Create)
            xlWorkbook.Write(fs)
            fs.Close()
            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        End Try
    End Function

 

The context in which I call the function is as follows:

 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
       
        Using fs As New FileStream(CompDir & SoftwareXL, FileMode.Open, FileAccess.ReadWrite)
            Dim xlWorkbook = New HSSFWorkbook(fs)
            xlSheet = xlWorkbook.GetSheet("Log")
            fs.Close()
        End Using

        Dim rCntNew As Integer
        rCntNew = xlSheet.LastRowNum + 1

        xlRow = xlSheet.CreateRow(rCntNew)

        xlCell = xlRow.CreateCell(1)
        xlCell.SetCellValue(ComboBox4.Text)

        xlCell = xlRow.CreateCell(2)
        xlCell.SetCellValue(Date.Today)

        xlCell = xlRow.CreateCell(3)
        xlCell.SetCellValue(ComboBox3.Text)

        xlCell = xlRow.CreateCell(4)
        xlCell.SetCellValue(TextBox1.Text)

        xlCell = xlRow.CreateCell(5)
        xlCell.SetCellValue(ComboBox5.Text)

        xlCell = xlRow.CreateCell(6)
        xlCell.SetCellValue(TextBox2.Text)

        xlCell = xlRow.CreateCell(7)
        xlCell.SetCellValue(Environment.UserName.ToString)

        Save(CompDir & "Test-" & SoftwareXL)

        releaseObject(xlWorkbook)
        releaseObject(xlSheet)
        releaseObject(xlRow)
        releaseObject(xlCell)

        Form1_Load(Me, Nothing)
    End Sub

 

The strings in the save function are the file path (compdir), a test prefix so i don't override my existing data, and the name of the excel file (SoftwareXL).

During the initial load of the form that I'm using, I get the following exceptions, but I've read that they're not probably that big of a deal (especially because it's during the load, not during the save):

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll

Then, when I try and save the data in the form to the excel file, I get:

A first chance exception of type 'System.NullReferenceException' occurred in iCore QA Module.exe

With a "Object reference not set to an instance of an object" message box...

Does anyone have any thoughts?  It would be most appreciated!

Thanks,

Mike

 

 

 

 

 

 

 

Coordinator
Feb 10, 2011 at 11:36 PM

What's Hunter Beanland's Excel NPOI wrapper?

Feb 11, 2011 at 1:38 PM

It's a wrapper that he wrote to bundle some of the NPOI functions into more basic "save" and "read cell" and "write to cell" functions.  You can read a little about it in the link below.

http://www.beanland.net.au/Programming/dotnet/ExcelNPOI/readme.htm

I used the code that he used for his "save" function, which is what you see above in my first post. 

-Mike

Feb 22, 2011 at 5:14 PM

Any thoughts? I'm really loving NPOI and I'm certain that it's something I'm doing wrong on my end.  This saving functionality is critical to what I'm doing, however.

Anything would be great!

 

Thanks,

 

Mike