Here i transferred the data from text file(using path of the file ) into LIST<>lidata..The count of list will be used for to run the transfer process.Because If the count is equal zero means there is no data in the list .For this i will show a simple message "liData File Appears to be Empty".If the count is >0 then have to check whether the data having the row headers or not .if the data having row headers the header fields are transfer into data table.Then the remaining data in the list<> will be transfer to Data table
Code behind:
Code behind:
DataTable DT = new DataTable(); String[] liData = File.ReadAllLines(textfilepath); if (liData .Length == 0) { throw new Exception("liData File Appears to be Empty"); } String[] headings = liData [0].Split('\t'); int index = 0; if (isRowOneHeader) { index = 0; for (int i = 0; i < headings.Length; i++) { headings[i] = headings[i].Replace(" ", "_"); DT.Columns.Add(headings[i], typeof(string)); } } else { for (int i = 0; i < headings.Length; i++) { DT.Columns.Add("col" + (i + 1).ToString(), typeof(string)); } } char[] delimiterChars = { '"' }; for (int i = index; i < liData .Length; i++) { if (liData [i] == "'") continue; else { DataRow row = DT.NewRow(); for (int j = 0; j < headings.Length; j++) { string[] rowData = liData [i].Split('\t'); if (rowData.Length <= j) continue; row[j] = rowData[j].Trim(delimiterChars); if (liData [i] != "'" && liData [i] != "") { string s = row[j].ToString(); s = s.Replace("'", ""); row[j] = s.Replace(",", ""); } } DT.Rows.Add(row); } } return DT; }
No comments:
Post a Comment