Solved

CSV To EXCEL CONVERSION

Posted on 2006-07-08
17
493 Views
Last Modified: 2013-11-13
Dear Sir,
I have a CSV (delimited character is  ;) file and want to convert into EXCEL file  using a VB program directly . For this I written following code :

Dim ex1 As Excel.Application
Dim ex2 As Excel.Workbook
Set ex1 = CreateObject("excel.application")
Set ex2 = ex1.Workbooks.Open("c:\a.csv")

ex1.Visible = False
ex2.SaveAs ("c:\bvm1.xls")
ex1.Quit
End Sub


My problem is that , I got the file in EXCEL , but delimeter characters are appearing in EXCEL file and no column wise seperation as per Delimeter character is made .
Kindly guide me with a sample code how to achieve the same .

Rgds
B Mittal
0
Comment
Question by:bvmittal
  • 4
  • 4
  • 3
  • +2
17 Comments
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
Try this:

Set ex2 = ex1.Workbooks.Open("c:\a.csv", 1, False, 2)
0
 
LVL 2

Expert Comment

by:cwile
Comment Utility
Try this:

    Dim ex1 As Excel.Application
    Dim ex2 As Excel.Workbook
    ex1 = CreateObject("excel.application")
    ex2 = ex1.Workbooks.Open("C:\a.csv", , , , , , , , Chr(44))
    ex1.Visible = False
    ex2.SaveAs("c:\bvm1.xls")
    ex1.Quit()


It asks you to save over bvm1.xls, if you say choose to overwrite it will make the .xls file open in the respected columns, without commas.
0
 
LVL 17

Expert Comment

by:jasonsbytes
Comment Utility
Have you tried using a comma instead of a ; for the delimeter?
0
 

Author Comment

by:bvmittal
Comment Utility
As suggested by Cwile, I tried but it is not working . The generated XLS file still has Delimeter Character and Excel is not able to open the file as a true xls file , rather it identifies it as a csv file only .

Rgds
B V Mittal
0
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
Did you try my solution? It explicitly addresses CSV import.
0
 
LVL 15

Expert Comment

by:Raisor
Comment Utility
Hi,

Here's some VB Code that I'd suggest:

    Dim x
    Dim MyString As String
    Dim Newstring As String
    Dim Oneletter
    ConvertedName = "C:\result.xls"
    sSourceFile = "C:\test.csv"
    Open ConvertedName For Output As #2
    Open sSourceFile For Input As #1
    Do While Not EOF(1)
        Input #1, MyString
        For x = 1 To Len(MyString)
            Oneletter = Mid(MyString, x, 1)
            If Asc(Oneletter) = 59 Then
                Oneletter = vbTab
            End If
            DoEvents
            Newstring = Newstring & Oneletter
        Next x
        Print #2, Newstring
        Newstring = ""
        DoEvents
    Loop
    Close #1
    Close #2


I've tested it at my systems and it worked ... if does not work at your systems please let me know!

Best regards,
Raisor
0
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
You can. of course explicitly name the parameters too:

ex1.Workbooks.Open FileName:="c:\a.csv", Format:=xlCSV, Delimiter:=","
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:Raisor
Comment Utility
Hi,

I might add that I've placed:

test1;test2;test3
test4;test5;test6

in a file named "C:\test.csv"

... and it turned out to be well formated "C:\result.xls"

;-))


Best regards,
Raisor
0
 

Author Comment

by:bvmittal
Comment Utility
As suggested by Cwile , I tried and incase Delimeter Character used is "," then I get proper XLS output file generated . But incase Delimeter is ";" then output XLS file has  all delimeter character in it and not properly formated .

Kindly help me .

Regards

B V Mittal
0
 
LVL 15

Accepted Solution

by:
Raisor earned 84 total points
Comment Utility
Hi,

Have you tried yet?:

    Dim x
    Dim MyString As String
    Dim Newstring As String
    Dim Oneletter
    ConvertedName = "C:\result.xls"
    sSourceFile = "C:\test.csv"
    Open ConvertedName For Output As #2
    Open sSourceFile For Input As #1
    Do While Not EOF(1)
        Input #1, MyString
        For x = 1 To Len(MyString)
            Oneletter = Mid(MyString, x, 1)
            If Asc(Oneletter) = 59 Then
                Oneletter = vbTab
            End If
            DoEvents
            Newstring = Newstring & Oneletter
        Next x
        Print #2, Newstring
        Newstring = ""
        DoEvents
    Loop
    Close #1
    Close #2

... placing:

test1;test2;test3
test4;test5;test6

in a file named: "C:\test.csv"

... and if you tried, what went wrong?

As I've stated ... I've tested the code on my systems and it perfectly worked the way you'd asked for ...


Best regards,
Raisor
0
 
LVL 2

Assisted Solution

by:cwile
cwile earned 83 total points
Comment Utility
I misread your original post.
Here is something quick I sorta thought up that you may wish to try:
Need the IMPORTS SYSTEM.IO at the beginning. Could convert the targets and what not to variables if you'd like. When it asks to save the file, just make sure that the file type is set to .xls and not .csv and it should convert the semi-colons to commas and then save fine for excel format.

Dim ex1 As Excel.Application
    Dim ex2 As Excel.Workbook
    Dim slines As String
    Dim target As String = "c:\a.csv"
    Dim x As Integer = 0

    TextBox1.Visible = False '// hide the textbox

    For Each slines In File.ReadAllLines(target)
      If x > 0 Then '// if you don't want the column headers
        TextBox1.Text = TextBox1.Text & Replace(slines, Chr(59), Chr(44)) & vbCrLf
      End If
      x = x + 1
    Next
    'TextBox1.Visible = True

    '// creat temp file to store new data
    File.WriteAllText("c:\temp.csv", TextBox1.Text)


    ex1 = CreateObject("excel.application")
    ex2 = ex1.Workbooks.Open("C:\temp.csv", , , , , , , , Chr(44))
    ex1.Visible = False
    ex2.SaveAs("c:\bvm1.xls")
    ex1.Quit()

    '// delete temp file
    File.Delete("c:\temp.csv")
0
 
LVL 2

Expert Comment

by:cwile
Comment Utility
forgot to add that you need to make sure that the textbox's multiline is set to true.
0
 
LVL 15

Assisted Solution

by:bpmurray
bpmurray earned 83 total points
Comment Utility
By the way, you'll want to save and close correctly - try:
        ex2.SaveAs("c:\temp\zzz.xls", Excel.XlFileFormat.xlWorkbookNormal)
        ex2.Close(False)
0
 
LVL 15

Expert Comment

by:Raisor
Comment Utility
Hi,

I haven't tested other approaches but mine ... mine worked out on my systems and the solution did what bvmittal was asking for (if I understood his question well) ... if it didn't work out for bvmittal ... well, then I'm sorry ;-))


Best regards,
Raisor
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

One of the most frequently asked questions on EE in the "Windows Installer" zone is how to eliminate self-triggered installation of some product.  The problem occurs when, suddenly, whenever a certain application is launched, or even when a folder i…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now