Private Sub GetElement()
'Read the data from the cv file
Try
Dim fileIn As String = "mel.csv"
Dim fileRows(), fileFields() As String
Dim count As Integer = 0
textBox.Text = String.Empty
If File.Exists(fileIn) Then
Dim fileStream As StreamReader = File.OpenText(fileIn)
fileRows = fileStream.ReadToEnd().Split(Environment.NewLine)
For i As Integer = 0 To fileRows.Length - 1
fileFields = fileRows(i).Split(",")
If fileFields.Length >= 2 Then
For x As Integer = 0 To fileFields.Length - 1
If x = 0 Then
For e As Integer = 0 To fileFields.Length
ele.Add(fileFields(e))
listBox.Items.Add(ele(e))
Next
End If
Next
count = count + 1
End If
Next
Else
textBox.Text = fileIn & " not found."
End If
Catch ex As Exception
textBox1.Text = ex.Message
End Try
End Sub
我有这个csv文件,我想放入一个二维数组。
unit Unit1;
{$mode delphi}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
Clipbrd, StdCtrls, Windows, Messages;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FNextClipboardOwner: HWnd; // handle to the next viewer
// Here are the clipboard event handlers
function WMChangeCBChain(wParam: WParam; lParam: LParam):LRESULT;
function WMDrawClipboard(wParam: WParam; lParam: LParam):LRESULT;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
var
PrevWndProc:windows.WNDPROC;
function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam;
lParam: LParam): LRESULT; stdcall;
begin
if uMsg = WM_CHANGECBCHAIN then begin
Result := Form1.WMChangeCBChain(wParam, lParam);
Exit;
end
else if uMsg=WM_DRAWCLIPBOARD then begin
Result := Form1.WMDrawClipboard(wParam, lParam);
Exit;
end;
Result := CallWindowProc(PrevWndProc, Ahwnd, uMsg, WParam, LParam);
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
PrevWndProc := Windows.WNDPROC(SetWindowLong(Self.Handle, GWL_WNDPROC, PtrInt(@WndCallback)));
FNextClipboardOwner := SetClipboardViewer(Self.Handle);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ChangeClipboardChain(Handle, FNextClipboardOwner);
end;
function TForm1.WMChangeCBChain(wParam: WParam; lParam: LParam): LRESULT;
var
Remove, Next: THandle;
begin
Remove := WParam;
Next := LParam;
if FNextClipboardOwner = Remove then FNextClipboardOwner := Next
else if FNextClipboardOwner <> 0 then
SendMessage(FNextClipboardOwner, WM_ChangeCBChain, Remove, Next)
end;
function TForm1.WMDrawClipboard(wParam: WParam; lParam: LParam): LRESULT;
begin
if Clipboard.HasFormat(CF_TEXT) Then Begin
ShowMessage(Clipboard.AsText);
end;
SendMessage(FNextClipboardOwner, WM_DRAWCLIPBOARD, 0, 0); // VERY IMPORTANT
Result := 0;
end;
end.
我只能收到第一行,然后我将它添加到列表框中。在MainWindow类中,我声明了两个用于存储列和行的字符串列表。
答案 0 :(得分:0)
为什么不使用对象,因为您正在使用OO&#39;编程语言。如果您创建了一个新类,它将更简单,更优化。然后在读取每一行后创建一个新对象。 Dim u as new User(x,y,z...)
并组织您的数据,使用哈希表或链接列表(不知道它是否像vb.net一样被调用)。然后,您可以从哈希表或链表中的对象填写列表框。
答案 1 :(得分:0)
试试这个......
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim folder = "Path to your CSV folder (do not include the file name here)"
Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
Dim dt As New DataTable
' change Test.csv to your csv file name here
Using Adp As New OleDbDataAdapter("select * from [Test.csv]", CnStr)
Try
Adp.Fill(dt)
Catch ex As Exception
End Try
End Using
DataGridView1.DataSource = dt
End Sub
End Class
这将导入CSV文件然后直接显示到DataGridView控件(列表框有列的问题)...希望有帮助