exporteren naar excel

Status
Niet open voor verdere reacties.

mustangBE

Gebruiker
Lid geworden
2 jun 2007
Berichten
349
hallo.

ik exporteer mijn db naar excel met volgende code.
maar in mijn DB zitten getallen die beginnen met een 0 , vb 048533.
als ik dit exporteer naar excel wordt de 0 weggelaten in excel.
aangezien dit een product referentie is moet ze er wel bij staan.
hoe kan ik dat op lossen ???

Code:
#Region "exporteer artikelen uit Excel"


    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click

        Dim msg As String
        Dim title As String
        Dim style As MsgBoxStyle
        Dim response As MsgBoxResult
        msg = "Do you want to continue?"   ' Define message.
        style = MsgBoxStyle.DefaultButton2 Or _
           MsgBoxStyle.Critical Or MsgBoxStyle.YesNo
        title = "CategorieImport"   ' Define title.
        ' Display message.
        response = MsgBox(msg, style, title)
        If response = MsgBoxResult.Yes Then   ' User chose Yes.
            dialog6.Labelcounting.Visible = True
            Me.Cursor = Cursors.WaitCursor
            ExportCategorie()
            ' Perform some action.
        Else
            ' Perform some other action.
        End If
    End Sub

    Private Sub ExportCategorie()
        dialog6.Show()
        'verfying the datagridview having data or not
        If ((CataloogDataGridView.Columns.Count = 0) Or (CataloogDataGridView.Rows.Count = 0)) Then
            Exit Sub
        End If
        'Creating dataset to export
        Dim dset As New DataSet
        'add table to dataset
        dset.Tables.Add()
        'add column to that table
        For i As Integer = 0 To CataloogDataGridView.ColumnCount - 1
            dset.Tables(0).Columns.Add(CataloogDataGridView.Columns(i).HeaderText)
        Next
        'add rows to the table
        Dim dr1 As DataRow
        For i As Integer = 0 To CataloogDataGridView.RowCount - 1
            dr1 = dset.Tables(0).NewRow
            For j As Integer = 0 To CataloogDataGridView.Columns.Count - 1
                dr1(j) = CataloogDataGridView.Rows(i).Cells(j).Value
            Next
            dset.Tables(0).Rows.Add(dr1)
        Next
        Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
        Dim wBook As Microsoft.Office.Interop.Excel.Workbook
        Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
        wBook = excel.Workbooks.Add()
        wSheet = wBook.ActiveSheet()
        Dim dt As System.Data.DataTable = dset.Tables(0)
        Dim dc As System.Data.DataColumn
        Dim colIndex As Integer = 0
        Dim rowIndex As Integer = 0
        Dim row As Integer = 0

        For Each dc In dt.Columns
            colIndex = colIndex + 1
            excel.Cells(1, colIndex) = dc.ColumnName
        Next
        For Each ItemR In dt.Rows

            Dim Columns As Integer = 0
            For Each ItemC In dt.Columns
                excel.Cells(row + 2, Columns + 1) = dt.Rows(row).Item(Columns).ToString
                Columns = Columns + 1
            Next
            Columns = 0
            row = row + 1
            'Resttijd in sec en min
            Dim Starttijd As Integer
            Dim Tijdloop As Integer
            Dim Seconden As Integer
            Dim Minuten As Integer
            Starttijd = (dt.Rows.Count).ToString
            Tijdloop = (row).ToString
            Seconden = FormatNumber(((Starttijd - Tijdloop) / 60 Mod 59), 0)
            Minuten = FormatNumber(((Starttijd - Tijdloop) / (60) / (60) - (Seconden / 60)), 0)
            dialog6.Label1.Text = "Export data to Excel Resterende tijd ongeveer " & Minuten & " Min " & Seconden & " Sec"
            Try
                If Minuten <= 0 Then
                    dialog6.Labelcounting.Text = "Export data to Excel Resterende tijd ongeveer " & Seconden & " Sec"
                End If
            Catch ex As Exception
                Seconden = FormatNumber(((Starttijd - Tijdloop) / 60 Mod 59), 0)
            End Try
            'Progresbar aansturing en label info

            dialog6.ProgressBar1.Value = (row.ToString / dt.Rows.Count.ToString * 100)
            dialog6.copiedlabel.Text = "Copied " & FormatNumber((row.ToString / dt.Rows.Count.ToString * 100), 0) & " %"
            dialog6.Labelcounting.Text = "Copied Data So Far :- " & row.ToString() - 1
            dialog6.ProgressBar1.PerformStep()  'increment your bar with each record

        Next

        wSheet.Columns.AutoFit()    'Let op pas je pat aan
        Dim strFileName As String = "C:\cataloog\export.xls"
        Dim blnFileOpen As Boolean = False
        Try
            Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
            fileTemp.Close()
        Catch ex As Exception
            blnFileOpen = False
        End Try

        If System.IO.File.Exists(strFileName) Then
            System.IO.File.Delete(strFileName)
        End If

        wBook.SaveAs(strFileName, FileFormat:=-4143)
        excel.Workbooks.Open(strFileName)
        excel.Visible = False
        MsgBox("Export Categorie gereed", MsgBoxStyle.Information, "Export Categorie")
        dialog6.ProgressBar1.Value = 0
        dialog6.Labelcounting.Text = Nothing
        dialog6.copiedlabel.Text = Nothing
        Me.Cursor = Cursors.Default
        dialog6.Close()
        Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("Excel")

        For Each p As Process In pProcess
            p.Kill()
        Next

    End Sub

#End Region
 
Status
Niet open voor verdere reacties.

Nieuwste berichten

Terug
Bovenaan Onderaan