简体   繁体   中英

Converting .CSV file to .XLS using VB.net

I am trying to convert a .csv to .xls using vb.net but I am having trouble finding something that works. I know how to convert an .xls to .csv is it similar to this? Here is the code I have so far and this is the error I recieve:

"External table is not in the expected format."

Public Sub ConvertCSV(ByVal lvFileName As String, ByVal lvFileNameNoExt As String)

Dim DA As New OleDb.OleDbDataAdapter

Dim DS As New DataSet

Dim objStreamWriter As StreamWriter

Dim objconn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\GD Test\Test.csv;Extended Properties=Excel 8.0;")


    ObjConn.Open()

    Dim objcmd As New System.Data.OleDb.OleDbCommand("Select * from [AutoBatchPrintBypass.RPT$]", objconn)

    objcmd.CommandType = CommandType.Text


    objStreamWriter = New StreamWriter(gvDipDir & gvClientCode & ".XLS")


    objconn.Close()

End Sub

When reading the CSV file, your telling the Jet Engine that its an excel file with this part of the connection string Extended Properties=Excel 8.0 . Try changing that part of the connection string to this Extended Properties='text;HDR=Yes;FMT=Delimited(,)' . I got that from this page .

As for writing the file, there are a few things you can do, here is a similiar SO question,

Create Excel (.XLS and .XLSX) file from C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM