简体   繁体   English

使用VB.net将.CSV文件转换为.XLS

[英]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. 我正在尝试使用vb.net将.csv转换为.xls,但是我无法找到有效的方法。 I know how to convert an .xls to .csv is it similar to this? 我知道如何将.xls转换为.csv,这与此类似吗? 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 . 当读取CSV文件时,您告诉Jet Engine它是带有连接字符串Extended Properties=Excel 8.0这部分内容的excel文件。 Try changing that part of the connection string to this Extended Properties='text;HDR=Yes;FMT=Delimited(,)' . 尝试将连接字符串的该部分更改为此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, 至于编写文件,您可以做一些事情,这是一个类似的SO问题,

Create Excel (.XLS and .XLSX) file from C# 从C#创建Excel(.XLS和.XLSX)文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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