Révision 2088
| tmp/org.txm.libs.office/src/org/txm/libs/office/ReadODS.java (revision 2088) | ||
|---|---|---|
| 87 | 87 |
ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>(); |
| 88 | 88 |
|
| 89 | 89 |
//int rowCount = table.getRowCount(); |
| 90 |
|
|
| 91 |
// findout maximum line length |
|
| 92 |
int sMax = 0; |
|
| 90 | 93 |
for (Row row : table.getRowList()) {
|
| 94 |
TableTableRowElement elem = row.getOdfElement(); |
|
| 95 |
NodeList children = elem.getChildNodes(); |
|
| 96 |
int s = children.getLength(); //FIXME row.getCellCount() takes too much time |
|
| 97 |
if (sMax < s) sMax = s; |
|
| 98 |
} |
|
| 99 |
for (Row row : table.getRowList()) {
|
|
| 91 | 100 |
ArrayList<String> dataline = new ArrayList<>(); |
| 92 | 101 |
data.add(dataline); |
| 93 | 102 |
//Row row = table.getRowByIndex(it); |
| 94 | 103 |
//int colCount = row.getCellCount(); |
| 95 |
TableTableRowElement elem = row.getOdfElement(); |
|
| 96 |
NodeList children = elem.getChildNodes(); |
|
| 97 |
int s = children.getLength(); //FIXME row.getCellCount() takes too much time |
|
| 98 |
NodeList elems = elem.getElementsByTagName("table:table-cell");
|
|
| 99 |
int size = elems.getLength(); |
|
| 100 |
for (int j = 0 ; j < size ; j++) {
|
|
| 104 |
//TableTableRowElement elem = row.getOdfElement(); |
|
| 105 |
//NodeList children = elem.getChildNodes(); |
|
| 106 |
//int s = children.getLength(); //FIXME row.getCellCount() takes too much time |
|
| 107 |
//System.out.println("line size="+s);
|
|
| 108 |
//NodeList elems = elem.getElementsByTagName("table:table-cell");
|
|
| 109 |
// int size = elems.getLength(); |
|
| 110 |
for (int j = 0 ; j < sMax ; j++) {
|
|
| 101 | 111 |
String cell = row.getCellByIndex(j).getDisplayText(); |
| 102 | 112 |
if (cell == null) {
|
| 103 | 113 |
dataline.add("");
|
| 104 | 114 |
} else {
|
| 105 |
dataline.add(cell); |
|
| 115 |
dataline.add(cell.replaceAll("\n", ";").trim());
|
|
| 106 | 116 |
} |
| 107 | 117 |
} |
| 108 | 118 |
} |
| ... | ... | |
| 111 | 121 |
} |
| 112 | 122 |
|
| 113 | 123 |
public static void main(String[] args) throws Exception {
|
| 114 |
File file = new File("/home/mdecorde/TXM/scripts/macro/org/txm/macro/atelier_txm_participants190327.ods");
|
|
| 115 |
System.out.println(toTable(file, "metadata")); |
|
| 124 |
File file = new File("/home/mdecorde/xml/ruscorpora1m-test/metadata.ods");
|
|
| 125 |
ArrayList<ArrayList<String>> table = toTable(file, "metadata"); |
|
| 126 |
for (ArrayList<String> line : table) {
|
|
| 127 |
System.out.print(line.get(0)+", "); |
|
| 128 |
} |
|
| 116 | 129 |
//Metadatas m = new Metadatas(file, ) |
| 117 | 130 |
} |
| 118 | 131 |
} |
| tmp/org.txm.libs.msoffice/src/org/txm/libs/msoffice/ReadExcel.java (revision 2088) | ||
|---|---|---|
| 4 | 4 |
import java.util.ArrayList; |
| 5 | 5 |
|
| 6 | 6 |
import org.apache.poi.ss.usermodel.Cell; |
| 7 |
import org.apache.poi.ss.usermodel.CellType; |
|
| 7 | 8 |
import org.apache.poi.ss.usermodel.Row; |
| 8 | 9 |
import org.apache.poi.ss.usermodel.Sheet; |
| 9 | 10 |
import org.apache.poi.ss.usermodel.Workbook; |
| ... | ... | |
| 15 | 16 |
* |
| 16 | 17 |
* @param inputFile |
| 17 | 18 |
* @param sheetName |
| 18 |
* @return list of lines (line = list of cells) |
|
| 19 |
* @return list of lines (line = list of cells) with values converted to String
|
|
| 19 | 20 |
*/ |
| 20 | 21 |
public static ArrayList<ArrayList<String>> toTable(File inputFile, String sheetName) {
|
| 21 | 22 |
|
| ... | ... | |
| 27 | 28 |
} |
| 28 | 29 |
|
| 29 | 30 |
try {
|
| 30 |
|
|
| 31 |
|
|
| 32 | 31 |
Workbook wb = WorkbookFactory.create(inputFile); |
| 33 | 32 |
Sheet ws; |
| 34 | 33 |
if (sheetName == null || sheetName.length() == 0) {
|
| ... | ... | |
| 63 | 62 |
for (int colIndex = 0 ; colIndex < colMax ; colIndex++) {
|
| 64 | 63 |
Cell cell = row.getCell(colIndex); |
| 65 | 64 |
if (cell != null) {
|
| 66 |
String value = cell.getStringCellValue().replaceAll("\n", ";").trim();
|
|
| 65 |
String value = cellToString(cell).replaceAll("\n", ";").trim();
|
|
| 67 | 66 |
dataLine.add(value); |
| 68 | 67 |
} else {
|
| 69 | 68 |
dataLine.add("");
|
| 70 | 69 |
} |
| 71 | 70 |
} |
| 72 | 71 |
} |
| 73 |
|
|
| 74 | 72 |
} catch (Exception e) {
|
| 75 | 73 |
System.out.println("** Excel2XML: unable to read input file. Aborting.");
|
| 76 | 74 |
System.out.println( e.getLocalizedMessage()); |
| ... | ... | |
| 80 | 78 |
|
| 81 | 79 |
return data; |
| 82 | 80 |
} |
| 83 |
|
|
| 81 |
|
|
| 82 |
/** |
|
| 83 |
* |
|
| 84 |
* @param cell |
|
| 85 |
* @return always a String starting with '#' if an error occurs |
|
| 86 |
*/ |
|
| 87 |
public static String cellToString(Cell cell) {
|
|
| 88 |
switch (cell.getCellTypeEnum()) {
|
|
| 89 |
case FORMULA: |
|
| 90 |
return "#formulla"; |
|
| 91 |
case NUMERIC: |
|
| 92 |
return ""+cell.getNumericCellValue(); |
|
| 93 |
case STRING: |
|
| 94 |
return cell.getStringCellValue(); |
|
| 95 |
case BLANK: |
|
| 96 |
return ""; |
|
| 97 |
case BOOLEAN: |
|
| 98 |
return ""+cell.getBooleanCellValue(); |
|
| 99 |
case ERROR: |
|
| 100 |
return "#error"; |
|
| 101 |
default: |
|
| 102 |
return "#typeerror"; |
|
| 103 |
} |
|
| 104 |
|
|
| 105 |
} |
|
| 106 |
|
|
| 84 | 107 |
public static void main(String[] args) {
|
| 85 |
ArrayList<ArrayList<String>> data = toTable(new File("/home/mdecorde/xml/metadata.xlsx"), null);
|
|
| 108 |
ArrayList<ArrayList<String>> data = toTable(new File("/home/mdecorde/xml/ruscorpora1m-test/metadata.xlsx"), null);
|
|
| 86 | 109 |
if (data.size() == 0) {
|
| 87 | 110 |
System.out.println("no data.");
|
| 88 | 111 |
} else {
|
| tmp/org.txm.libs.msoffice/META-INF/MANIFEST.MF (revision 2088) | ||
|---|---|---|
| 19 | 19 |
lib/poi-scratchpad-3.17.jar, |
| 20 | 20 |
lib/xmlbeans-2.6.0.jar, |
| 21 | 21 |
. |
| 22 |
Export-Package: org.txm.libs.msoffice |
|
Formats disponibles : Unified diff