Organize and Print CSV data

J

Jchick

Boy, this should be a simple bit of code but I can't figure out how to
make it happen.

I have a CSV file shows up in a directory that has 4 fields that need
to be printed on labels. Each line of the CSV looks something like
this:

AcctNo, Name, junk, junk, Address, junk, PhoneNo

I need to read each line of the CSV and print them to Avery Label stock
(30 labels per sheet, 3 columns, 10 rows). I also need them to pull
from a specific tray on the printer.

The result should look something like this:

Jim Smith Mary Smith John Doe
123 any street 50 First st 12 Maple ST
8542115 12345678 9858574
555-555-1212 555-555-1111 555-555-2222

I am thinking the steps are something like this:
1. Watch the directory for a CSV file
2. Use StreamReader to read each line of the CSV
3. Drop fields 3,4 and 6 (the Junk fields)
4. Organize fields to this order: Name, address, AcctNo, PhoneNo
5. Select the appropriate tray of a specific printer
6. Print the records in 3 rows, 10 columns.


I know Word/Excel can read a CSV, do a mailmerge and output the labels
but my customer is interested in a VB.net solution.

Is this way too complicated? Any ideas on how to get started?

Thanks in advance.
 
T

tomb

I would recommend the Word/Excel combo. If your customer insists on
reinventing the wheel, charge him double!

T
 
G

GhostInAK

Hello Jchick,

This is relatively straight forward with ADO.Net. Use a OdbcConnection/Command
or OleDbConnection/Command with the appropriate connection string (see connectionstrings.com).
Other than that, your method looks fine.

-Boo
 
C

Cor Ligthert [MVP]

Boo,

I don't see where OleDB or ODBC can help the OP?

Can you explain that further, I would keep it with his own procedure.

Cor
 
J

Jchick

I'm not familiar with the ADO stuff anyway. I'm a noob. After thinking
about it, it may be better to simply install Word (use that for the
'engine') and write macros or VB to do the process. What do ya'll
think?
 
G

GhostInAK

Hello Cor Ligthert [MVP],

ADO is the way to go when accessing CSV files. Field values can be quoted
or not.. which makes parsing my hand difficult, but not impossible. ADO
will simply be easier and faster than rolling your own CSV parser, especially
for a newbie.

-Boo
 
C

Cor Ligthert [MVP]

Boo,

Why? A classic approach needs only a classic loop whatever that is (For Do
While), which can be done even direct in the reading part. The OP shows that
he/she is a newbie in Net not in programming.

That you and I probably will direct do it with OleDb and a datatable, does
not mean in my eyes that what we do is the most efficient or the best method
for this problem.

Just my thought,

Cor
 
J

Jchick

OK, how about I simplify this. What about simply reading a CSV and
printing each record to a label? Any bits of code available for that?
 
C

Cor Ligthert [MVP]

Jchick,

This should be so simple that if you cannot make this, than try to find
somebody who can make this for you. I would be shame myself to put this kind
of simple things on Internet or it should be a part of a much wider sample.

Cor
 
S

ShaneO

Jchick said:
Boy, this should be a simple bit of code but I can't figure out how to
make it happen.

I have a CSV file shows up in a directory that has 4 fields that need
to be printed on labels. Each line of the CSV looks something like
this:

AcctNo, Name, junk, junk, Address, junk, PhoneNo

I need to read each line of the CSV and print them to Avery Label stock
(30 labels per sheet, 3 columns, 10 rows). I also need them to pull
from a specific tray on the printer.

The result should look something like this:

Jim Smith Mary Smith John Doe
123 any street 50 First st 12 Maple ST
8542115 12345678 9858574
555-555-1212 555-555-1111 555-555-2222

I am thinking the steps are something like this:
1. Watch the directory for a CSV file
2. Use StreamReader to read each line of the CSV
3. Drop fields 3,4 and 6 (the Junk fields)
4. Organize fields to this order: Name, address, AcctNo, PhoneNo
5. Select the appropriate tray of a specific printer
6. Print the records in 3 rows, 10 columns.


I know Word/Excel can read a CSV, do a mailmerge and output the labels
but my customer is interested in a VB.net solution.

Is this way too complicated? Any ideas on how to get started?

Thanks in advance.

I borrowed from the MS Help and was able to knock-up the following in a
couple of minutes. If your CSV is as you wrote then this works
perfectly (watch for wrapping!) -

Dim sString As String
Using MyReader As New
Microsoft.VisualBasic.FileIO.TextFieldParser("C:\test.csv")
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim sCurrentRow As String()
While Not MyReader.EndOfData
sString = ""
Try
sCurrentRow = MyReader.ReadFields()
sString = sCurrentRow(1) & vbCrLf & sCurrentRow(4)_
& vbCrLf & sCurrentRow(6) & vbCrLf & sCurrentRow(0)
MsgBox(sString)
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
End Try
End While
End Using

I'm not arguing that this is the "Best" solution, but it does work.

Hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
J

Jchick

ShaneO, that's awesome. Thanks for taking the time to do that. It works
perfectly and gives me something to work with.

Sorry it was too simple folks!

J
 
G

GhostInAK

Hello Cor Ligthert [MVP],

The issue is readability and intent. Some crack-ass loop with data reads
and string splits and all manner of nastiness THAT IS COMPLETELY UNNEEDED
muddles the readability and confuses the intent. Was this chunk of code
supposed to demonstrate the ability to parse a CSV file? or was this chunk
of code supposed to demonstrate reading a CSV file and manipulating the data?


There are times when re-inventing the wheel is acceptable and at time nessecary,
especialy if the existing wheel is horribly broken. However, ADO.NET is
not horribly broken, and it is absolutely the right tool for reading CSV
files (writing may be a different matter).

-Boo
 
C

Cor Ligthert [MVP]

Boo,

Did you look at the sample ShaneO has given, I think that it shows a quiet
better way than using OleDB.

Although I repeat, OleDB would be for me the first choice if it was for
myself. But from the first time I came on this board, I have showed that I
like very much datatables.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top