当前位置:网站首页>C# 读取 CSV文件内数据,导入DataTable后显示
C# 读取 CSV文件内数据,导入DataTable后显示
2022-07-28 18:37:00 【laocooon】
1 99 99 99
2 88 88 88
3 77 77 77
4 66 66 66
5 55 55 55
6 44 44 44
7 33 33 33
8 22 22 22
9 11 11 11
10 0 0 0
private void button4_Click(object sender, EventArgs e)
{
string fileName = string.Empty;
string strFilePath = System.IO.Directory.GetCurrentDirectory();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = strFilePath;
openFileDialog.Filter = "(*.CSV)|*.CSV";
openFileDialog.Title = "打开数据文件";
DialogResult result = openFileDialog.ShowDialog();
if (result != DialogResult.OK)
return;
List<string> listA=new List<string>();
List<string> listB = new List<string>();
List<string> listC = new List<string>();
fileName = openFileDialog.FileName;
using (var reader = new StreamReader(fileName))
{
while (!reader.EndOfStream)
{
var line= reader.ReadLine();
var values= line.Split('\t');
if (values.Length != 3)
{
MessageBox.Show(
"不是合法数据源,程序导入数据不成功~!",
"提示信息",
MessageBoxButtons.OK,
MessageBoxIcon.Warning
);
return;
}
listA.Add(values[0]);
listB.Add(values[1]);
listC.Add(values[2]);
}
}
string[,] data = new string[listA.Count,3];
for (int i = 0; i < listA.Count; i++)
{
data[i,0] = listA[i];
data[i, 1] = listB[i];
data[i, 2] = listC[i];
Console.WriteLine($"{data[i, 0]} {data[i, 1]} {data[i, 2]}");
}
//二维数组 转 DataTable 数据源
DataTable dt = new DataTable();
for (int i = 0; i < data.GetLength(1); i++)
dt.Columns.Add(i.ToString(), typeof(int));
for (int i = 0; i < data.GetLength(0); i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < data.GetLength(1); j++)
dr[j] = data[i, j];
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;
}边栏推荐
- Teach you how to draw a map with ArcGIS [thermal map]
- 太空射击第11课: Sound and Music
- Unity package exe to read and write excel table files
- Nocturnal simulator settings agent cannot be saved
- Who cares about the safety of the battery when it ignites in a collision? SAIC GM has something to say
- Raspberrypico analytic PWM
- 产品力大幅提升 新款福特探险者发布
- LVS load balancing cluster
- DHCP.DNS.NFS
- Solve the problem that jupyter cannot import new packages
猜你喜欢
随机推荐
太空射击第16课: 道具(Part 2)
Nocturnal simulator settings agent cannot be saved
Soft raid
C# 委托 delegate 的理解
Does any elder brother know how to solve the huge flinksql log
About IP address
弹出模态框
js飞入js特效弹窗登录框
Use of DDR3 (axi4) in Xilinx vivado (5) board test
TCP.IP
卡通js射击小游戏源码
Simple example of C language 1
太空游戏第12课: 盾牌
Voice controlled robot based on ROS (II): implementation of upper computer
Dsactf July re
Raspberry Pie 3 connected to WiFi
Raspberrypico analytic PWM
[task03: complex query methods - views, subqueries, functions, etc.]
Linxu [permission, sticky bit]
太空射击第10课: Score (繪畫和文字)

![Linxu [permission, sticky bit]](/img/57/ceacb5c67b97db8a4743cb319f81d7.png)

![[fasttext -- Summary notes]](/img/4d/2871b2525bf0ea75ee958338b18013.png)





