Révision 1334
tmp/org.txm.libs.office/src/org/txm/libs/office/ReadODS.java (revision 1334) | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.io.File; |
4 | 4 |
import java.util.ArrayList; |
5 |
import java.util.HashMap; |
|
6 |
import java.util.List; |
|
5 | 7 |
|
6 | 8 |
import org.odftoolkit.odfdom.dom.element.table.TableTableRowElement; |
7 | 9 |
import org.odftoolkit.simple.SpreadsheetDocument; |
... | ... | |
11 | 13 |
|
12 | 14 |
public class ReadODS { |
13 | 15 |
|
16 |
public static ArrayList<HashMap<String, String>> toHashMap(File inputFile, String sheetname) throws Exception { |
|
17 |
SpreadsheetDocument spreadsheet = SpreadsheetDocument.loadDocument(inputFile); |
|
18 |
|
|
19 |
// the first access to the document causes a very long process |
|
20 |
int sheetCount = spreadsheet.getSheetCount(); |
|
21 |
if (sheetCount < 1) { |
|
22 |
System.out.println("** ReadODS: no sheet found in file. Aborting."); |
|
23 |
return null; |
|
24 |
} |
|
25 |
|
|
26 |
Table table = null; |
|
27 |
if (sheetname != null) { |
|
28 |
table = spreadsheet.getSheetByName(sheetname); |
|
29 |
} |
|
30 |
if (table == null) { |
|
31 |
table = spreadsheet.getSheetByIndex(0); |
|
32 |
} |
|
33 |
|
|
34 |
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); |
|
35 |
|
|
36 |
List<Row> rows = table.getRowList(); |
|
37 |
ArrayList<String> cols = new ArrayList<String>(); |
|
38 |
for (int i = 0 ; i < 4 ; i++) { |
|
39 |
cols.add(rows.get(0).getCellByIndex(i).getStringValue()); |
|
40 |
} |
|
41 |
|
|
42 |
//int rowCount = table.getRowCount(); |
|
43 |
for (int iRow = 1 ; iRow < rows.size() ; iRow++) { |
|
44 |
Row row = rows.get(iRow); |
|
45 |
HashMap<String, String> dataline = new HashMap<String, String>(); |
|
46 |
data.add(dataline); |
|
47 |
|
|
48 |
for (int j = 0 ; j < 4 ; j++) { |
|
49 |
String cell = row.getCellByIndex(j).getDisplayText(); |
|
50 |
if (cell == null) { |
|
51 |
dataline.put(cols.get(j), ""); |
|
52 |
} else { |
|
53 |
dataline.put(cols.get(j), cell); |
|
54 |
} |
|
55 |
} |
|
56 |
} |
|
57 |
|
|
58 |
return data; |
|
59 |
} |
|
60 |
|
|
14 | 61 |
public static ArrayList<ArrayList<String>> toTable(File inputFile, String sheetname) throws Exception { |
15 | 62 |
|
16 | 63 |
SpreadsheetDocument spreadsheet = SpreadsheetDocument.loadDocument(inputFile); |
Formats disponibles : Unified diff