0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2020-03-28

エクスポート

「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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?