日期:2014-05-17 浏览次数:20829 次
string text = @"年份/项目 2011/12 2010/12 2009/12 2008/12 2007/12 盈利(百万) 130,446 102,311 45,249 44,398 149,276 盈利增长(%) 27.50 126.11 1.92 -70.26 21.64"; DataTable dt = new DataTable(); string pattern_column = @"(?<=^|\s+?)[\u4e00-\u9fa5()%/]+(?=\s+)"; foreach (Match m in Regex.Matches(text, pattern_column)) { string column = m.Value; dt.Columns.Add(new DataColumn(column,typeof(string))); } int rows = (Regex.Matches(text, @"\S+?(?=\s+|$)").Count - dt.Columns.Count) / dt.Columns.Count; for (int i = 0; i < rows; i++) { DataRow dr = dt.NewRow(); foreach (DataColumn dc in dt.Columns) { string temp_name = Regex.Replace(dc.ColumnName,@"(?=[().])","\\"); string pattern_temp = @"(?<=" + temp_name + @"\s+?(\S+?\s+?){" + i + @"})\S+"; dr[dc.ColumnName] = Regex.Match(text,pattern_temp).Value; } dt.Rows.Add(dr); } this.dataGridView1.DataSource = dt;