Listbox to Excel?

Peter,
The new updated code you have just so kindly provided - does it go inside a module like your previous example?
Do I now call it like this:

myListbox.CSVOut

might be a good idea to add a text qualifier in the event that the source data contains the delimiter character.

Sorry - I meant call it like this:

myListBox.textOut

(Banging my head against my wall - but laughing at the same time) :slight_smile:

Any ideas what the complete code (including a text qualifier) would look like.
I have added the snippet 3 times, and each time I do it - the code changes :slight_smile: :slight_smile:

Really appreciate all your help and improvements guys.

Yes - place in a module and call as you wrote above.

Rich brings up a very good point.

The Tab delimited method needs to account for a tab character being in the listbox data. The comma delimited method should account for double quote characters being in the listbox data.

Peter - there will definitely be no tabs in the columns.

It is for a simple piece of software whereby I can log my income and expenses.
Each column will contain data such as:

1.00
56.50
1,345.00

There may be a comma in a ListBox cell if someone enters 1,345.00 as an income or expense amount.
So basically - only digits, a comma, and a period will be in any given cell.

Hopefully, your final code you posted will be able to export both columns in a spreadsheet readable format.

Hi,

some partial copy paste from my own app to help you get started if you want to use xojo’s excelapplication

Dim excel As New ExcelApplication
Dim book As ExcelWorkbook
dim f as FolderItem
dim i as integer

f=GetFolderItem(“c:\data\OrderForm.xlsx”)
excel.Workbooks.Open(f.AbsolutePath)
excel.Visible = true
excel.Range(“C10”).value = txtDevices.Text
excel.Range(“G69”).value = labToday.Text
excel.Range(“B73”).value = txtOmanimi.Text
excel.Range(“E16”).Value = “Yes”

for i = 1 to val(txtDevices.Text)
excel.Range(“J” + str(i + 20)).Value = “1”
excel.Range(“D” + str(i + 20)).Value = “N”
next

That fills excel workbook (.xslx) file with few cell data from textfields and at the end it fills a couple column of data with predefined data (1 and N), leaves the excel application open to be edited.

Before working officeautomation apps with xojo, remember to copy MSOfficeAutomation.rbx from Xojo (version) \Extras\Office Automation\ folder to Xojo (version)\Plugins folder. Oh exceptions,
Exception err as OLEException
MsgBox err.message

hth, petteri

Note that Petteri’s code is Windiws only.

Thanks Petteri - way above my current level of knowledge, but appreciated anyway.

Thank You.

How does one escape commas in a .csv?

Example

(Open, Save, SaveAs with comma)

change it to whatever you want

SaveCSV LB, ",", Window1 // ListBox Name, delimiter, Window Name

Tab-delim is much less finicky, and you get it for free with listbox.cell(-1,-1).

To be really pedantic a tab-delimited file should really have the extension “.tsv”.

Here in the Netherlands we use the semi-column instead of the comma in csv-files.
Main reason is the fact that the comma is used here as a decimal separator.
I believe the semi-column is more reliable then using the commonly used comma.
Just my 2 cents.

You might be interested in two classes that I have created for CSV file handling. One for CSV input and one for CSV output.

Get them here: https://dl.dropboxusercontent.com/u/10747116/RealStudioClasses/sbCSV_In_Out.zip

Tim’s suggestion is a good one. CSV is an ill-conceived, inconsistent mess and must die:
http://www.fourthworld.com/embassy/articles/csv-must-die.html

Please help it die: make importers where needed, but never write CSV exporters.

@Simon: many apps just use .tab for the saner tab-delimited format.

BTW: If anyone here wants to contribute a Xojo example of a CSV parser for that page that’s complicated enough to correctly handle the example data shown there I’d be happy to add it.

take a look at http://www.scout-soft.com/xlistbox/

or maybe http://www.smoothpaygold.com/realbasic/excelwriter.html with this classes you can export to excel using the same recordset that fill a listbox, its works under any platform.

[quote=80796:@Richard Gaskin]Tim’s suggestion is a good one. CSV is an ill-conceived, inconsistent mess and must die:
http://www.fourthworld.com/embassy/articles/csv-must-die.html

Please help it die: make importers where needed, but never write CSV exporters.

@Simon: many apps just use .tab for the saner tab-delimited format.

BTW: If anyone here wants to contribute a Xojo example of a CSV parser for that page that’s complicated enough to correctly handle the example data shown there I’d be happy to add it.[/quote]
Actually I agree with you, but CSV is here and used a great deal in business so I’m not sure that it can die!

I have slightly changed my csv class and it now supports your example in the blog.

Download and try: https://dl.dropboxusercontent.com/u/10747116/RealStudioClasses/sbCSV_In_Out.zip

Changed the routines so now will work with both of the examples you showed in the blog item. The same download link as above.

you can replace tab

excelOut.Write me.cell(-1,-1).ReplaceAll(chr(9),",")
1 Like