如何打开.CSV文件? [英] How to open a .CSV file ?

查看:107
本文介绍了如何打开.CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.CSV文件(以逗号分隔),我想用OLEDB打开,但我

得到错误外部表不是预期的格式。

如果我将.CSV文件保存到.XLS文件,我可以打开没有

问题的连接。

打开的正确方法是什么a .CSV文件?

如果我无法打开CSV文件,如何以编程方式将CSV文件

保存到XLS文件?

非常感谢。


dim myCon OleDb.OleDbConnection

myCon =新OleDbConnection(" Provider = Microsoft.Jet.OLEDB.4.0; Data

Source = c:\ file.csv;扩展属性=""" Excel 8.0; HDR = NO; IMEX = 1""")

--error 外部表格不是预期的格式。

解决方案

为什么不使用StreamReader类并用逗号分析值?

" fniles" < fn **** @ pfmail.com写信息

新闻:%2 ****************** @ TK2MSFTNGP05.phx.gbl。 ..


>我有一个.CSV文件(以逗号分隔),我想用OLEDB打开,但我得到错误外部table不是预期的格式。

如果我将.CSV文件保存到.XLS文件,我可以打开没有

问题的连接。 />
打开.CSV文件的正确方法是什么?

如果我无法打开CSV文件,怎么能以编程方式保存CSV

文件到XLS文件?

非常感谢。


dim myCon OleDb.OleDbConnection

myCon = New OleDbConnection(" Provider = Microsoft.Jet.OLEDB.4.0; Data

Source = c:\ file.csv; Extended Properties ="" Excel 8.0; HDR = NO; IMEX = 1""" ;)

--error外部表格不符合预期格式。



你好Scott M.,

因为不是所有的CSV文件都应该用逗号解析:Value

One,Value,Two,Value Three


OP,您的连接字符串错误。尝试:Provider = Microsoft.Jet.OLEDB.4.0;数据

来源= c:\;扩展属性=文字;


-Boo


为什么不直接使用StreamReader类并用

逗号分析值?


" fniles" < fn **** @ pfmail.com写信息

新闻:%2 ****************** @ TK2MSFTNGP05.phx.gbl。 ..


>我有一个.CSV文件(以逗号分隔),我想用OLEDB打开,
但是我得到了错误外部表格不是预期的格式。
如果我将.CSV文件保存为.XLS文件,我可以打开连接
而没有
问题。 />打开.CSV文件的正确方法是什么?
如果我无法打开CSV文件,如何以编程方式将
CSV
文件保存到XLS文件? dim myCon OleDb.OleDbConnection
myCon =新的OleDbConnection(" Provider = Microsoft.Jet.OLEDB.4.0; Data
Source = c:\ file.csv ;扩展属性="" Excel 8.0; HDR = NO;
IMEX = 1""")
- error"外部表格不是预期的格式。



这是我为此目的而开发的CSVLine类。它不是优雅的b $ b优雅,但它确实有效。它最初是用VB 6编写的,所以它仍然使用VB Collection对象代替.NET框架集合。您可以使用以下两种方法之一创建一个对象:


dim csv as new CSVLine

dim csv as new CSVLine( line as string,headers()as string)


第一种方法允许你从头创建一个csv行并使用

ToString方法生成一个用于写入

文件的Excel兼容csv行。


第二种方法采用excel兼容行和标题数组

字符串,允许您通过索引名称引用行的内容


dim headers()as string = split(" H1,H2,H3","," )

dim line as string =""""" Header,1""",Header 2,""" Header 3"""" br />
dim csv作为新的CSVLine(行,标题)


Debug.Print csv(" H1")''不带引号返回" Header,1"


虽然可能存在Excel兼容的CSV文件这个班不能用b
解析,在使用这个课程的几年里我没有碰到它,首先是VB 6中的
和现在的VB 2005 。


希望这会有所帮助,

Mike Ober。

============== =========

选项比较文字

选项明确开启

期权严格上


Public Class csvLine

Dim cRecs As New Collection


Public Sub New()

End Sub


Public Sub New(ByVal Line As String,ByVal Keys()As String,optional

ByVal delim As String =",")

Dim temp As String

Dim tKey As String

Dim i As Integer

Dim InQuotes as Boolean

Dim c As String =""

Dim j As Integer


for i = LBound(Keys)To UBound(Keys)

InQuotes = False

temp =""


如果Len(Line)0那么

c = Left

I have a .CSV file (comma delimited) that I want to open using OLEDB, but I
get the error "External table is not in the expected format."
If I save the .CSV file to an .XLS file, I can open the connection with no
problem.
What is the correct way to open a .CSV file ?
If I can not open the CSV file, how can I programmatically save the CSV file
to an XLS file ?
Thanks a lot.

dim myCon OleDb.OleDbConnection
myCon = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\file.csv; Extended Properties=""Excel 8.0; HDR=NO; IMEX=1""")
--error "External table is not in the expected format."

解决方案

Why not just use a StreamReader class and parse the values at the commas?
"fniles" <fn****@pfmail.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...

>I have a .CSV file (comma delimited) that I want to open using OLEDB, but I
get the error "External table is not in the expected format."
If I save the .CSV file to an .XLS file, I can open the connection with no
problem.
What is the correct way to open a .CSV file ?
If I can not open the CSV file, how can I programmatically save the CSV
file to an XLS file ?
Thanks a lot.

dim myCon OleDb.OleDbConnection
myCon = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\file.csv; Extended Properties=""Excel 8.0; HDR=NO; IMEX=1""")
--error "External table is not in the expected format."



Hello Scott M.,

Because not all CSV files are supposed to be parsed at the comma: Value
One, "Value, Two", Value Three

OP, your connection string is wrong. Try: Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\;Extended Properties=Text;

-Boo

Why not just use a StreamReader class and parse the values at the
commas?

"fniles" <fn****@pfmail.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...

>I have a .CSV file (comma delimited) that I want to open using OLEDB,
but I
get the error "External table is not in the expected format."
If I save the .CSV file to an .XLS file, I can open the connection
with no
problem.
What is the correct way to open a .CSV file ?
If I can not open the CSV file, how can I programmatically save the
CSV
file to an XLS file ?
Thanks a lot.
dim myCon OleDb.OleDbConnection
myCon = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\file.csv; Extended Properties=""Excel 8.0; HDR=NO;
IMEX=1""")
--error "External table is not in the expected format."



Here''s a CSVLine class that I developed for this very purpose. It''s not
elegant, but it works. It was originally written in VB 6, so it still uses
the VB Collection object instead of .NET framework collections. You can
create an object in one of two methods:

dim csv as new CSVLine
dim csv as new CSVLine(line as string, headers() as string)

The first method allows you to create a csv line from scratch and use the
ToString method to generate an Excel compatible csv line for writing to a
file.

The second method takes an excel compatible line and an array of header
strings and allows you to reference the contents of the line by index name

dim headers() as string = split("H1,H2,H3", ",")
dim line as string = """"Header, 1""",Header 2,"""Header 3""""
dim csv as new CSVLine(line, headers)

Debug.Print csv("H1") '' Returns without quotes "Header, 1"

Although there may be an Excel compatible CSV file that this class can''t
parse, I haven''t run across it in several years of using this class, first
in VB 6 and now in VB 2005.

Hope this helps,
Mike Ober.
=======================
Option Compare Text
Option Explicit On
Option Strict On

Public Class csvLine
Dim cRecs As New Collection

Public Sub New()
End Sub

Public Sub New(ByVal Line As String, ByVal Keys() As String, Optional
ByVal delim As String = ",")
Dim temp As String
Dim tKey As String
Dim i As Integer
Dim InQuotes As Boolean
Dim c As String = ""
Dim j As Integer

For i = LBound(Keys) To UBound(Keys)
InQuotes = False
temp = ""

If Len(Line) 0 Then
c = Left


这篇关于如何打开.CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆