[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[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!

6.2

Output to excel spreadsheet using vb.net

Asked by thomashospital in .NET, Miscellaneous Programming, Visual Studio

Tags: excel, output, spreadsheet, file

I am trying to write to an excel file and everything works fine, but it writes the data twice.  I thought maybe it was my tables in my database, but I wrote code that I tested to delete my temp tables before I run this code.  Here is my code:

 Private Sub writeToExcelPatientData(ByVal fileName2 As String, ByVal dsFinal As System.Data.DataTable)
        Dim exlApp As Microsoft.Office.Interop.Excel.Application
        Dim iCol, iRow, iColVal As Integer

        Dim missing As Object = System.Reflection.Missing.Value
        Dim bNew As Boolean

        Dim i As Integer
        ' Open the document that was chosen by the dialog
        Dim aBook As Microsoft.Office.Interop.Excel.Workbook


        Try
            ''re-initialize excel app
            exlApp = New Microsoft.Office.Interop.Excel.Application

            If exlApp Is Nothing Then
                ''throw an exception
                Throw (New Exception("Unable to Start Microsoft Excel"))
            Else
                ''supresses overwrite warnings
                exlApp.DisplayAlerts = False

                'aBook = New Excel.Workbook
                ''check if file exists
                If File.Exists(fileName2) Then
                    aBook = exlApp.Workbooks.Open(fileName2)
                Else
                    aBook = exlApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet)
                End If
                With exlApp
                    .SheetsInNewWorkbook = 1
                    '.Workbooks.Add()

                    'trying to adjust where data is put
                    .Worksheets(1).Select(2)
                    'For displaying the column name in the the excel file.
                    Dim iColumn As Integer = 16
                    For iCol = 0 To dsFinal.Columns.Count - 1
                        ''clear column name before setting a new value
                        'iColumn = iColumn + iCol
                        .Cells(1, iCol + 1).Value = ""
                        .Cells(1, iCol + 1).Value = dsFinal.Columns(iCol).ColumnName.ToString
                    Next
                    'For displaying the column value row-by-row in the the excel file.

                    For iRow = 0 To dsFinal.Rows.Count - 1
                        'iColumn = 15
                        For iColVal = 0 To dsFinal.Columns.Count - 1
                            iColumn = iColumn + 1
                            'iColumn = iColumn + iColVal
                            '.Cells(iRow + 2, iColumn + 1).Value = ""
                            '.Cells(iRow + 2, iColumn + 1).Value = Trim(Dt.Rows(iRow).ItemArray(iColVal).ToString)
                            .Cells(iRow + 2, iColVal + 1).Value = Trim(dsFinal.Rows(iRow).ItemArray(iColVal).ToString)
                        Next
                    Next
                    If File.Exists(fileName2) Then
                        .ActiveWorkbook().Save() 'filename)
                    Else
                        .ActiveWorkbook().SaveAs(fileName2, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing)
                    End If
                    .ActiveWorkbook.Close()
                End With
                Console.Write("File exported sucessfully")
            End If
        Catch ex As Runtime.InteropServices.COMException

            Console.Write("ERROR: " & ex.Message)
        Catch ex As Exception

            Console.Write("ERROR: " & ex.Message)
        Finally
            exlApp.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(exlApp)
            aBook = Nothing
            exlApp = Nothing

        End Try

    End Sub

OTHER PART OF CODE***************************************'

 filename2 = "c:\Census.xls"

        Dim dataRow As DataRow
        Dim columnName As String
        Dim column As DataColumn
        Dim schemaTable1 As System.Data.DataTable
        Dim dataTable2 As System.Data.DataTable

        Do
            ' Create new data table
            schemaTable1 = myreaderCY.GetSchemaTable
            dataTable2 = DSfinal.Tables(0)
            If Not IsDBNull(schemaTable1) Then
                ' A query returning records was executed
                Dim j As Integer
                For j = 0 To schemaTable1.Rows.Count - 1
                    dataRow = schemaTable1.Rows(j)
                    ' Create a column name that is unique in the data table
                    columnName = dataRow("ColumnName")
                    'Add the column definition to the data table
                    column = New DataColumn(columnName, CType(dataRow("DataType"), Type))
                    'dataTable1.Columns.Add(column)
                Next
                'Dt.Tables.Add(dataTable1)

                'Fill the data table we just created
                While myreaderCY.Read()
                    dataRow = dataTable2.NewRow()
                    For j = 0 To myreaderCY.FieldCount - 1
                        dataRow(j) = myreaderCY(j)


                    Next
                    dataTable2.Rows.Add(dataRow)
                End While
            Else
                'No records were returned
                column = New DataColumn("RowsAffected")
                dataTable2.Columns.Add(column)
                Dt.Tables.Add(dataTable2)
                dataRow = dataTable2.NewRow()
                dataRow(0) = myreaderCY.RecordsAffected
                dataTable2.Rows.Add(dataRow)
            End If
        Loop While myreaderCY.NextResult()

    End Sub
 
Loading Advertisement...
 
[+][-]07/25/07 06:41 AM, ID: 19565216Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/25/07 07:03 AM, ID: 19565407Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/25/07 07:06 AM, ID: 19565436Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/25/07 07:24 AM, ID: 19565620Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/25/07 07:56 AM, ID: 19565957Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/25/07 09:30 AM, ID: 19567138Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/25/07 10:07 AM, ID: 19567533Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/25/07 10:08 PM, ID: 19571939Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/26/07 05:31 AM, ID: 19573777Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/26/07 05:39 AM, ID: 19573844Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/26/07 06:03 AM, ID: 19574005Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/26/07 06:06 AM, ID: 19574022Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/26/07 08:05 AM, ID: 19575136Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/26/07 08:13 AM, ID: 19575210Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/26/07 08:45 AM, ID: 19575559Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/26/07 08:49 AM, ID: 19575606Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/26/07 09:14 AM, ID: 19575864Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/26/07 09:23 AM, ID: 19575943Accepted Solution

View this solution now by starting your 30-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

Zones: .NET, Miscellaneous Programming, Visual Studio
Tags: excel, output, spreadsheet, file
Sign Up Now!
Solution Provided By: Dhaest
Participating Experts: 1
Solution Grade: A
 
[+][-]07/26/07 09:39 AM, ID: 19576088Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/26/07 11:37 AM, ID: 19577006Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/26/07 11:41 AM, ID: 19577042Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81