| 
									
										
										
										
											2023-05-27 23:07:45 +00:00
										 |  |  | --- | 
					
						
							| 
									
										
										
										
											2023-06-25 09:36:58 +00:00
										 |  |  | sidebar_position: 5 | 
					
						
							| 
									
										
										
										
											2023-05-27 23:07:45 +00:00
										 |  |  | title: CSV and Text | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CSV is a common format for data interchange. Spreadsheet applications such as | 
					
						
							|  |  |  | Excel also support other delimiter-separated formats including "Text" (using a | 
					
						
							|  |  |  | tab character as the field separator). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The general write functions (`XLSX.write` and `XLSX.writeFile`) support `csv` | 
					
						
							|  |  |  | and `txt` (for CSV and tab-separated values respectively), but the specific | 
					
						
							|  |  |  | utility functions generate raw JS strings for further processing. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Live Demo
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | After choosing a file, the demo will print the data from each worksheet: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```jsx live | 
					
						
							|  |  |  | function SheetJSPreCSView() { | 
					
						
							|  |  |  |   const [__html, setHTML] = React.useState("Select a file"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const process = (ab) => { | 
					
						
							|  |  |  |     const wb = XLSX.read(ab); | 
					
						
							|  |  |  |     var res = ""; | 
					
						
							|  |  |  |     wb.SheetNames.forEach((n, idx) => { | 
					
						
							|  |  |  |       const ws = wb.Sheets[n]; | 
					
						
							|  |  |  |       res += `<b>Sheet #${idx+1} (${n})</b>\n`; | 
					
						
							|  |  |  |       res += XLSX.utils.sheet_to_csv(ws) + "\n\n"; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     setHTML(res); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   React.useEffect(() => { (async() => { | 
					
						
							| 
									
										
										
										
											2024-04-26 04:16:13 +00:00
										 |  |  |     const url = "https://docs.sheetjs.com/pres.numbers"; | 
					
						
							| 
									
										
										
										
											2023-05-27 23:07:45 +00:00
										 |  |  |     process(await (await fetch(url)).arrayBuffer()); | 
					
						
							|  |  |  |   })(); }, []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( <> | 
					
						
							|  |  |  |     <input type="file" onChange={async(e) => { | 
					
						
							|  |  |  |       process(await e.target.files[0].arrayBuffer()); | 
					
						
							|  |  |  |     }}/> | 
					
						
							|  |  |  |     <pre dangerouslySetInnerHTML={{ __html }}/> | 
					
						
							|  |  |  |   </> ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Delimiter-Separated Output
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | **Export worksheet data in CSV, TSV, or other delimiter-separated format** | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | var csv = XLSX.utils.sheet_to_csv(ws, opts); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | As an alternative to the `writeFile` CSV type, `XLSX.utils.sheet_to_csv` also | 
					
						
							|  |  |  | produces CSV output.  The function takes an options argument: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | | Option Name  |  Default | Description                                        | | 
					
						
							|  |  |  | | :----------- | :------: | :------------------------------------------------- | | 
					
						
							|  |  |  | |`FS`          |  `","`   | "Field Separator"  delimiter between fields        | | 
					
						
							|  |  |  | |`RS`          |  `"\n"`  | "Record Separator" delimiter between rows          | | 
					
						
							|  |  |  | |`dateNF`      |  FMT 14  | Use specified date format in string output         | | 
					
						
							|  |  |  | |`strip`       |  false   | Remove trailing field separators in each record ** | | 
					
						
							|  |  |  | |`blankrows`   |  true    | Include blank lines in the CSV output              | | 
					
						
							|  |  |  | |`skipHidden`  |  false   | Skips hidden rows/columns in the CSV output        | | 
					
						
							|  |  |  | |`forceQuotes` |  false   | Force quotes around fields                         | | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - `strip` will remove trailing commas from each line under default `FS/RS` | 
					
						
							|  |  |  | - `blankrows` must be set to `false` to skip blank lines. | 
					
						
							|  |  |  | - Fields containing the record or field separator will automatically be wrapped | 
					
						
							|  |  |  |   in double quotes; `forceQuotes` forces all cells to be wrapped in quotes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The following example shows `FS` and `RS` options: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```jsx live | 
					
						
							|  |  |  | function SheetJSCSVTest() { | 
					
						
							|  |  |  |   var ws = XLSX.utils.aoa_to_sheet([ | 
					
						
							|  |  |  |     ["S", "h", "e", "e", "t", "J", "S"], | 
					
						
							|  |  |  |     [  1,   2,    ,    ,   5,   6,   7], | 
					
						
							|  |  |  |     [  2,   3,    ,    ,   6,   7,   8], | 
					
						
							|  |  |  |     [  3,   4,    ,    ,   7,   8,   9], | 
					
						
							|  |  |  |     [  4,   5,   6,   7,   8,   9,   0] | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  |   return ( <pre> | 
					
						
							|  |  |  |     <b>Worksheet (as HTML)</b> | 
					
						
							|  |  |  |     <div dangerouslySetInnerHTML={{__html: XLSX.utils.sheet_to_html(ws)}}/> | 
					
						
							|  |  |  |     <b>XLSX.utils.sheet_to_csv(ws)</b><br/> | 
					
						
							|  |  |  |     {XLSX.utils.sheet_to_csv(ws)}<br/><br/> | 
					
						
							|  |  |  |     <b>XLSX.utils.sheet_to_csv(ws, {'{'} FS: "\t" {'}'})</b><br/> | 
					
						
							|  |  |  |     {XLSX.utils.sheet_to_csv(ws, { FS: "\t" })}<br/><br/> | 
					
						
							|  |  |  |     <b>XLSX.utils.sheet_to_csv(ws, {'{'} FS: ":", RS: "|" {'}'})</b><br/> | 
					
						
							|  |  |  |     {XLSX.utils.sheet_to_csv(ws, { FS: ":", RS: "|" })}<br/> | 
					
						
							|  |  |  |   </pre> ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### CSV Output
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | **Export worksheet data in "Comma-Separated Values" (CSV)** | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | var csv = XLSX.utils.sheet_to_csv(ws, opts); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `sheet_to_csv` uses the comma character as the field separator by default. This | 
					
						
							|  |  |  | utility function mirrors the `csv` book type in `XLSX.write` or `XLSX.writeFile`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `sheet_to_csv` always returns a JS string and always omits byte-order marks. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### UTF-16 Text Output
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | **Export worksheet data in "UTF-16 Text" or Tab-Separated Values (TSV)** | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | var txt = XLSX.utils.sheet_to_txt(ws, opts); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `sheet_to_txt` uses the tab character as the field separator. This utility | 
					
						
							|  |  |  | function matches the `txt` book type in `XLSX.write` or `XLSX.writeFile`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If encoding support is available, the output will be encoded in `CP1200` and the | 
					
						
							|  |  |  | UTF-16 BOM will be added. If encoding support is not available, the output will | 
					
						
							|  |  |  | be encoded as a standard `string`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `XLSX.utils.sheet_to_txt` takes the same arguments as `sheet_to_csv`. |