Read .csv File add to dataTable
Create a CSVReader.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Collections;
public class CSVReader
{
private Stream objStream;
private StreamReader objReader;
#region CSVFileReader
// CSV File Read
public CSVReader(Stream filestream) : this(filestream, null) { }
public CSVReader(Stream filestream, Encoding enc)
{
try
{
this.objStream = filestream;
if (!filestream.CanRead)
{
return;
}
objReader = (enc != null) ? new StreamReader(filestream, enc) : new StreamReader(filestream);
}
catch(Exception ex)
{
AppLogger.LogError("CSVReader" + ex.Message, AppLogger.Severity.Error);
}
}
public string[] GetCSVLine()
{
string data = objReader.ReadLine();
data = data.Replace("\t", "\",\"");
if (!data.StartsWith("\""))
{
data = "\"" + data + "\"";
}
if (data == null) return null;
if (data.Length == 0) return new string[0];
ArrayList result = new ArrayList();
ParseCSVData(result, data);
return (string[])result.ToArray(typeof(string));
}
private void ParseCSVData(ArrayList result, string data)
{
try
{
int position = -1;
while (position < data.Length)
result.Add(ParseCSVField(ref data, ref position));
}
catch(Exception ex)
{
AppLogger.LogError("ParseCSVData" + ex.Message, AppLogger.Severity.Error);
}
}
private string ParseCSVField(ref string data, ref int StartSeperatorPos)
{
if (StartSeperatorPos == data.Length - 1)
{
StartSeperatorPos++;
return "";
}
int fromPos = StartSeperatorPos + 1;
if (data[fromPos] == '"')
{
int nextSingleQuote = GetSingleQuote(data, fromPos + 1);
int lines = 1;
while (nextSingleQuote == -1)
{
data = data + "\n" + objReader.ReadLine();
nextSingleQuote = GetSingleQuote(data, fromPos + 1);
lines++;
if (lines > 20)
throw new Exception("lines overflow: " + data);
}
StartSeperatorPos = nextSingleQuote + 1;
string tempString = data.Substring(fromPos + 1, nextSingleQuote - fromPos - 1);
tempString = tempString.Replace("'", "''");
return tempString.Replace("\"\"", "\"");
}
int nextComma = data.IndexOf(',', fromPos);
if (nextComma == -1)
{
StartSeperatorPos = data.Length;
return data.Substring(fromPos);
}
else
{
StartSeperatorPos = nextComma;
return data.Substring(fromPos, nextComma - fromPos);
}
}
private int GetSingleQuote(string data, int SFrom)
{
try
{
int i = SFrom - 1;
while (++i < data.Length)
if (data[i] == '"')
{
if (i < data.Length - 1 && data[i + 1] == '"')
{
i++;
continue;
}
else
{
return i;
}
}
}
catch(Exception ex)
{
AppLogger.LogError("GetSingleQuote" + ex.Message, AppLogger.Severity.Error);
}
return -1;
}
#endregion
#region destructor
~CSVReader()
{}
#endregion
}
DataTable dtUser = new DataTable();
int[] iColumns = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int iUpdate = 0;
try
{
//fexport.PostedFile.InputStream --> Directly reading .csv file from .net file uploader.
CSVReader objreader = new CSVReader(fexport.PostedFile.InputStream);
string[] strheaders = objreader.GetCSVLine();
foreach (string strHeader in strheaders)
{
dtUser.Columns.Add(strHeader);
}
string[] struserData;
while ((struserData = objreader.GetCSVLine()) != null)
dtUser.Rows.Add(struserData);
}
catch (Exception exe)
{
lblMsg.Text = "Check the CSV Data Sheet Format.";
AppLogger.LogError("csv sheet DataRead()QNews " + exe.Message, AppLogger.Severity.Error);
}
ஏஞ்சல்
என்னை பற்றி
Google reader
Categories
- .Net (9)
- .Net Crystal Report (1)
- .Net GridView (3)
- Articles (5)
- Log File .Net (1)
- Sharepoint (2)
- Sitefinity 3.7 (3)
- SQL (5)
- Technical Question and Ans (3)
- Validation (1)
- XML (2)
Blog Archive
Tuesday, October 13, 2009
.Csv File Reader
When hearts listen
angel sing.If you seek an angel with an open heart...
You shall always find loved one