2010年2月2日 星期二

關於C#讀取CSV檔以及顯示中文亂碼

1.關於C#讀取CSV檔

2.以及顯示中文亂碼∼
using (StreamReader sr = new StreamReader(ytcsv))
變:using (StreamReader sr = new StreamReader(ytcsv,System.Text.Encoding.Default))

string[,] aryResult;
                using (StreamReader sr = new StreamReader(ytcsv,System.Text.Encoding.Default))
                {
                    //讀取文字檔
                    string txt = sr.ReadToEnd().Trim();
                    string[] aryStr = txt.Split('\n');//有幾"列"
                    int hight = aryStr.Length;
                    int width = aryStr[0].Split(',').Length;//有幾"行"
                    aryResult = new string[hight, width];//確定後宣告陣列大小
                    //帶值進去
                    for (int h = 0; h < hight; h++)
                    {
                        string[] arySplit = aryStr[h].Split(',');
                        for (int w = 0; w < width; w++)
                        {
                            aryResult[h, w] = arySplit[w];
                            this.Text = "H:" + hight + "-W:" + width;
                        }
                    }
                }

                //印出結果
                for (int h = 0; h <= aryResult.GetUpperBound(0); h++)
                {
                    for (int w = 0; w <= aryResult.GetUpperBound(1); w++)
                    {
                        //Console.WriteLine("ary[{0},{1}]='{2}'", h, w, aryResult[h, w]);
                    }
                    //列印開始!
                    /*
                    ※96 DPI 時, 1 Inch = 96 Pixels , 1 CM = 1/2.54 Inch = 96/2.54 Pixels = 37.7953 Pixels
                     Sol.1:動態產生控制項,整體一起列印。
                     Sol.2:使用迴圈連續列印。
                     */
                    lbl_cus.Text = aryResult[h, 0];
                    lbl_item.Text = aryResult[h, 1];
                    lbl_content.Text = aryResult[h, 2];
                    lbl_date.Text = aryResult[h, 3];
                    //Print
                }
 

--
看到、想到、說到、做到 
能夠填平大海的誓言,也比不上邁出一步的價值 
是以吾輩此生,再無任何誓言

沒有留言:

張貼留言