第一次在博客园发博客,直接上代码 大家都懂得!
#region Scv格式文件读取和生成 /// <summary> /// 对读取到的cvs单独一行内容进行处理,去掉Csv格式,返回常规字符串,每项之间用特殊字符“^”隔开 /// </summary> /// <param name="str"></param> /// <returns></returns> private static string DealCode(string str) { string s = ""; int k = 1; if (str.Length == 0) return ""; str = str.Replace("^", ""); for (int i = 0; i < str.Length; i++) { switch (str.Substring(i, 1)) { case "\"": s += str.Substring(i, 1); k++; break; case ",": if (k % 2 == 0) s += str.Substring(i, 1); else s += "^"; break; default: s += str.Substring(i, 1); break; } } return s; } /// <summary> /// 对单引号和双引号处理 /// </summary> /// <param name="tmp"></param> /// <returns></returns> private static string[] DealCode2(string[] tmp) { string[] tmps = new string[tmp.Length]; for (int i = 0; i < tmp.Length; i++) { string temp = tmp[i].Replace("\"\"", "^"); temp = temp.Replace("\"", ""); temp = temp.Replace("^", "\""); temp = temp.Replace("''", "∵"); temp = temp.Replace("∵", "'"); tmps[i] = temp; } return tmps; } /// <summary> /// 获取Scv文件 /// </summary> /// <param name="reader">System.IO.StreamReader流</param> /// <returns>返回List<string[]>数组</returns> public static List<string[]> GetScv(System.IO.StreamReader reader) { List<string[]> list = new List<string[]>(); string strline=""; while ((strline = reader.ReadLine()) != null)//每次单独抽取Csv一行的内容来处理 { strline = DealCode(strline).Replace("'", "''");//调用函数处理每一行内容 string[] strs = strline.Split(new char[] { '^' });//对处理后的内容进行特殊字符“^”分隔就得到了常规的字符数组了,你就可以进行其他用途了。 strs = DealCode2(strs); list.Add(strs); } return list; } //输出Csv格式数据 /// <summary> /// 输出Csv格式文件 /// </summary> /// <param name="dt"></param> public static void CreateCsv(DataTable dt) { StringWriter sw = new StringWriter(); string column = ""; foreach (DataColumn item in dt.Columns) { column += item + ","; } column = column.Trim(','); sw.WriteLine(column);//添加行s foreach (DataRow row in dt.Rows) { string rows = ""; foreach (DataColumn col in dt.Columns) { rows += row[col] + ",";//添加列 } rows = rows.Trim(','); sw.WriteLine(rows); } sw.Close(); System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=DomeCsv" + DateTime.Now.ToShortTimeString() + ".csv"); System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel"; System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); System.Web.HttpContext.Current.Response.Write(sw); System.Web.HttpContext.Current.Response.End(); } #endregion