Jump to content
astrinh0

Problema com a exportação para CSV

Recommended Posts

astrinh0

Boa tarde, eu tenho feita a exportação de um datatable para CSV mas não faço ideia como fazer isso para um dataset porque só consigo imprimir a primeira datatable do datasete  queria imprimir as duas! Vou deixar o código que tenho para tentarem me ajudar:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Csv As New Exporter()
        Dim o As String = save()
        Using CsvWriter As New StreamWriter(o)
            CsvWriter.Write(Csv.CsvFromDatatable(ds.Tables(0)))
        End Using
        If (abrir) Then
            System.Diagnostics.Process.Start(o)
        End If

        MessageBox.Show(o)
    End Sub

A classe que cria o CSV:

Imports System.Text
Class Exporter
    Public Sub New()
        TextDelimiter = ";"c
        TextQualifiers = """"c
        HasColumnHeaders = True
    End Sub
    Private _TextDelimiter As Char
    Public Property TextDelimiter() As Char
        Get
            Return _TextDelimiter
        End Get
        Set(ByVal value As Char)
            _TextDelimiter = value
        End Set
    End Property
    Private _TextQualifiers As Char
    Public Property TextQualifiers() As Char
        Get
            Return _TextQualifiers
        End Get
        Set(ByVal value As Char)
            _TextQualifiers = value
        End Set
    End Property
    Private _HasColumnHeaders As Boolean
    Public Property HasColumnHeaders() As Boolean
        Get
            Return _HasColumnHeaders
        End Get
        Set(ByVal value As Boolean)
            _HasColumnHeaders = value
        End Set
    End Property
    Public Function CsvFromDatatable(ByVal InputTable As DataTable) As String
        Dim CsvBuilder As New StringBuilder()
        If HasColumnHeaders Then
            CreateHeader(InputTable, CsvBuilder)
        End If
        CreateRows(InputTable, CsvBuilder)
        Return CsvBuilder.ToString()
    End Function
    Private Sub CreateRows(ByVal InputTable As DataTable, ByVal CsvBuilder As StringBuilder)
        For Each ExportRow As DataRow In InputTable.Rows
            For Each ExportColumn As DataColumn In InputTable.Columns
                Dim ColumnText As String = ExportRow(ExportColumn.ColumnName).ToString()
                ColumnText = ColumnText.Replace(TextQualifiers.ToString(), TextQualifiers.ToString() + TextQualifiers.ToString())
                CsvBuilder.Append(TextQualifiers + ColumnText + TextQualifiers)
                CsvBuilder.Append(TextDelimiter)
            Next
            CsvBuilder.AppendLine()
        Next
    End Sub
    Private Sub CreateHeader(ByVal InputTable As DataTable, ByVal CsvBuilder As StringBuilder)
        For Each ExportColumn As DataColumn In InputTable.Columns
            Dim ColumnText As String = ExportColumn.ColumnName.ToString()
            ColumnText = ColumnText.Replace(TextQualifiers.ToString(), TextQualifiers.ToString() + TextQualifiers.ToString())
            CsvBuilder.Append(TextQualifiers + ExportColumn.ColumnName + TextQualifiers)
            CsvBuilder.Append(TextDelimiter)
        Next
        CsvBuilder.AppendLine()
    End Sub
End Class

Obrigado pela compreensão :P

Share this post


Link to post
Share on other sites
Rechousa

Olá,

Ainda bem que ele gera o CSV para um única tabela. É isso que estás a mandar o programa fazer  :P

O que tens de fazer é gerar o CSV para todas as tabelas que o DataSet tiver, ou seja, é +/- isto (não testei):

Using CsvWriter As New StreamWriter(o)
   For Each dt As DataTable In ds.Tables
       CsvWriter.Write(Csv.CsvFromDatatable(dt))
   Next
End Using


Pedro Martins

Sharing is Knowledge!

http://www.linkedin.com/in/rechousa

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.