Export listbox data to other formats?

I’m currently using several listboxes and want to export the data so it can be used for different applications. Right now i’m using the: Listbox5.Cell(-1,-1) type method and then saving the strings to a file, which is a tab-delimited file. Is there a way to export and save it in HTML format or other formats for example?

Do you mean, is there some built-in function for this? No, you’d have to write the code yourself.

The easiest common format is JSON, so I’d recommend that. Maybe something like this:

Function ToJSONItem (Extends lb As ListBox) As JSONItem
  dim root as new JSONItem

  dim lastRow as integer = lb.ListCount - 1
  dim lastCol as integer = lb.ColumnCount - 1
  for row as integer = 0 to lastRow
    dim rowJSON as new JSONItem
    for col as integer = 0 to lastCol
      rowJSON.Append lb.Cell( row, col )
    next col
    root.Append rowJSON
  next row

  return root
End Function

(This is off the top of my head and not tested.)

You also can export to csv (surrounding quotes and a comma as Column delimiter).

Basically, you do:

For LoopIdx = 0 To RowCnt // Read the ListBox and save the data ExportTOS.WriteLine Chr(34) + LB.Cell(LoopIdx,0) + Chr(34) + "," + Chr(34) + LB.Cell(LoopIdx,1) + Chr(34) Next

Chr(34) is a quote (")
LB is the name of my ListBox (my default name when a more pertinent is not needed).
That ListBox have only two Columns to be saved.

Real code extracted from my Export_As_CSV(ExportFI) Method.


That code will fail if you have any quotes in the data itself. A ReplaceAll(celldata,&u22,&u22+&u22) is needed to escape them. (&u22 is a newer synonym for Chr(34).)

Thanks, Emile!

In my case, all I have is letters and numbers in 9 different columns (saving a “box score” for an auto race). Want to save the results so players can re-view the results later on, and have it load into various formats for leagues and the like. So it would need to be a method where I can easily reload the saved file data into listboxes later on. The method I mentioned in the opening post sometimes produces wonky results (missing carriage returns, etc).

Anyone have more methods for doing this?

Didn’t I post actual code for doing this or am I hallucinating…

I liked your code, Kem :slight_smile: It may end up being what I use, for purposes of highest compatibility.
Just looking for tips to exporting to other formats as well, that way I can offer players a choice.

JSON is likely not the best format for this. Tab delimited text is likely the best choice. I would choose it over comma delim any day. If you’re having problems with ListBox5.Cell(-1,-1), it’s because you haven’t filled in all the cells. At least put a “” in the cell. There is unfortunately a difference between a cell having no value and the cell having an empty string.

Curious: Why don’t you like JSON for this purpose?

it’s just that fewer other apps will read it. And it certainly isn’t human readable. Or at least grokable. A tab-delim file will at least resemble a table in a simple text editor. And look good in excel, etc.

Aside from disagreeing about "human readable ( :slight_smile: ), your points are all valid.

json can be as clear or obtuse as you want much like XML

I do not saw that.

At Export time: it always have worked fine since I add a quote on Cell left and right contents, etc…

At Import time, I use Mid(ALine,2,Len(ALine)-2) for each Column.

These piece fo code comes from my head / memory and I can be wrong (I don’t think so).


care to explain why JSon vs csv (ot tab return text file) ?

Using QuickLok (OS X) to show a csv file is very fine (try if you have a Mac): it shows the text into read grid.

The best view I ever found is… a ListBox with resizeable columns (or better an auto-size the columns) and two colors Row background.

OR: a table in an html file (less easy to edit, but that depend on your needs).