Posted by: piman007 | 31-10-2008

CSV field format

สวัสดีครับ วันนี้ผมมีปัญหาใหม่เข้ามานำเสนอ เป็นเรื่องเกี่ยวกับการทำ CSV File ปัญหาที่พบก็คือ
 
1.ข้อมูลมี , ปนอยู่ใน content   ให้แก้โดยใช้ "(double quote) เปิดปิดข้อความ เช่น  ….,"THB","2,660,000.00",….
2.Excel แปลง Format เอง เราต้องการไม่ให้แปลง format ข้อมูลดังกล่าว ให้ใส่ = (เครื่องหมายเท่ากับ) นำหน้า "ข้อความ"  เช่น "1-01" เครื่องจะแปลงเป็น 01-Jan  ให้เปลี่ยนเป็น ="1-01"
3.แสดงเลขศูนย์นำหน้า โดยไม่มีการตัดศูนย์ออก เช่น  "0001234" ให้ใส่เครื่องหมายเท่ากับเหมือนข้อ 2   ตัวอย่าง  ="0001234"
 
ชอให้สนุกกับ CSV ครับ
 
 
 
The CSV ("Comma Separated Values") file format is often used to exchange data between differently similar applications. The CSV file format is useable by KSpread, OpenOffice Calc and Microsoft Execl. CSV files have become somewhat obslete due to XML data exchange possiblilites (like SOAP).

The CSV Format

  • Each record is one line – Line separator may be LF (0x0A) or CRLF (0x0D0A), a line seperator may also be embedded in the data (making a record more than one line but still acceptable).
  • Fields are separated with commas. – Duh.
  • Leading and trailing whitespace is ignored – Unless the field is delimited with double-quotes in that case the whitespace is preserved.
  • Embedded commas – Field must be delimited with double-quotes.
  • Embedded double-quotes – Embedded double-quote characters must be doubled, and the field must be delimited with double-quotes.
  • Embedded line-breaks – Fields must be surounded by double-quotes.
  • Always Delimiting – Fields may always be delimited with double quotes, the delimiters will be parsed and discarded by the reading applications.

CSV Files and Leading Zeros on Numeric Fields

Sometimes leading zero values are required in a data set and while the leading zeros are present in the data they are not displayed. In some software it’s possible to force strict interpretation of the CSV field value with a leading = (equal) symbol.

This may chop the leading zero on some softwares, even if quoted.

0306703,0035866,NO_ACTION,06/19/2006
0086003,"0005866",UPDATED,06/19/2006

This incantation may convince that software to keep the leading zero.

="0306703",="0035866",NO_ACTION,06/19/2006
="0086003",="0005866",UPDATED,06/19/2006

Acceptable CSV Mime Types

Sadly there is no definitive standard for this, here is a collection of types we’ve seen in use.

  • application/octet-stream
  • text/comma-separated-values – this is best
  • text/csv

CSV Examples

Here are some examples that demonstrate the rules above. Each sample describes the data and how the reading application should interpret it.

Standard Line

This shows three fields, each with simple data.

Edoceo, Seattle, WA

Whitespace

The first field should be interpreted by reading applications as [space]Edoceo[comma][space]Inc.[space]. Whitespace also could include line breaks.

" Edoceo, Inc. ",Seattle,WA

Embedded Commas

The first field should be interpreted by reading applications as Edoceo[comma][space]Inc.

"Edoceo, Inc.",Seattle,WA


Responses

  1. ได้ประโยชน์มากครับ


ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s

หมวดหมู่

%d bloggers like this: