[Original] Export CSV file, special character processing.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.