Help us understand the problem. What is going on with this article?

Xojo で CSV ファイルをエクスポート/インポート

エクスポート

「Exporting to text in xojo」
https://stackoverflow.com/questions/49976204/exporting-to-text-in-xojo
を参考に

ListBox1.AddRow("1", "ThunderBlade", "SEGA")
ListBox1.AddRow("2", "GALAXY FORCE", "SEGA")
ListBox1.AddRow("3", "STARBLADE", "namco")
ListBox1.AddRow("4", "DARIUS", "TAITO")

Dim f As FolderItem
Dim outcsv As TextOutputStream
Var dir As New FolderItem("/mnt", FolderItem.PathModes.Native)
f = dir.Child("itemm.txt")
outcsv = f.CreateTextFile
Dim maxRow As Integer = Listbox1.ListCount - 1
For row As Integer =  0 To maxRow
  outcsv.WriteLine(""""+ListBox1.Cell(row, 0)+""","""+ListBox1.Cell(row, 1)+""","""+ListBox1.Cell(row, 2)+""","""+ListBox1.Cell(row, 3)+"""")
Next
outcsv.Close

これはデスクトップアプリ。Webアプリの場合は Listbox1.ListCount の代わりに Listbox1.RowCount を使う。

インポート

ListBoxにインポート

「TextInputStream.ReadLine」
https://docs.xojo.com/TextInputStream.ReadLine を参考

Var f As FolderItem
Var csvFile As TextInputStream
Var csvRow As String

f = FolderItem.ShowOpenFileDialog("")
If f <> Nil Then
  csvFile = TextInputStream.Open(f)
  csvFile.Encoding = Encodings.UTF8
  Do
    csvRow = csvFile.ReadLine
    Listbox1.AddRow( NthField(csvRow,",",1).ReplaceALL("""",""), NthField(csvRow,",",2).ReplaceALL("""",""), NthField(csvRow,",",3).ReplaceALL("""",""))
  Loop Until csvFile.EndOfFile
  csvFile.Close
End If

なお、 元にした
「TextInputStream.ReadLine」
https://docs.xojo.com/TextInputStream.ReadLine
では、タブ区切りファイルをインポート、更に動的にListBoxのカラム数を増やしている。

配列にインポート

ListBoxに読み込むのは遅い。上を RaspberryPi Model 3 で試してみたら7カラムx1000件の場合で10秒程度。
配列に読めば早い。

Var f As FolderItem
Var csvFile As TextInputStream
Var csvRow As String
Var regends(0, 10) As String
var counter as Integer = 0

f = FolderItem.ShowOpenFileDialog("")
If f <> Nil Then
  csvFile = TextInputStream.Open(f)
  csvFile.Encoding = Encodings.UTF8
  Do
    regends.ResizeTo(counter,10)
    csvRow = csvFile.ReadLine
    csvRow = csvFile.ReadLine
    regends(counter,1)=NthField(csvRow,",",1).ReplaceALL("""","")
    regends(counter,2)=NthField(csvRow,",",2).ReplaceALL("""","")
    regends(counter,3)=NthField(csvRow,",",3).ReplaceALL("""","")
    counter=counter+1
  Loop Until csvFile.EndOfFile
  csvFile.Close
End If
nanbuwks
iotlt
IoT縛りの勉強会です。 毎月イベントを実施しているので是非遊びに来てください! 登壇者を中心にQiitaでも情報発信していきます。 https://iotlt.connpass.com
https://iotlt.connpass.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away