User Tools

Site Tools


k17:k17.7:k17.7.6:start

17.7.6 GridView - Data export to a csv file

This project is not primarily concerned with displaying data in a GridView, but with backing up data that is displayed in a GridView. The data is saved in a csv file.

This text file format is used as a transfer format for simply structured data. Each row contains a data record, where the individual data fields are separated from the other fields by a field separator and a field value is enclosed by a text separator. Optionally, the first line can also contain the field labels.

csv-Datei

Figure 17.7.6.1: Saving data in a csv file

Here you see a section with the data stored in a csv file - please compare with figure 17.7.6.1 - where the 4 field names are stored in the first data set:

"Number","Boolean","String","Date"
"76,987","True","Emma","01.11.2012"
"-90,454","False","Hans","29.03.2013"
"-16,513","False","Anna","25.06.2012"
"-98,831","True","Robert","17.08.2013"
"-63,46","True","Stefan","03.11.2013"

The comma serves as field separator and the quotation mark '' is used as text separator. In the export form, you can select both the field separator and the text separator from a predefined character set. You can also specify whether the field names are to be saved in the first data record.

Optionen

Figure 17.7.6.2: Specifying storage options in a separate form

You determine the storage location and the file name for the csv file in a dialog:

Save-Dialog

Figure 17.7.6.3: save dialog

To use the source code for saving data in a csv file in other projects, a separate form for setting the saving options and a module were used.

The complete source code for the module can be found here:

' Gambas module file
 
PUBLIC sFeldTrennzeichen AS String
PUBLIC sTextTrennzeichen AS String
PUBLIC bSaveTitel AS Boolean
PUBLIC bError AS Boolean
 
PUBLIC SUB ExportGridView2CSV(Grid AS GridView,pFeldTrenner AS String, pTextTrenner AS String, /
                              pSaveTitel AS Boolean)
 
  DIM iCount, rCount, cCount AS Integer
  DIM sZeile AS String
  DIM hFile AS File
 
  Dialog.Title = "Speichern Sie die Daten in einer csv-Datei!"
  Dialog.Filter = ["*.csv", "csv-Dateien"]
  IF Dialog.SaveFile() = TRUE THEN 
     Message.Warning("Der Daten-Export wurde abgebrochen!")
     RETURN ' Cancel-Button     
  ELSE 
   ' Die CSV-Datei wird neu angelegt oder geleert
     IF File.Ext(Dialog.Path) <> "csv" AND File.Ext(Dialog.Path) <> "txt" THEN 
        Dialog.Path = File.SetExt(Dialog.Path, "csv")
     ENDIF 
     hFile = OPEN Dialog.Path FOR WRITE CREATE
     IF ERROR THEN 
        Message.Error(Error.Text & Error.Where)
        RETURN
     ENDIF ' ERROR
     seek">SEEK #hFile, 0
 
   ' Spaltenüberschriften speichern (optional)
     IF pSaveTitel = TRUE THEN 
        IF Grid.Header = Grid.Horizontal OR Grid.Header = Grid.Both THEN
           FOR iCount = 0 TO Grid.Columns.Count - 1
               IF iCount > 0 THEN 
                  sZeile &= pFeldTrenner & pTextTrenner & Grid.Columns[iCount].Title & pTextTrenner
               ELSE 
                  sZeile &= pTextTrenner & Grid.Columns[iCount].Title & pTextTrenner
               ENDIF ' iCount > 0
           NEXT 
           PRINT #hFile, sZeile 
        ENDIF ' Grid.Horizontal OR Grid.Header?
     ENDIF ' bTitel = TRUE?
 
   ' GridView-Daten speichern
     sZeile = ""
     FOR rCount = 0 TO Grid.Rows.Count - 1
         FOR cCount = 0 TO Grid.Columns.Count - 1
             IF cCount > 0 THEN 
                sZeile &= pFeldTrenner & pTextTrenner & Grid[rCount, cCount].Text & pTextTrenner
             ELSE 
                sZeile &= pTextTrenner & Grid[rCount, cCount].Text & pTextTrenner
             ENDIF ' cCount > 0  
         NEXT ' cCount
         PRINT #hFile, sZeile 
         sZeile = ""
     NEXT ' rCount  
   ENDIF ' Dialog.SaveFile() = TRUE?
 
  CLOSE #hFile
 
END ' GridViewExport2CSV

In the main program, the procedure with which a data export is started is of particular interest:

PUBLIC SUB ExportGridView2CSV()
  FExport.ShowModal
  IF MCSV.bError = FALSE THEN 
     MCSV.GridView2CSV(GridView1, MCSV.sFeldTrennzeichen, MCSV.sTextTrennzeichen, MCSV.bSaveTitel)
     btnGridViewExportCSV.Enabled = TRUE
  ELSE 
    Message.Warning("Der Daten-Export wurde abgebrochen!")
    btnGridViewExportCSV.Enabled = TRUE
  ENDIF ' MCSV.bError = FALSE?
END ' ExportGridView2CSV()

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k17/k17.7/k17.7.6/start.txt · Last modified: 30.09.2023 by emma

Page Tools