如何将读取的cvs文件的内容写入datagrid?(达人帮忙啊!)

awk88 2005-07-19 02:20:27
我现在只能读出文件名,如何读内容呢?

我的部分代码如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mystream As System.IO.Stream
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.Filter = "txt files (*.csv)|*.csv"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
mystream = OpenFileDialog1.OpenFile()
If Not (mystream Is Nothing) Then
' Insert code to read the stream here.
mystream.Close()
End If
End If

TextBox1.Text = OpenFileDialog1.FileName
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ds As New DataSet
Dim dt As DataTable
Dim dr As DataRow
dt = ds.Tables(OpenFileDialog1.FileName)

End Sub


在button2里面是读内容
dt = ds.Tables(OpenFileDialog1.FileName)
以上这句都不出来!


...全文
180 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Brunhild 2005-07-22
  • 打赏
  • 举报
回复
1. 最好为你的TXT文件建立一个schema.ini来描述其结构,见:
http://community.csdn.net/Expert/TopicView3.asp?id=4155953

2.
Me.OleDbConnection1.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=text;data source=c:\temp"
Me.OleDbConnection1.Open()
Dim cmd As New OleDb.OleDbCommand("select * from 文件名.txt", Me.OleDbConnection1)
Dim adp As New OleDb.OleDbDataAdapter(cmd)
Dim tbl As New DataTable
adp.Fill(tbl)
Me.DataGrid1.DataSource = tbl
awk88 2005-07-20
  • 打赏
  • 举报
回复
不能MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\myData.XLS; " & _
"Extended Properties=Excel 8.0; ")
应该是Extended Properties=text;
farrio 2005-07-20
  • 打赏
  • 举报
回复
我自己做了一下试验,首先直接连接csv文件好像不行。目前通过excel文件连接,但是说我的isam driver没有找到。本人做ado.net不多,望高手支招。
xiongyong 2005-07-19
  • 打赏
  • 举报
回复
对啊,通过导入到excel去做
awk88 2005-07-19
  • 打赏
  • 举报
回复
现在出现错误:“外部table的格式不正确”,但是我的明明是好的csv文件啊!
henrysap 2005-07-19
  • 打赏
  • 举报
回复

试试类似的方法:

Dim DS As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As System.Data.OleDb.OleDbConnection

MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=C:\myData.XLS; " & _
"Extended Properties=Excel 8.0;")
' Select the data from Sheet1 of the workbook.
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [Sheet1$]", MyConnection)

DS = New System.Data.DataSet()
MyCommand.Fill(DS)
MyConnection.Close()
awk88 2005-07-19
  • 打赏
  • 举报
回复
ado.net对于excel的通道是怎么样的啊?
怎么都没人了啊?
awk88 2005-07-19
  • 打赏
  • 举报
回复
jackie615,我按照你的函数不能打开csv文件,显示错误信息“数据库的形式 'C:\import.csv'不能确认 ”
awk88 2005-07-19
  • 打赏
  • 举报
回复
用ado.net似乎更加困难!
mathsword 2005-07-19
  • 打赏
  • 举报
回复
楼上的思路值得考虑一下
farrio 2005-07-19
  • 打赏
  • 举报
回复
csv文件可以通过execl读取,能不能通过ado.net对于excel的通道来读取csv文件呢?
jackie615 2005-07-19
  • 打赏
  • 举报
回复
给你一个函数吧
参数一: 文件名称
参数二: 读出多少列,也就是文件中的前几段. 你可以去掉这个参数.修改一下吧
返回的是DataTable 你也可以修改成DataSet:

#region read file
/// <summary>
/// read data from file and return a data table
/// </summary>
/// <param name="strFile">file name</param>
/// <param name="iCol">the columns number of the data table</param>
/// <returns></returns>
public DataTable readfile(string strFile, int iCol)
{
DataTable dt = new DataTable();
DataRow dr;
FileStream fs = new FileStream(strFile, FileMode.Open, FileAccess.Read);
StreamReader r = new StreamReader(fs,System.Text.Encoding.Default);

string output = "";
string[] arrData;

// init datatable columns
for (int i=0;i<iCol;i++)
{
dt.Columns.Add(new DataColumn(i.ToString(), typeof(string)));
}

// read file
r.BaseStream.Seek(0, SeekOrigin.Begin);
while (r.Peek() > -1)
{
output = FmtString(r.ReadLine());

arrData = output.Split(',');

if (arrData.Length<iCol)
{
iCol = arrData.Length;
}

dr = dt.NewRow();
for (int i=0;i<iCol;i++)
{
dr[i] = arrData[i];
}
dt.Rows.Add(dr);
}

r.Close();

return dt;
}
#endregion

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧