C# study 8th day

12 篇文章 0 订阅

today ,i will improve my software's memory way,sqlite to excel,because using sql will employ system  recourse.

so first problem i meet is how to get the path from a explore.I used following code 

 OpenFileDialog fd = new OpenFileDialog();
            fd.InitialDirectory = _path;
            fd.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
            if (fd.ShowDialog(this) == DialogResult.OK)
            {
                MessageBox.Show(fd.FileName);
            }
this openfiledialog used regular expression to filte files,just show files that end with ".xls",and i will get the file'path if user click "ok".

if you just want to open the explore ,you can do it by following code ,more simply.

  System.Diagnostics.Process.Start(_path); 

so when i got the path i will use the stream to parse file content.because the ".csv" file is more efficient when the data is large,i create a temporary file in CSV format to copy the file  user selected.

  _helper = new ExcelHelper(fd.FileName, fd.FileName.Replace(".xls", ".csv"));
                _helper.SaveAsFile(SaveAsFileFormat.CSV);
                FileStream s = File.OpenRead(fd.FileName.Replace(".xls",".csv"));
                CSVReader csv = new CSVReader(s);
i display the data with  datagridview,this way need a datatable to bind the view.so i find some code to do it .

        /// <summary>  
        /// 把一个M行N列的二维数组转换为DataTable  
        /// </summary>  
        /// <param name="ColumnNames">一维数组,代表列名,不能有重复值</param>  
        /// <param name="Arrays">M行N列的二维数组</param>  
        /// <returns>返回DataTable</returns>  
        public static DataTable Convert(string[] ColumnNames, string[,] Arrays)
        {
            DataTable dt = new DataTable();

            foreach (string ColumnName in ColumnNames)
            {
                dt.Columns.Add(ColumnName, typeof(string));
            }

            for (int i1 = 0; i1 < Arrays.GetLength(0); i1++)
            {
                DataRow dr = dt.NewRow();
                for (int i = 0; i < ColumnNames.Length; i++)
                {
                    dr[i] = Arrays[i1, i].ToString();
                }
                dt.Rows.Add(dr);
            }

            return dt;

        }

but there are some problems, the stream  of c# can't reset,when i readline until to end,i can read again,and however i do,the stream is the only.can't read twice.

so it's a serious problem ,that mean i can't count and read data at the same time.if i can't count i would't to declare a  array.

and then i use arraylist ,but it have problem too . how to write a cycle,how i judge whether the data had finished reading.

now i had solved the last problem ,

  FileStream s = File.OpenRead(fd.FileName.Replace(".xls",".csv"));
                CSVReader csv = new CSVReader(s);
                ArrayList tmpArray = new ArrayList();
                Boolean isend=false;
                while (!isend){
                    string[] temp_str = csv.GetCSVLine();
                    if (temp_str != null)
                    {
                        tmpArray.Add(temp_str);
                        MessageBox.Show(temp_str[1]);
                    }
                    else
                    {
                        isend = true;
                    }
                } 
                string[,] data=new string[tmpArray.Count,4];
                for(int i=0;i<data.GetLength(0);i++){
                    string []temp=(string[])tmpArray[i];
                    for (int j = 0; j < 4; j++) {
                        data[i, j] = temp[j];
                    }
                }
                dataGridView1.DataSource = ArrayToDataTable.Convert(new string[]{"学号","姓名","班级","卡号"},data);


there is a new problem ,which is Chinese garbled .

haha ,i almost forgot the jsp teacher had tought me.

so easy by use this code

 CSVReader csv = new CSVReader(s, Encoding.GetEncoding("gb2312"));











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值