Advertisement

06.09.2008 at 07:58AM PDT, ID: 23469273
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.7

VB.NET 2005 Excel spreadsheet save error

Asked by gwosgood in Microsoft Visual Basic.Net

Tags:

Greetings experts;

I have an application that allows custom queries to a SQL 2005 database and displays the results in a datagrid.  The user then has the option of exporting the resulting datagrid to either a .TXT or .XLS file for further data analysis.

I am having serious difficulty saving the created excel spreadsheet.  I can create, open and write the data to the workbook, however it will not allow me to save the file.

At this line --

oXL.ActiveWorkbook.SaveAs(.FileName)

The catch section of the try block executes and I get an error message saying that it can not access the given file.  The base .xls file is created in the desired path, however the data is missing, since the file can not save properly, and an error message pops up stating that the file is corrupt.

Any help you can give me on this issue would be greatly appreciated.  thank you ahead of time for your support.


Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
Private Sub ExportGridContentsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles ExportGridContentsToolStripMenuItem.Click
        Dim strCSV As String = ""
        Dim strFilename As String = ""
        Dim swExport As StreamWriter
        Dim intCount As Integer = 0
        Dim intRow As Integer = 0
        Dim intColumn As Integer = 0
 
        SaveFileDialog1.Title = "Save File As "
        SaveFileDialog1.InitialDirectory = defaultExportLocation
 
        Try
            With SaveFileDialog1
                .Filter = "TXT files|*.txt|Excel 97-2003 Workbook|*.xls"
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    strFilename = .FileName
                    swExport = New StreamWriter(.OpenFile)
                    intCount = 0
 
                    Application.DoEvents()
                    Me.Cursor = Cursors.WaitCursor
 
                    If .FileName.Contains(".txt") Then
 
                        For Each dgColumn As DataGridViewTextBoxColumn In DataGridView1.Columns
                            If dgColumn.Index = 0 Then
                                strCSV = Chr(34) & dgColumn.HeaderText & Chr(34)
                            Else
                                strCSV = strCSV & ";" & Chr(34) & dgColumn.HeaderText & Chr(34)
                            End If
                        Next
 
                        swExport.WriteLine(strCSV)
 
                        For intRow = 0 To DataGridView1.RowCount - 2
                            strCSV = ""
                            For intColumn = 0 To DataGridView1.ColumnCount - 1
                                If intColumn = 0 Then
                                    ' first entry
                                    strCSV = DataGridView1.Item(intColumn, intRow).Value.ToString
                                Else
                                    strCSV = strCSV & ";" & DataGridView1.Item(intColumn, intRow).Value.ToString
                                End If
                            Next intColumn
 
                            ' print contents of string here
                            swExport.WriteLine(strCSV)
                            intCount = intCount + 1
                        Next intRow
 
                        swExport.Close()
                    ElseIf .FileName.Contains(".xls") Then
                        Dim oXL As New Excel.Application
                        Dim oWB As Excel.Workbook = oXL.Workbooks.Add
                        Dim oSheet As Excel.Worksheet = CType(oWB.Worksheets.Add, Excel.Worksheet)
                        Dim intColumnMax, intRowMax As Integer
                        
                        intRowMax = DataGridView1.Rows.Count - 1
                        intColumnMax = DataGridView1.Columns.Count - 1
 
                        For intRow = 0 To intRowMax
                            For intColumn = 0 To intColumnMax
                                oSheet.Cells(intRow + 1, intColumn + 1) = DataGridView1.Item(intColumn, intRow).Value.ToString
                            Next
                            intCount = intCount + 1
                        Next
 
                        oSheet.Columns.AutoFit()
                        oSheet.Activate()
 
                        If System.IO.File.Exists(.FileName) Then
                            oXL.DisplayAlerts = False
                            oXL.ActiveWorkbook.SaveAs(.FileName)
                            oXL.ActiveWorkbook.Close()                            
                            oXL.DisplayAlerts = True
                        Else
                            oSheet.SaveAs(.FileName)
                        End If
 
                        oXL.Quit()
                        oXL = Nothing
                        oWB = Nothing
                        oSheet = Nothing
                    End If
                End If
            End With
 
            MsgBox(intCount & " records exported to file", MsgBoxStyle.OkOnly)
            Me.Cursor = Cursors.Default
 
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
 
 
[+][-]06.11.2008 at 08:45AM PDT, ID: 21761282

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Visual Basic.Net
Tags: Visual Basic .NET 2005
Sign Up Now!
Solution Provided By: planocz
Participating Experts: 1
Solution Grade: A
 
 
[+][-]06.11.2008 at 09:06AM PDT, ID: 21761513

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.12.2008 at 07:34AM PDT, ID: 21769870

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.12.2008 at 09:50AM PDT, ID: 21771371

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628