DataTable , ReaderStream, openFileDialog的使用

一 :openFileDialog 使用浏览文件的功能

       1、openFileDialog.InitialDirectory 属性;浏览时默认文件夹

       2  openFileDialog.FileName 选择的文件路径

       3  openFileDialog.Filter 过滤器(只选择某种类型的文件)

二 : ReaderStream 读取CSV文件

        1、readerstream.ReadLine()函数:读取文件的一行

        2、reader.peek()函数

三 :DataTable 使用内存表存储csv文件的数据

四 :实现的功能 导入CSV文件,将内容导入到内存中,并显示出内容

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //文件浏览功能
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            
            OpenFileDialog openfiledialog = new OpenFileDialog();
            openfiledialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;//指定浏览文件的初始化目录
            openfiledialog.Filter = "CSV文件|*.csv";//只选择csv文件
            if (openfiledialog.ShowDialog() == DialogResult.OK)
            {
                this.textPath.Text = openfiledialog.FileName;//选定文件的文件名和路径
            }
        }
        //将CSV文件中的内容导入内存表DataTable中
        private void btnLoad_Click(object sender, EventArgs e)
        {
            StreamReader streamReader = new StreamReader(this.textPath.Text,Encoding.Default);//创建文件流
            String str = "";
            String[] arrStr;//定义字符串数组
            int i = 0;

            DataTable dt = new DataTable();//定义内存表"
            DataRow dtRow;
            DataColumn dtColumn = new DataColumn("ProjectName",System.Type.GetType("System.String"));//定义一个列,列名为ProjectName
            dt.Columns.Add(dtColumn);//往内存表里添加一个列名,列名为ProjectName
            dt.Columns.Add(new DataColumn("BuildingName",System.Type.GetType("System.String")));//往内存表里添加一个列名,列名为ProjectName
            dtColumn = new DataColumn("RoomName",System.Type.GetType("System.String"));
            dt.Columns.Add(dtColumn);
           // dtRow = dt.NewRow(); //增加一行,与dt的表结构相同的行,这行代码不能加在这里
            while (streamReader.Peek() > 0)
            {
                str = streamReader.ReadLine();//从文件流中读取1行
                arrStr = str.Split(','); //把以逗号为分隔符的字符串,转成数组
                dtRow = dt.NewRow();//增加一行,与dt的表结构相同的行
                for (i = 0; i < arrStr.Length; i++)
                {
                    dtRow[i] = arrStr[i];//把数组中的值赋值给行
                }
                dt.Rows.Add(dtRow);//往dt内存表中加1行
            }
            for(i = 0; i< dt.Rows.Count;i++)
            {
                MessageBox.Show(dt.Rows[i]["ProjectName"]+" "+dt.Rows[i]["BuildingName"]);
            }
            
        }


    }
}


   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值