asp.net讀取CSV
用Excel導了兩天數據,各種問題,折磨客戶也折磨了自己,以前沒發現的問題一下子都暴露出來了
特意收集兩篇Excel跟CSV讀取相關的兩篇文章
asp.net讀取excel文件,將excel文件先上傳,在讀取,最後刪除。
但有一個問題,那就是excel文件夾需要有讀寫的權限,IIS需要為asp.net用戶開通權限的。
而且excel文件本身存在安全隱患,那就是它可以運行vba程序。所以從安全角度考慮,上傳excel是個不好的方法。
今天介紹另一種方法,讀取CVS文件。
Step1.拖一個fileupload和button控件。
Step2.雙擊button,在button事件中寫下列代碼。
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUploadCVS.HasFile)
{
if (System.IO.Path.GetExtension(FileUploadCVS.FileName) == ".csv")
{
DataTable dt = GetdataFromCVS(FileUploadCVS);
}
}
}
Step3. 寫讀取CVS文件的函數。
public static DataTable GetdataFromCVS(FileUpload fileload)
{
DataTable dt = newDataTable();
StreamReader sr = newStreamReader(fileload.PostedFile .InputStream);
string strTitle = sr.ReadLine();
string[] strColumTitle = strTitle.Split(‘,‘); //CVS 文件默認以逗號隔開
for (int i = 0; i < strColumTitle.Length; i++)
{
dt.Columns.Add(strColumTitle[i]);
}
while (!sr.EndOfStream)
{
string strTest = sr.ReadLine();
string[] strTestAttribute = strTest.Split(‘,‘);
DataRow dr = dt.NewRow();
for (int i = 0; i < strColumTitle.Length; i++)
{
dr[strColumTitle[i]] = strTestAttribute[i];
}
dt.Rows.Add(dr);
}
return dt;
}
記得加上System.IO 命名空間。StreamReader 是在這個namespace下的。很多跟輸入輸出相關的累都在這個命名空間下
經常用到csv文件,一直用odbc進行讀取,但是在unicode編碼的時候讀取不正確,有時候就算是ANSI編碼,如一列數據混編,讀取也不正確。不清楚是不是個人電腦的問題。只好自己寫個來實現簡單的讀取,解析含","及"""號CSV文件。
更新1:使用中發現有些軟件生存csv文件時,所有數據默認帶有"",以前代碼處理帶引號空字段數據不正確。
代碼如下,默認用UTF8編碼,一次性讀取整個CSV文件,若誰試用了此段代碼,有問題請反饋給我,謝謝。
/// <summary>
/// 讀取csv文件到DataTable
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
static private DataTable ReadCsv(string filepath)
{
DataTable dt = new DataTable("NewTable");
DataRow row;
string[] lines = File.ReadAllLines(filepath, Encoding.UTF8);
string[] head = lines[0].Split(‘,‘);
int cnt = head.Length;
for (int i = 0; i < cnt; i++)
{
dt.Columns.Add(head[i]);
}
for (int i = 0; i < lines.Length; i++)
{
lines[i].Trim();
if ((string.IsNullOrWhiteSpace(lines[i])))
{
continue;
}
try
{
row = dt.NewRow();
row.ItemArray = GetRow(lines[i], cnt);
dt.Rows.Add(row);
}
catch { }
}
return dt;
}
/// <summary>
/// 解析字符串 獲取 該行的數據 已經處理,及"號
/// </summary>
/// <param name="line">該行的內容</param>
/// <param name="cnt">總的條目數</param>
/// <returns></returns>
static private string[] GetRow(string line, int cnt)
{
//line = line.Replace("\"\"", "\""); //若空數據加引號替換不正確
string[] strs = line.Split(‘,‘);
if (strs.Length == cnt)
{
return RemoveQuotes(strs);
}
List<string> list = new List<string>();
int n = 0, begin = 0;
bool flag = false;
for (int i = 0; i < strs.Length; i++)
{
//沒有引號 或者 中間有引號 直接添加
if (strs[i].IndexOf("\"") == -1
|| (flag == false && strs[i][0] != ‘\"‘))
{
list.Add(strs[i]);
continue;
}
//其實有引號,但該段沒有,號,直接添加
n = 0;
foreach (char ch in strs[i])
{
if (ch == ‘\"‘)
{
n++;
}
}
if (n % 2 == 0)
{
list.Add(strs[i]);
continue;
}
//該段有引號 有 ,號,下一段增加後添加
flag = true;
begin = i;
i++;
for (i = begin + 1; i < strs.Length; i++)
{
foreach (char ch in strs[i])
{
if (ch == ‘\"‘)
{
n++;
}
}
if (strs[i][strs[i].Length - 1] == ‘\"‘ && n % 2 == 0)
{
StringBuilder sb = new StringBuilder();
for (; begin <= i; begin++)
{
sb.Append(strs[begin]);
if (begin != i)
{
sb.Append(",");
}
}
list.Add(sb.ToString());
break;
}
}
}
return RemoveQuotes(list.ToArray());
}
/// <summary>
/// 將解析的數據 去除多余的引號
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
static string[] RemoveQuotes(string[] strs)
{
for (int i = 0; i < strs.Length; i++)
{
//若該項數據為空 但csv文件中加上雙引號
if (strs[i] == "\"\"")
{
strs[i] = "";
continue;
}
//若該項數據頭和尾加上引號
if (strs[i].Length > 2 && strs[i][0] == ‘\"‘ && strs[i][strs[i].Length - 1] == ‘\"‘)
{
strs[i] = strs[i].Substring(1, strs[i].Length - 2);
}
//若該項數據中間有引號
strs[i] = strs[i].Replace("\"\"", "\"");
}
return strs;
}
asp.net讀取CSV