CSV file format
1、CSV
文件默认以英文逗号(,)做为列分隔符,换行符(\n)作为行分隔符。
2、CSV
默认认为由
""
括起来的内容是一个栏位,这时不管栏位内容里有除
"
之外字符的任何字符都可以按原来形式引用。
3、若字段内容里含有
",
这时只需将
"
替换成两个双引号
("")
即可。
CSV
会将字段里的两个双引号
""
显示成一个。
4. Apply char (9) to display as text (instance red font part).
Application examples
private void Exporttoscv (GridView GridView, String path)
{
Try
{
if (file.exists (path) && isfileinuse (path))
{
MessageBox.Show ("File is occupied, please close the file first!") ");
Return
}
StringBuilder sb = new StringBuilder ();
String strcols = String. Empty;
foreach (GridColumn col in GridView. Columns)
{
if (!col. Visible) continue;
Strcols + = Col. Caption;
Strcols + = ",";
}
Strcols = Strcols.remove (strcols.length-1, 1);
Sb. Appendline (Strcols);
for (int i = 0; i < GridView. RowCount; i++)
{
String Strrow = String. Empty;
foreach (GridColumn col in GridView. Columns)
{
if (!col. Visible) continue;
Apply char (9) to display as text
string strvalue = "\" "+ ((char) (9)). ToString () + GridView. Getrowcelldisplaytext (i, col) + "\" ";
Strrow + = strvalue;
Strrow + = ",";
}
Strrow = Strrow.remove (strrow.length-1, 1);
Sb. Appendline (Strrow);
}
using (StreamWriter SW = new StreamWriter (path, False, Encoding.GetEncoding ("GB2312")))
{
Sw. Write (sb.) ToString ());
Sw. Flush ();
}
}
catch (Exception ex)
{
MessageBox.Show (ex. Message);
}
}
private bool Isfileinuse (string fileName)
{
bool InUse = true;
FileStream fs = null;
Try
{
FS = new FileStream (FileName, FileMode.Open, FileAccess.Read, Fileshare.none);
InUse = false;
}
Catch
{
}
Finally
{
if (fs! = NULL)
Fs. Close ();
}
Return inuse;//true indicates being used, false not used
}
[Original] Export CSV file, special character processing.