asp怎么读取CSV数据文件

如题所述

  /// <summary>
        /// è¯»å–csv文件到DataTable
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        static private DataTable ReadCsv(string filepath)
        {
            DataTable dt = new DataTable("NewTable");
            DataRow row;

            string[] lines = File.ReadAllLines(filepath, Encoding.UTF8);
            string[] head = lines[0].Split(',');
            int cnt = head.Length;
            for (int i = 0; i < cnt; i++)
            {
                dt.Columns.Add(head[i]);
            }
            for (int i = 0; i < lines.Length; i++)
            {
                lines[i].Trim();
                if ((string.IsNullOrWhiteSpace(lines[i])))
                {
                    continue;
                }
                try
                {
                    row = dt.NewRow();
                    row.ItemArray = GetRow(lines[i], cnt);
                    dt.Rows.Add(row);
                }
                catch { }
            }
            return dt;
        }
        /// <summary>
        /// è§£æžå­—符串 èŽ·å– è¯¥è¡Œçš„数据 å·²ç»å¤„理,及"号
        /// </summary>
        /// <param name="line">该行的内容</param>
        /// <param name="cnt">总的条目数</param>
        /// <returns></returns>
        static private string[] GetRow(string line, int cnt)
        {
            //line = line.Replace("\"\"", "\""); //若空数据加引号替换不正确
            string[] strs = line.Split(',');
            if (strs.Length == cnt)
            {
                return RemoveQuotes(strs);
            }
            List<string> list = new List<string>();
            int n = 0, begin = 0;
            bool flag = false;

            for (int i = 0; i < strs.Length; i++)
            {

                //没有引号 æˆ–者 ä¸­é—´æœ‰å¼•å· ç›´æŽ¥æ·»åŠ 
                if (strs[i].IndexOf("\"") == -1
                    || (flag == false && strs[i][0] != '\"'))
                {
                    list.Add(strs[i]);
                    continue;
                }
                //其实有引号,但该段没有,号,直接添加
                n = 0;
                foreach (char ch in strs[i])
                {
                    if (ch == '\"')
                    {
                        n++;
                    }
                }
                if (n % 2 == 0)
                {
                    list.Add(strs[i]);
                    continue;
                }
                //该段有引号 æœ‰ ,号,下一段增加后添加
                flag = true;
                begin = i;
                i++;
                for (i = begin + 1; i < strs.Length; i++)
                {
                    foreach (char ch in strs[i])
                    {
                        if (ch == '\"')
                        {
                            n++;
                        }
                    }
                    if (strs[i][strs[i].Length - 1] == '\"' && n % 2 == 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        for (; begin <= i; begin++)
                        {
                            sb.Append(strs[begin]);
                            if (begin != i)
                            {
                                sb.Append(",");
                            }
                        }
                        list.Add(sb.ToString());
                        break;
                    }
                }
            }
            return RemoveQuotes(list.ToArray());
        }
        /// <summary>
        /// å°†è§£æžçš„数据 åŽ»é™¤å¤šä½™çš„引号
        /// </summary>
        /// <param name="strs"></param>
        /// <returns></returns>
        static string[] RemoveQuotes(string[] strs)
        {
            for (int i = 0; i < strs.Length; i++)
            {
                //若该项数据为空 ä½†csv文件中加上双引号
                if (strs[i] == "\"\"")
                {
                    strs[i] = "";
                    continue;
                }
                //若该项数据头和尾加上引号
                if (strs[i].Length > 2 && strs[i][0] == '\"' && strs[i][strs[i].Length - 1] == '\"')
                {
                    strs[i] = strs[i].Substring(1, strs[i].Length - 2);
                }
                //若该项数据中间有引号
                strs[i] = strs[i].Replace("\"\"", "\"");
            }
            return strs;
        }
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-08-05
读取CSV和读取XLS道理是一样的,可以参照读取XSL的方法!
在百度里面搜索一下:ASP读取EXCEL,即可知道答案!追问

asp代码读取EXCEL文件,我可以实现,但是读取CSV文件就失败,不知道怎么回事?

追答

"
response.Write""
for table = 0 to num_imported -1

response.Write""
response.Write split_text(count)
response.Write""
count=count+1
next
response.Write""
response.Write""

count=0

for tablea = 0 to (total_num_imported/ (num_imported)-1)
for table = 0 to num_imported -1

response.Write""
response.Write total_split_text(count)
response.Write""
count=count+1
next
response.Write""
next
response.Write""
%>
给你一段我以前写的程序,你看一下就会明白了。

本回答被提问者采纳
第2个回答  2011-08-05
直接导入不能读取么?
第3个回答  2011-08-05
可以用excel来读