Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This should work: (from <a href="http://www.hotblue.com/article0000.aspx?a=0006" rel="nofollow noreferrer">http://www.hotblue.com/article0000.aspx?a=0006</a>)</p> <p>just replace the comma part with:</p> <pre><code>if ((postdata || !quoted) &amp;&amp; (c == ',' || c == '\t')) </code></pre> <p>to make it tab delimited.</p> <pre><code>using System.Data; using System.IO; using System.Text.RegularExpressions; public DataTable ParseCSV(string inputString) { DataTable dt=new DataTable(); // declare the Regular Expression that will match versus the input string Regex re=new Regex("((?&lt;field&gt;[^\",\\r\\n]+)|\"(?&lt;field&gt;([^\"]|\"\")+)\")(,|(?&lt;rowbreak&gt;\\r\\n|\\n|$))"); ArrayList colArray=new ArrayList(); ArrayList rowArray=new ArrayList(); int colCount=0; int maxColCount=0; string rowbreak=""; string field=""; MatchCollection mc=re.Matches(inputString); foreach(Match m in mc) { // retrieve the field and replace two double-quotes with a single double-quote field=m.Result("${field}").Replace("\"\"","\""); rowbreak=m.Result("${rowbreak}"); if (field.Length &gt; 0) { colArray.Add(field); colCount++; } if (rowbreak.Length &gt; 0) { // add the column array to the row Array List rowArray.Add(colArray.ToArray()); // create a new Array List to hold the field values colArray=new ArrayList(); if (colCount &gt; maxColCount) maxColCount=colCount; colCount=0; } } if (rowbreak.Length == 0) { // this is executed when the last line doesn't // end with a line break rowArray.Add(colArray.ToArray()); if (colCount &gt; maxColCount) maxColCount=colCount; } // create the columns for the table for(int i=0; i &lt; maxColCount; i++) dt.Columns.Add(String.Format("col{0:000}",i)); // convert the row Array List into an Array object for easier access Array ra=rowArray.ToArray(); for(int i=0; i &lt; ra.Length; i++) { // create a new DataRow DataRow dr=dt.NewRow(); // convert the column Array List into an Array object for easier access Array ca=(Array)(ra.GetValue(i)); // add each field into the new DataRow for(int j=0; j &lt; ca.Length; j++) dr[j]=ca.GetValue(j); // add the new DataRow to the DataTable dt.Rows.Add(dr); } // in case no data was parsed, create a single column if (dt.Columns.Count == 0) dt.Columns.Add("NoData"); return dt; } </code></pre> <p>Now that we have a parser for converting a string into a DataTable, all we need now is a function that will read the content from a CSV file and pass it to our ParseCSV function:</p> <pre><code>public DataTable ParseCSVFile(string path) { string inputString=""; // check that the file exists before opening it if (File.Exists(path)) { StreamReader sr = new StreamReader(path); inputString = sr.ReadToEnd(); sr.Close(); } return ParseCSV(inputString); } </code></pre> <p>And now you can easily fill a DataGrid with data coming off the CSV file:</p> <pre><code>protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load(object sender, System.EventArgs e) { // call the parser DataTable dt=ParseCSVFile(Server.MapPath("./demo.csv")); // bind the resulting DataTable to a DataGrid Web Control DataGrid1.DataSource=dt; DataGrid1.DataBind(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload