当前位置:网站首页>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;
}边栏推荐
- js飞入js特效弹窗登录框
- UE4 3dui widget translucent rendering blur and ghosting problems
- 太空射击第13课: 爆炸效果
- Explain RESNET residual network in detail
- Read JSON configuration file to realize data-driven testing
- Product manager interview | innovation and background of the fifth generation verification code
- 平均海拔4000米!我们在世界屋脊建了一朵云
- PXE_ KS unattended system
- 【pytorch】LSTM神经网络
- 激光slam:LeGO-LOAM---代码编译安装与gazebo测试
猜你喜欢
随机推荐
Explain RESNET residual network in detail
UE4 3dui widget translucent rendering blur and ghosting problems
Talking about canvas and three rendering modes in unity
UE4.25 Slate源码解读
[C language] 5000 word super detailed explanation of various operations of the sequence table
Linxu [permission, sticky bit]
File lookup and file permissions
Usage Summary of thymeleaf
Raspberry pie CM4 -- using metartc3.0 to integrate ffmpeg to realize webrtc push-pull streaming
太空射击第11课: Sound and Music
MySQL error: specified key was too long; max key length is 767 bytes
Raspberry pie uses the command line to configure WiFi connections
Solve the kangaroo crossing problem (DP)
平均海拔4000米!我们在世界屋脊建了一朵云
TCP.IP
Raspberry pie 4B parsing PWM
Common instructions of vim software
关于链接到其他页面的标题
Teach you how to draw a map with ArcGIS [thermal map]
js图片悬挂样式照片墙js特效



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





