zoukankan      html  css  js  c++  java
  • 读取csv文件

    public static DataTable OpenCSV(string filePath)
    {
        DataTable dt = new DataTable();
        FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    
        StreamReader sr = new StreamReader(fs);
        string strLine = "";
        string[] aryLine = null;
        string[] tableHead = null;
        int columnCount = 0;
        bool IsFirst = true;
        while ((strLine = sr.ReadLine()) != null)
        {
            if (IsFirst == true)
            {
                tableHead = strLine.Split(',');
                IsFirst = false;
                columnCount = tableHead.Length;
                for (int i = 0; i < columnCount; i++)
                {
                    DataColumn dc = new DataColumn(tableHead[i]);
                    dt.Columns.Add(dc);
                }
            }
            else
            {
                aryLine = strLine.Split(',');
                DataRow dr = dt.NewRow();
                for (int j = 0; j < columnCount; j++)
                {
                    dr[j] = aryLine[j];
                }
                dt.Rows.Add(dr);
            }
        }
        if (aryLine != null && aryLine.Length > 0)
        {
            dt.DefaultView.Sort = tableHead[0] + " " + "asc";
        }
        sr.Close();
        fs.Close();
        return dt;
    }
    
    private void btOK_Click(object sender, EventArgs e)
    {
        foreach (DataRow dr in OpenCSV(textBox1.Text).Rows)
        {
            // Get the first column
            checkedListBox1.Items.Add(dr["Tests"]);
        }
    }
  • 相关阅读:
    ibatis核心内容概述
    ibatis selectKey
    setTimeout ;setInterval
    <select>设置multiple="multiple"属性后 下拉框全部展开了 不再是折叠的怎么回事
    $.ajax()实例
    html页面不显示中文
    sublime卡顿
    error_reporting
    js笔记
    怎样查看MYSQL数据库的端口号
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/10572476.html
Copyright © 2011-2022 走看看