|
| 1 | +<?xml version="1.0"?> |
| 2 | +<!-- |
| 3 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +contributor license agreements. See the NOTICE file distributed with |
| 5 | +this work for additional information regarding copyright ownership. |
| 6 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +(the "License"); you may not use this file except in compliance with |
| 8 | +the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +--> |
| 18 | +<document> |
| 19 | + <properties> |
| 20 | + <title>User Guide</title> |
| 21 | + <author email="dev@commons.apache.org">Commons Documentation Team</author> |
| 22 | + </properties> |
| 23 | +<body> |
| 24 | +<!-- ================================================== --> |
| 25 | +<section name="Parsing an Excel CSV File"> |
| 26 | + <p>To parse an Excel CSV file, write:</p> |
| 27 | + <source>Reader in = new FileReader("path/to/file.csv"); |
| 28 | +Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in); |
| 29 | +for (CSVRecord record : records) { |
| 30 | + String lastName = record.get("Last Name"); |
| 31 | + String firstName = record.get("First Name"); |
| 32 | +}</source> |
| 33 | +</section> |
| 34 | +<section name="Handling Byte Order Marks"> |
| 35 | + <p> |
| 36 | + To handle files that start with a Byte Order Mark (BOM) like some Excel CSV files, you need an extra step to deal with these optional bytes. |
| 37 | + You can use the |
| 38 | + <a href="https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/BOMInputStream.html">BOMInputStream</a> |
| 39 | + class from <a href="https://commons.apache.org/proper/commons-io/">Apache Commons IO</a> for example: |
| 40 | + </p> |
| 41 | + <source>final URL url = ...; |
| 42 | +final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8"); |
| 43 | +final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); |
| 44 | +try { |
| 45 | + for (final CSVRecord record : parser) { |
| 46 | + final String string = record.get("SomeColumn"); |
| 47 | + ... |
| 48 | + } |
| 49 | +} finally { |
| 50 | + parser.close(); |
| 51 | + reader.close(); |
| 52 | +}</source> |
| 53 | +</section> |
| 54 | +<!-- ================================================== --> |
| 55 | +</body> |
| 56 | +</document> |
0 commit comments