Révision 3043
tmp/org.txm.libs.msoffice/src/org/txm/libs/msoffice/ReadExcel.java (revision 3043) | ||
---|---|---|
23 | 23 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
24 | 24 |
|
25 | 25 |
public class ReadExcel { |
26 |
|
|
26 |
|
|
27 | 27 |
Workbook wb = null; |
28 |
|
|
28 |
|
|
29 | 29 |
Sheet ws = null; |
30 |
|
|
30 |
|
|
31 | 31 |
String[] header = null; |
32 |
|
|
32 |
|
|
33 | 33 |
HashMap<String, String> record = null; |
34 |
|
|
34 |
|
|
35 | 35 |
int iRow = 0; |
36 |
|
|
36 |
|
|
37 | 37 |
int nRows; |
38 |
|
|
38 |
|
|
39 | 39 |
private File tableFile; |
40 |
|
|
40 |
|
|
41 | 41 |
public ReadExcel(File tableFile, String sheetName) throws EncryptedDocumentException, InvalidFormatException, IOException { |
42 |
|
|
42 |
|
|
43 | 43 |
this.tableFile = tableFile; |
44 |
|
|
44 |
|
|
45 | 45 |
if (tableFile.exists() && tableFile.length() > 0) { |
46 | 46 |
wb = WorkbookFactory.create(tableFile); |
47 | 47 |
if (sheetName == null || sheetName.length() == 0) { |
... | ... | |
53 | 53 |
ws = wb.getSheetAt(0); |
54 | 54 |
} |
55 | 55 |
} |
56 |
|
|
56 |
|
|
57 | 57 |
nRows = ws.getPhysicalNumberOfRows(); |
58 | 58 |
} |
59 | 59 |
else { |
... | ... | |
61 | 61 |
ws = wb.createSheet(); |
62 | 62 |
} |
63 | 63 |
} |
64 |
|
|
64 |
|
|
65 | 65 |
/** |
66 | 66 |
* one rule (regex test) per column |
67 | 67 |
* all lines are processed |
... | ... | |
72 | 72 |
Row headers = ws.getRow(0); |
73 | 73 |
if (headers == null) return false; |
74 | 74 |
int colMax = headers.getLastCellNum(); |
75 |
|
|
75 |
|
|
76 | 76 |
ArrayList<Integer> columnIdxToTest = new ArrayList<>(); |
77 | 77 |
ArrayList<String> columnsTest = new ArrayList<>(); |
78 | 78 |
for (int colIndex = 0; colIndex < colMax; colIndex++) { |
... | ... | |
85 | 85 |
} |
86 | 86 |
} |
87 | 87 |
} |
88 |
|
|
88 |
|
|
89 | 89 |
if (columnIdxToTest.size() == 0) { |
90 | 90 |
System.out.println("Error: no column found with name=" + rules.keySet()); |
91 | 91 |
return false; |
92 | 92 |
} |
93 |
|
|
93 |
|
|
94 | 94 |
ArrayList<Integer> removed = new ArrayList<>(); |
95 | 95 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) { |
96 | 96 |
Row row = ws.getRow(rowIndex); |
97 | 97 |
if (row == null) continue; |
98 | 98 |
for (int i = 0; i < columnIdxToTest.size(); i++) { |
99 | 99 |
int colIndex = columnIdxToTest.get(i); |
100 |
|
|
100 |
|
|
101 | 101 |
Cell cell = row.getCell(colIndex); |
102 | 102 |
if (cell != null) { |
103 | 103 |
String value = cellToString(cell).trim(); |
... | ... | |
109 | 109 |
} |
110 | 110 |
} |
111 | 111 |
} |
112 |
|
|
112 |
|
|
113 | 113 |
// TODO very slow when there is a lot of lines |
114 | 114 |
for (int rowIndex : removed) { |
115 | 115 |
ws.shiftRows(rowIndex + 1, nRows, -1); |
116 | 116 |
nRows--; |
117 | 117 |
} |
118 |
|
|
118 |
|
|
119 | 119 |
return true; |
120 | 120 |
} |
121 |
|
|
121 |
|
|
122 | 122 |
/** |
123 | 123 |
* search and replace lines utils |
124 | 124 |
* |
... | ... | |
135 | 135 |
if (!extractTo(ws2, lineRules, columnsSelection)) { |
136 | 136 |
return false; |
137 | 137 |
} |
138 |
|
|
138 |
|
|
139 | 139 |
return true; |
140 | 140 |
} |
141 |
|
|
141 |
|
|
142 | 142 |
/** |
143 | 143 |
* extract lines and columns following rules. |
144 | 144 |
* |
... | ... | |
155 | 155 |
public boolean copyColumns(HashMap<String, String[]> columnsToCopy) throws EncryptedDocumentException, InvalidFormatException, IOException { |
156 | 156 |
Row headers = ws.getRow(0); |
157 | 157 |
if (headers == null) return false; |
158 |
|
|
158 |
|
|
159 | 159 |
int colMax = headers.getLastCellNum(); |
160 | 160 |
int nRows = ws.getPhysicalNumberOfRows(); |
161 |
|
|
161 |
|
|
162 | 162 |
HashMap<String, Integer> searchColumns = new HashMap<>(); |
163 | 163 |
for (int colIndex = 0; colIndex < colMax; colIndex++) { |
164 | 164 |
Cell cell = headers.getCell(colIndex); |
... | ... | |
169 | 169 |
} |
170 | 170 |
} |
171 | 171 |
} |
172 |
|
|
172 |
|
|
173 | 173 |
if (searchColumns.keySet().size() != columnsToCopy.keySet().size()) { |
174 | 174 |
System.out.println("Error: not all columns found=" + searchColumns.keySet() + " of " + columnsToCopy.keySet()); |
175 | 175 |
ArrayList<String> notFound = new ArrayList<>(columnsToCopy.keySet()); |
... | ... | |
177 | 177 |
System.out.println("NOT FOUND: " + notFound); |
178 | 178 |
return false; |
179 | 179 |
} |
180 |
|
|
180 |
|
|
181 | 181 |
System.out.println("N Rows to update: " + nRows); |
182 | 182 |
int nRowWritten = 0; |
183 |
|
|
183 |
|
|
184 | 184 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) { // update all rows, starting from the second row (first row is the header) |
185 | 185 |
Row row = ws.getRow(rowIndex); |
186 | 186 |
if (row == null) continue; |
187 |
|
|
187 |
|
|
188 | 188 |
int icol = 0; |
189 | 189 |
for (String col : columnsToCopy.keySet()) { |
190 | 190 |
for (String newcol : columnsToCopy.get(col)) { |
191 | 191 |
int colIndex = searchColumns.get(col); |
192 |
|
|
192 |
|
|
193 | 193 |
Cell cell = row.getCell(colIndex); |
194 | 194 |
if (cell != null) { |
195 |
|
|
195 |
|
|
196 | 196 |
Cell newCell = row.createCell(colMax + icol); |
197 | 197 |
if (rowIndex == 0) { |
198 | 198 |
newCell.setCellValue(newcol); // first column must be renamed |
... | ... | |
204 | 204 |
icol++; |
205 | 205 |
} |
206 | 206 |
} |
207 |
|
|
207 |
|
|
208 | 208 |
nRowWritten++; |
209 | 209 |
} |
210 |
|
|
210 |
|
|
211 | 211 |
System.out.println("" + nRowWritten + " rows updated."); |
212 |
|
|
212 |
|
|
213 | 213 |
return nRowWritten > 0; |
214 | 214 |
} |
215 |
|
|
215 |
|
|
216 | 216 |
public boolean formatDateColumns(List<String> columns, String dateformat) { |
217 | 217 |
try { |
218 | 218 |
java.text.SimpleDateFormat parser = new java.text.SimpleDateFormat(dateformat); |
... | ... | |
232 | 232 |
} |
233 | 233 |
} |
234 | 234 |
} |
235 |
|
|
235 |
|
|
236 | 236 |
if (searchColumns.keySet().size() != columns.size()) { |
237 | 237 |
System.out.println("Error: not all columns found=" + searchColumns.keySet() + " of " + columns); |
238 | 238 |
ArrayList<String> notFound = new ArrayList<>(columns); |
... | ... | |
242 | 242 |
} |
243 | 243 |
|
244 | 244 |
for (String column : columns) { |
245 |
|
|
245 |
|
|
246 | 246 |
int colIndex = searchColumns.get(column); |
247 | 247 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) { // update all rows, starting from the second row (first row is the header) |
248 | 248 |
Row row = ws.getRow(rowIndex); |
249 | 249 |
if (row == null) continue; |
250 |
|
|
250 |
|
|
251 | 251 |
Cell cell = row.getCell(colIndex); |
252 | 252 |
int icol = 0; // number of columns written for this row |
253 |
|
|
253 |
|
|
254 | 254 |
if (cell != null) { |
255 |
|
|
255 |
|
|
256 | 256 |
Cell newCell = null; |
257 | 257 |
if (rowIndex == 0) { // header line |
258 | 258 |
newCell = row.createCell(colMax + icol++); |
259 |
newCell.setCellValue(column+"-jour-semaine"); // first row must be renamed
|
|
259 |
newCell.setCellValue(column + "-jour-semaine"); // first row must be renamed
|
|
260 | 260 |
newCell = row.createCell(colMax + icol++); |
261 |
newCell.setCellValue(column+"-jour"); // first row must be renamed
|
|
261 |
newCell.setCellValue(column + "-jour"); // first row must be renamed
|
|
262 | 262 |
newCell = row.createCell(colMax + icol++); |
263 |
newCell.setCellValue(column+"-mois"); // first row must be renamed
|
|
263 |
newCell.setCellValue(column + "-mois"); // first row must be renamed
|
|
264 | 264 |
newCell = row.createCell(colMax + icol++); |
265 |
newCell.setCellValue(column+"-annee"); // first row must be renamed
|
|
265 |
newCell.setCellValue(column + "-annee"); // first row must be renamed
|
|
266 | 266 |
newCell = row.createCell(colMax + icol++); |
267 |
newCell.setCellValue(column+"-tri"); // first row must be renamed
|
|
267 |
newCell.setCellValue(column + "-tri"); // first row must be renamed
|
|
268 | 268 |
} |
269 | 269 |
else { |
270 | 270 |
|
... | ... | |
292 | 292 |
|
293 | 293 |
colMax += 5; // 5 new columns written |
294 | 294 |
} |
295 |
} catch (ParseException e) { |
|
296 |
System.out.println("Error: while formating date columns: "+e); |
|
295 |
} |
|
296 |
catch (ParseException e) { |
|
297 |
System.out.println("Error: while formating date columns: " + e); |
|
297 | 298 |
e.printStackTrace(); |
298 | 299 |
return false; |
299 | 300 |
} |
300 | 301 |
return true; |
301 | 302 |
} |
302 |
|
|
303 |
|
|
303 | 304 |
/** |
304 | 305 |
* extract lines and columns following rules. |
305 | 306 |
* |
... | ... | |
316 | 317 |
public boolean searchAndReplaceInLines(HashMap<String, String[]> searchAndReplaceRules) throws EncryptedDocumentException, InvalidFormatException, IOException { |
317 | 318 |
Row headers = ws.getRow(0); |
318 | 319 |
if (headers == null) return false; |
319 |
|
|
320 |
|
|
320 | 321 |
int colMax = headers.getLastCellNum(); |
321 | 322 |
int nRows = ws.getPhysicalNumberOfRows(); |
322 |
|
|
323 |
|
|
323 | 324 |
HashMap<String, Integer> searchColumns = new HashMap<>(); |
324 | 325 |
for (int colIndex = 0; colIndex < colMax; colIndex++) { |
325 | 326 |
Cell cell = headers.getCell(colIndex); |
... | ... | |
330 | 331 |
} |
331 | 332 |
} |
332 | 333 |
} |
333 |
|
|
334 |
|
|
334 | 335 |
if (searchColumns.keySet().size() != searchAndReplaceRules.keySet().size()) { |
335 | 336 |
System.out.println("Error: not all columns found=" + searchColumns.keySet() + " of " + searchAndReplaceRules.keySet()); |
336 | 337 |
ArrayList<String> notFound = new ArrayList<>(searchAndReplaceRules.keySet()); |
... | ... | |
338 | 339 |
System.out.println("NOT FOUND: " + notFound); |
339 | 340 |
return false; |
340 | 341 |
} |
341 |
|
|
342 |
|
|
342 | 343 |
System.out.println("N Rows to update: " + nRows); |
343 | 344 |
int nRowWritten = 0; |
344 |
|
|
345 |
|
|
345 | 346 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) { // update all rows, starting from the second row (first row is the header) |
346 | 347 |
Row row = ws.getRow(rowIndex); |
347 | 348 |
if (row == null) continue; |
348 |
|
|
349 |
|
|
349 | 350 |
boolean change = false; |
350 | 351 |
for (String col : searchAndReplaceRules.keySet()) { |
351 | 352 |
int colIndex = searchColumns.get(col); |
352 |
|
|
353 |
|
|
353 | 354 |
Cell cell = row.getCell(colIndex); |
354 | 355 |
if (cell != null) { |
355 | 356 |
String value = cellToString(cell).trim(); |
... | ... | |
361 | 362 |
} |
362 | 363 |
} |
363 | 364 |
} |
364 |
|
|
365 |
|
|
365 | 366 |
if (change) { |
366 | 367 |
nRowWritten++; |
367 | 368 |
} |
368 | 369 |
} |
369 |
|
|
370 |
|
|
370 | 371 |
System.out.println("" + nRowWritten + " rows updated."); |
371 |
|
|
372 |
|
|
372 | 373 |
return true; |
373 | 374 |
} |
374 |
|
|
375 |
|
|
375 | 376 |
/** |
376 | 377 |
* one rule (regex test) per column |
377 | 378 |
* all lines are processed |
... | ... | |
384 | 385 |
public boolean extractTo(Sheet ws2, HashMap<String, String> lineRules, List<String> columnsSelection) throws EncryptedDocumentException, InvalidFormatException, IOException { |
385 | 386 |
Row headers = ws.getRow(0); |
386 | 387 |
if (headers == null) return false; |
387 |
|
|
388 |
|
|
388 | 389 |
int colMax = headers.getLastCellNum(); |
389 |
|
|
390 |
|
|
390 | 391 |
ArrayList<Integer> columnIdxToTest = new ArrayList<>(); |
391 | 392 |
ArrayList<Integer> columnIdxToWrite = new ArrayList<>(); |
392 | 393 |
ArrayList<String> columns = new ArrayList<>(); |
... | ... | |
400 | 401 |
columnIdxToTest.add(colIndex); |
401 | 402 |
columnsTest.add(lineRules.get(value)); |
402 | 403 |
} |
403 |
|
|
404 |
|
|
404 | 405 |
if (columnsSelection.contains(value)) { |
405 | 406 |
columnIdxToWrite.add(colIndex); |
406 | 407 |
} |
407 | 408 |
} |
408 | 409 |
} |
409 |
|
|
410 |
|
|
410 | 411 |
if (columnIdxToTest.size() == 0) { |
411 | 412 |
System.out.println("Error: no column found with name=" + lineRules.keySet()); |
412 | 413 |
return false; |
413 | 414 |
} |
414 |
|
|
415 |
|
|
415 | 416 |
if (columnIdxToWrite.size() != columnsSelection.size()) { |
416 | 417 |
System.out.println("Error: not all columns found=" + columnIdxToWrite + " of " + columnsSelection); |
417 | 418 |
ArrayList<String> notFound = new ArrayList<>(columnsSelection); |
... | ... | |
419 | 420 |
System.out.println("NOT FOUND: " + notFound); |
420 | 421 |
return false; |
421 | 422 |
} |
422 |
|
|
423 |
|
|
423 | 424 |
Row row2 = ws2.createRow(0); |
424 | 425 |
int nCell = 0; |
425 | 426 |
for (int iCol : columnIdxToWrite) { |
... | ... | |
427 | 428 |
cell2.setCellValue(cellToString(headers.getCell(iCol)).trim()); |
428 | 429 |
nCell++; |
429 | 430 |
} |
430 |
|
|
431 |
|
|
431 | 432 |
int nRowWritten = 1; |
432 | 433 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) { |
433 | 434 |
Row row = ws.getRow(rowIndex); |
434 | 435 |
if (row == null) continue; |
435 | 436 |
for (int i = 0; i < columnIdxToTest.size(); i++) { |
436 | 437 |
int colIndex = columnIdxToTest.get(i); |
437 |
|
|
438 |
|
|
438 | 439 |
Cell cell = row.getCell(colIndex); |
439 | 440 |
if (cell != null) { |
440 | 441 |
String value = cellToString(cell).trim(); |
441 | 442 |
if (value.matches(columnsTest.get(i))) { |
442 |
|
|
443 |
|
|
443 | 444 |
// write new line in Sheet ws2 |
444 | 445 |
row2 = ws2.createRow(nRowWritten); |
445 | 446 |
nCell = 0; |
... | ... | |
449 | 450 |
nCell++; |
450 | 451 |
} |
451 | 452 |
nRowWritten++; |
452 |
|
|
453 |
|
|
453 | 454 |
break; // next row |
454 | 455 |
} |
455 | 456 |
} |
456 | 457 |
} |
457 | 458 |
} |
458 |
|
|
459 |
|
|
459 | 460 |
return true; |
460 | 461 |
} |
461 |
|
|
462 |
|
|
462 | 463 |
/** |
463 | 464 |
* |
464 | 465 |
* @param rules String:old column name -> String:new column name |
... | ... | |
466 | 467 |
private boolean renameColumns(HashMap<String, String> rules) { |
467 | 468 |
Row headers = ws.getRow(0); |
468 | 469 |
if (headers == null) return false; |
469 |
|
|
470 |
|
|
470 | 471 |
int colMax = headers.getLastCellNum(); |
471 | 472 |
for (int colIndex = 0; colIndex < colMax; colIndex++) { |
472 | 473 |
Cell cell = headers.getCell(colIndex); |
... | ... | |
480 | 481 |
} |
481 | 482 |
return true; |
482 | 483 |
} |
483 |
|
|
484 |
|
|
484 | 485 |
public boolean save() throws IOException { |
485 | 486 |
return saveAs(tableFile); |
486 | 487 |
} |
487 |
|
|
488 |
|
|
488 | 489 |
public boolean saveAs(File newTableFile) throws IOException { |
489 | 490 |
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newTableFile, false)); |
490 | 491 |
wb.write(writer); |
491 | 492 |
writer.close(); |
492 | 493 |
return true; |
493 | 494 |
} |
494 |
|
|
495 |
|
|
495 | 496 |
public boolean readHeaders() { |
496 |
|
|
497 |
|
|
497 | 498 |
if (nRows == 0) { |
498 | 499 |
header = new String[0]; |
499 | 500 |
return header != null; |
500 | 501 |
} |
501 |
|
|
502 |
|
|
502 | 503 |
Row firstRow = ws.getRow(0); |
503 | 504 |
int colMax = firstRow.getLastCellNum(); |
504 |
|
|
505 |
|
|
505 | 506 |
header = new String[colMax]; |
506 | 507 |
for (int it = 0; it < colMax; it++) { |
507 | 508 |
header[it] = firstRow.getCell(it).getStringCellValue(); |
508 | 509 |
} |
509 |
|
|
510 |
|
|
510 | 511 |
if (iRow == 0) iRow = 1; // skip first line |
511 | 512 |
return true; |
512 | 513 |
} |
513 |
|
|
514 |
|
|
514 | 515 |
public boolean readRecord() { |
515 | 516 |
if (iRow < nRows) { |
516 | 517 |
_getRecord(); |
... | ... | |
522 | 523 |
return false; |
523 | 524 |
} |
524 | 525 |
} |
525 |
|
|
526 |
|
|
526 | 527 |
public String[] getHeaders() { |
527 | 528 |
return header; |
528 | 529 |
} |
529 |
|
|
530 |
|
|
530 | 531 |
public HashMap<String, String> getRecord() { |
531 | 532 |
return record; |
532 | 533 |
} |
533 |
|
|
534 |
|
|
534 | 535 |
public String get(String h) { |
535 | 536 |
if (record == null) return null; |
536 |
|
|
537 |
|
|
537 | 538 |
return record.get(h); |
538 | 539 |
} |
539 |
|
|
540 |
|
|
540 | 541 |
protected void _getRecord() { |
541 | 542 |
record = new HashMap<>(); |
542 | 543 |
Row row = ws.getRow(iRow); |
543 |
|
|
544 |
|
|
544 | 545 |
for (int colIndex = 0; colIndex < header.length; colIndex++) { |
545 | 546 |
String col = header[colIndex]; |
546 | 547 |
Cell cell = row.getCell(colIndex); |
... | ... | |
557 | 558 |
record.put(col, ""); |
558 | 559 |
} |
559 | 560 |
} |
560 |
|
|
561 |
|
|
561 | 562 |
for (String k : record.keySet()) |
562 | 563 |
if (record.get(k) == null) System.out.println("ERROR null value with " + k); |
563 | 564 |
} |
564 |
|
|
565 |
|
|
565 | 566 |
public void close() throws IOException { |
566 | 567 |
if (wb != null) wb.close(); |
567 | 568 |
} |
568 |
|
|
569 |
|
|
569 | 570 |
/** |
570 | 571 |
* |
571 | 572 |
* @param inputFile |
... | ... | |
573 | 574 |
* @return list of lines (line = list of cells) with values converted to String - the file is completely read |
574 | 575 |
*/ |
575 | 576 |
public static ArrayList<ArrayList<String>> toTable(File inputFile, String sheetName) { |
576 |
|
|
577 |
|
|
577 | 578 |
ArrayList<ArrayList<String>> data = new ArrayList<>(); |
578 |
|
|
579 |
|
|
579 | 580 |
if (!inputFile.canRead()) { |
580 | 581 |
System.out.println("** Excel2XML: '" + inputFile.getName() + "' file not readable. Aborting."); |
581 | 582 |
return data; |
582 | 583 |
} |
583 |
|
|
584 |
|
|
584 | 585 |
try { |
585 | 586 |
Workbook wb = WorkbookFactory.create(inputFile); |
586 | 587 |
Sheet ws; |
... | ... | |
593 | 594 |
ws = wb.getSheetAt(0); |
594 | 595 |
} |
595 | 596 |
} |
596 |
|
|
597 |
|
|
597 | 598 |
if (ws == null) { |
598 | 599 |
System.out.println("** Excel2XML: no sheet found. Aborting."); |
599 | 600 |
return data; |
600 | 601 |
} |
601 |
|
|
602 |
|
|
602 | 603 |
int nRows = ws.getPhysicalNumberOfRows(); |
603 | 604 |
if (nRows == 0) return null; |
604 |
|
|
605 |
|
|
605 | 606 |
Row firstRow = ws.getRow(0); |
606 | 607 |
int colMax = firstRow.getLastCellNum(); |
607 |
|
|
608 |
|
|
608 | 609 |
ArrayList<String> headers = new ArrayList<>(); |
609 | 610 |
for (int it = 0; it < colMax; it++) { |
610 | 611 |
headers.add(firstRow.getCell(it).getStringCellValue()); |
611 | 612 |
} |
612 |
|
|
613 |
|
|
613 | 614 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) { |
614 | 615 |
Row row = ws.getRow(rowIndex); |
615 | 616 |
ArrayList<String> dataLine = new ArrayList<>(); |
... | ... | |
632 | 633 |
e.printStackTrace(); |
633 | 634 |
return null; |
634 | 635 |
} |
635 |
|
|
636 |
|
|
636 | 637 |
return data; |
637 | 638 |
} |
638 |
|
|
639 |
|
|
639 | 640 |
/** |
640 | 641 |
* |
641 | 642 |
* @param cell. If null returns "" |
... | ... | |
643 | 644 |
*/ |
644 | 645 |
public static String cellToString(Cell cell) { |
645 | 646 |
if (cell == null) return ""; |
646 |
|
|
647 |
switch (cell.getCellTypeEnum()) {
|
|
648 |
case FORMULA: |
|
649 |
return "#formulla"; |
|
650 |
case NUMERIC: |
|
651 |
if (DateUtil.isCellDateFormatted(cell)) { |
|
652 |
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); |
|
653 |
return dateFormat.format(cell.getDateCellValue()); |
|
654 |
} |
|
655 |
else { |
|
656 |
Double value = cell.getNumericCellValue(); |
|
657 |
return value.toString(); |
|
658 |
} |
|
659 |
case STRING: |
|
660 |
return cell.getStringCellValue(); |
|
661 |
case BLANK: |
|
662 |
return ""; |
|
663 |
case BOOLEAN: |
|
664 |
return "" + cell.getBooleanCellValue(); |
|
665 |
case ERROR: |
|
666 |
return "#error"; |
|
667 |
default: |
|
668 |
return "#typeerror"; |
|
647 |
|
|
648 |
switch (cell.getCellType()) { |
|
649 |
case FORMULA:
|
|
650 |
return "#formulla";
|
|
651 |
case NUMERIC:
|
|
652 |
if (DateUtil.isCellDateFormatted(cell)) {
|
|
653 |
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
|
|
654 |
return dateFormat.format(cell.getDateCellValue());
|
|
655 |
}
|
|
656 |
else {
|
|
657 |
Double value = cell.getNumericCellValue();
|
|
658 |
return value.toString();
|
|
659 |
}
|
|
660 |
case STRING:
|
|
661 |
return cell.getStringCellValue();
|
|
662 |
case BLANK:
|
|
663 |
return "";
|
|
664 |
case BOOLEAN:
|
|
665 |
return "" + cell.getBooleanCellValue();
|
|
666 |
case ERROR:
|
|
667 |
return "#error";
|
|
668 |
default:
|
|
669 |
return "#typeerror";
|
|
669 | 670 |
} |
670 | 671 |
} |
671 |
|
|
672 |
|
|
672 |
|
|
673 |
|
|
673 | 674 |
public static void main(String[] args) throws Exception { |
674 | 675 |
File tableFile = new File("/home/mdecorde/Téléchargements/Requêtes_SVO-SOV.xlsx"); |
675 | 676 |
// ReadExcel excel = new ReadExcel(tableFile, null); |
... | ... | |
678 | 679 |
System.out.println(line); |
679 | 680 |
} |
680 | 681 |
} |
681 |
|
|
682 |
|
|
682 | 683 |
public static void mainAF(String[] args) throws Exception { |
683 | 684 |
// ArrayList<ArrayList<String>> data = toTable(new File("/home/mdecorde/xml/ruscorpora1m-test/metadata.xlsx"), null); |
684 | 685 |
// if (data.size() == 0) { |
... | ... | |
686 | 687 |
// } else { |
687 | 688 |
// System.out.println(data); |
688 | 689 |
// } |
689 |
|
|
690 |
|
|
690 | 691 |
File tableFile = new File("/home/mdecorde/TEMP/ANTRACT/AF/all.xlsx"); |
691 | 692 |
File table2File = null; |
692 |
|
|
693 |
|
|
693 | 694 |
System.out.println("open..."); |
694 | 695 |
ReadExcel excel = new ReadExcel(tableFile, null); |
695 |
|
|
696 |
|
|
696 | 697 |
HashMap<String, String> lineRules = new HashMap<>(); // line tests to select line to keep |
697 | 698 |
List<String> columnsSelection; // list of columns to keep |
698 | 699 |
HashMap<String, String> columnsToCopy = new HashMap<>(); |
... | ... | |
710 | 711 |
// columnsToCopy.put("Titre propre", "title"); // not working yet |
711 | 712 |
// columnsToCopy.put("Date de diffusion", "text-order"); // not working yet |
712 | 713 |
// searchAndReplaceRules.put("text-order", new String[] {"../../....", "$3$2$1"}); // not working yet |
713 |
|
|
714 |
|
|
714 | 715 |
// sujets |
715 | 716 |
table2File = new File("/home/mdecorde/TEMP/ANTRACT/AF/sujets.xlsx"); |
716 | 717 |
columnsSelection = Arrays.asList( |
... | ... | |
719 | 720 |
"Nom fichier segmenté (info)", "antract_video", "antract_debut", "antract_fin", "antract_duree", "antract_tc_type", "antract_tc_date", |
720 | 721 |
"Résumé", "Séquences", "Descripteurs (Aff. Lig.)", "Générique (Aff. Lig.)"); |
721 | 722 |
lineRules.put("Type de notice", "Notice sujet"); |
722 |
|
|
723 |
|
|
723 | 724 |
table2File.delete(); |
724 | 725 |
ReadExcel excel2 = new ReadExcel(table2File, null); |
725 | 726 |
if (!excel.extractTo(excel2, lineRules, columnsSelection)) { |
726 | 727 |
System.out.println("FAIL"); |
727 | 728 |
return; |
728 | 729 |
} |
729 |
|
|
730 |
|
|
730 | 731 |
System.out.println("copying column: " + columnsToCopy.size()); |
731 | 732 |
// excel2.copyColumns(columnsToCopy); |
732 |
|
|
733 |
|
|
733 | 734 |
System.out.println("search&replace column: " + searchAndReplaceRules.size()); |
734 | 735 |
// excel2.searchAndReplaceInLines(searchAndReplaceRules); |
735 |
|
|
736 |
|
|
736 | 737 |
System.out.println("renaming column: " + columnsToRenameRules.size()); |
737 | 738 |
excel2.renameColumns(columnsToRenameRules); |
738 |
|
|
739 |
|
|
739 | 740 |
System.out.println("saving&closing..."); |
740 | 741 |
excel2.save(); |
741 | 742 |
excel2.close(); |
tmp/org.txm.libs.msoffice/src/org/txm/libs/msoffice/SheetUtility.java (revision 3043) | ||
---|---|---|
1 | 1 |
package org.txm.libs.msoffice; |
2 | 2 |
|
3 | 3 |
import org.apache.poi.ss.usermodel.Cell; |
4 |
import org.apache.poi.ss.usermodel.CellType; |
|
4 | 5 |
import org.apache.poi.ss.usermodel.Row; |
5 | 6 |
import org.apache.poi.ss.usermodel.Sheet; |
6 | 7 |
|
... | ... | |
67 | 68 |
cNew.setCellStyle(cOld.getCellStyle()); |
68 | 69 |
|
69 | 70 |
switch (cNew.getCellType()) { |
70 |
case Cell.CELL_TYPE_BOOLEAN: {
|
|
71 |
case BOOLEAN: { |
|
71 | 72 |
cNew.setCellValue(cOld.getBooleanCellValue()); |
72 | 73 |
break; |
73 | 74 |
} |
74 |
case Cell.CELL_TYPE_NUMERIC: {
|
|
75 |
case NUMERIC: { |
|
75 | 76 |
cNew.setCellValue(cOld.getNumericCellValue()); |
76 | 77 |
break; |
77 | 78 |
} |
78 |
case Cell.CELL_TYPE_STRING: {
|
|
79 |
case STRING: { |
|
79 | 80 |
cNew.setCellValue(cOld.getStringCellValue()); |
80 | 81 |
break; |
81 | 82 |
} |
82 |
case Cell.CELL_TYPE_ERROR: {
|
|
83 |
case ERROR: { |
|
83 | 84 |
cNew.setCellValue(cOld.getErrorCellValue()); |
84 | 85 |
break; |
85 | 86 |
} |
86 |
case Cell.CELL_TYPE_FORMULA: {
|
|
87 |
case FORMULA: { |
|
87 | 88 |
cNew.setCellFormula(cOld.getCellFormula()); |
88 | 89 |
break; |
89 | 90 |
} |
91 |
default: |
|
92 |
break; |
|
90 | 93 |
} |
91 |
|
|
92 | 94 |
} |
93 | 95 |
} |
tmp/org.txm.libs.msoffice/build.properties (revision 3043) | ||
---|---|---|
2 | 2 |
output.. = bin/ |
3 | 3 |
bin.includes = META-INF/,\ |
4 | 4 |
.,\ |
5 |
lib/commons-codec-1.10.jar,\
|
|
6 |
lib/commons-collections4-4.1.jar,\
|
|
7 |
lib/commons-logging-1.2.jar,\
|
|
8 |
lib/curvesapi-1.04.jar,\
|
|
9 |
lib/junit-4.12.jar,\
|
|
10 |
lib/log4j-1.2.17.jar,\
|
|
11 |
lib/ooxml-schemas-1.3.jar,\
|
|
12 |
lib/poi-3.17.jar,\
|
|
13 |
lib/poi-excelant-3.17.jar,\
|
|
14 |
lib/poi-ooxml-3.17.jar,\
|
|
15 |
lib/poi-ooxml-schemas-3.17.jar,\
|
|
16 |
lib/poi-scratchpad-3.17.jar,\
|
|
17 |
lib/xmlbeans-2.6.0.jar
|
|
5 |
lib/commons-codec-1.15.jar,\
|
|
6 |
lib/commons-collections4-4.4.jar,\
|
|
7 |
lib/commons-math3-3.6.1.jar,\
|
|
8 |
lib/SparseBitSet-1.2.jar,\
|
|
9 |
lib/poi-5.0.0.jar,\
|
|
10 |
lib/poi-excelant-5.0.0.jar,\
|
|
11 |
lib/poi-integration-5.0.0.jar,\
|
|
12 |
lib/poi-ooxml-5.0.0.jar,\
|
|
13 |
lib/poi-ooxml-lite-5.0.0.jar,\
|
|
14 |
lib/poi-scratchpad-5.0.0.jar,\
|
|
15 |
lib/commons-compress-1.20.jar,\
|
|
16 |
lib/curvesapi-1.06.jar,\
|
|
17 |
lib/xmlbeans-4.0.0.jar
|
|
18 | 18 |
#qualifier=svn |
tmp/org.txm.libs.msoffice/.classpath (revision 3043) | ||
---|---|---|
1 | 1 |
<?xml version="1.0" encoding="UTF-8"?> |
2 | 2 |
<classpath> |
3 |
<classpathentry kind="con" |
|
4 |
path="org.eclipse.jdt.launching.JRE_CONTAINER" /> |
|
5 |
<classpathentry kind="con" |
|
6 |
path="org.eclipse.pde.core.requiredPlugins" /> |
|
7 |
<classpathentry kind="src" path="src" /> |
|
8 |
<classpathentry exported="true" kind="lib" |
|
9 |
path="lib/commons-codec-1.10.jar" /> |
|
10 |
<classpathentry exported="true" kind="lib" |
|
11 |
path="lib/commons-collections4-4.1.jar" /> |
|
12 |
<classpathentry exported="true" kind="lib" |
|
13 |
path="lib/commons-logging-1.2.jar" /> |
|
14 |
<classpathentry exported="true" kind="lib" |
|
15 |
path="lib/curvesapi-1.04.jar" /> |
|
16 |
<classpathentry exported="true" kind="lib" |
|
17 |
path="lib/junit-4.12.jar" /> |
|
18 |
<classpathentry exported="true" kind="lib" |
|
19 |
path="lib/log4j-1.2.17.jar" /> |
|
20 |
<classpathentry exported="true" kind="lib" |
|
21 |
path="lib/ooxml-schemas-1.3.jar" /> |
|
22 |
<classpathentry exported="true" kind="lib" |
|
23 |
path="lib/poi-3.17.jar" /> |
|
24 |
<classpathentry exported="true" kind="lib" |
|
25 |
path="lib/poi-excelant-3.17.jar" /> |
|
26 |
<classpathentry exported="true" kind="lib" |
|
27 |
path="lib/poi-ooxml-3.17.jar" /> |
|
28 |
<classpathentry exported="true" kind="lib" |
|
29 |
path="lib/poi-ooxml-schemas-3.17.jar" /> |
|
30 |
<classpathentry exported="true" kind="lib" |
|
31 |
path="lib/poi-scratchpad-3.17.jar" /> |
|
32 |
<classpathentry exported="true" kind="lib" |
|
33 |
path="lib/xmlbeans-2.6.0.jar" /> |
|
34 |
<classpathentry kind="output" path="bin" /> |
|
35 |
</classpath> |
|
3 |
<classpathentry exported="true" kind="lib" path="lib/commons-compress-1.20.jar"/> |
|
4 |
<classpathentry exported="true" kind="lib" path="lib/curvesapi-1.06.jar"/> |
|
5 |
<classpathentry exported="true" kind="lib" path="lib/xmlbeans-4.0.0.jar"/> |
|
6 |
<classpathentry exported="true" kind="lib" path="lib/poi-5.0.0.jar"/> |
|
7 |
<classpathentry exported="true" kind="lib" path="lib/poi-excelant-5.0.0.jar"/> |
|
8 |
<classpathentry exported="true" kind="lib" path="lib/poi-integration-5.0.0.jar"/> |
|
9 |
<classpathentry exported="true" kind="lib" path="lib/poi-ooxml-5.0.0.jar"/> |
|
10 |
<classpathentry exported="true" kind="lib" path="lib/poi-ooxml-lite-5.0.0.jar"/> |
|
11 |
<classpathentry exported="true" kind="lib" path="lib/poi-scratchpad-5.0.0.jar"/> |
|
12 |
<classpathentry exported="true" kind="lib" path="lib/commons-codec-1.15.jar"/> |
|
13 |
<classpathentry exported="true" kind="lib" path="lib/commons-collections4-4.4.jar"/> |
|
14 |
<classpathentry exported="true" kind="lib" path="lib/commons-math3-3.6.1.jar"/> |
|
15 |
<classpathentry exported="true" kind="lib" path="lib/SparseBitSet-1.2.jar"/> |
|
16 |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> |
|
17 |
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> |
|
18 |
<classpathentry kind="src" path="src"/> |
|
19 |
<classpathentry kind="output" path="bin"/> |
|
20 |
</classpath> |
tmp/org.txm.libs.msoffice/META-INF/MANIFEST.MF (revision 3043) | ||
---|---|---|
1 | 1 |
Manifest-Version: 1.0 |
2 | 2 |
Automatic-Module-Name: org.txm.libs.msoffice |
3 | 3 |
Bundle-SymbolicName: org.txm.libs.msoffice |
4 |
Export-Package: org.txm.libs.msoffice |
|
4 |
Export-Package: com.graphbuilder.curve, |
|
5 |
com.graphbuilder.geom, |
|
6 |
com.graphbuilder.math, |
|
7 |
com.graphbuilder.math.func, |
|
8 |
com.graphbuilder.org.apache.harmony.awt.gl, |
|
9 |
com.graphbuilder.struc, |
|
10 |
com.microsoft.schemas.compatibility, |
|
11 |
com.microsoft.schemas.compatibility.impl, |
|
12 |
com.microsoft.schemas.office.excel, |
|
13 |
com.microsoft.schemas.office.excel.impl, |
|
14 |
com.microsoft.schemas.office.office, |
|
15 |
com.microsoft.schemas.office.office.impl, |
|
16 |
com.microsoft.schemas.office.visio.x2012.main, |
|
17 |
com.microsoft.schemas.office.visio.x2012.main.impl, |
|
18 |
com.microsoft.schemas.office.x2006.digsig, |
|
19 |
com.microsoft.schemas.office.x2006.digsig.impl, |
|
20 |
com.microsoft.schemas.vml, |
|
21 |
com.microsoft.schemas.vml.impl, |
|
22 |
com.zaxxer.sparsebits, |
|
23 |
org.apache.commons.codec, |
|
24 |
org.apache.commons.codec.binary, |
|
25 |
org.apache.commons.codec.cli, |
|
26 |
org.apache.commons.codec.digest, |
|
27 |
org.apache.commons.codec.language, |
|
28 |
org.apache.commons.codec.language.bm, |
|
29 |
org.apache.commons.codec.net, |
|
30 |
org.apache.commons.collections4, |
|
31 |
org.apache.commons.collections4.bag, |
|
32 |
org.apache.commons.collections4.bidimap, |
|
33 |
org.apache.commons.collections4.collection, |
|
34 |
org.apache.commons.collections4.comparators, |
|
35 |
org.apache.commons.collections4.functors, |
|
36 |
org.apache.commons.collections4.iterators, |
|
37 |
org.apache.commons.collections4.keyvalue, |
|
38 |
org.apache.commons.collections4.list, |
|
39 |
org.apache.commons.collections4.map, |
|
40 |
org.apache.commons.collections4.multimap, |
|
41 |
org.apache.commons.collections4.multiset, |
|
42 |
org.apache.commons.collections4.properties, |
|
43 |
org.apache.commons.collections4.queue, |
|
44 |
org.apache.commons.collections4.sequence, |
|
45 |
org.apache.commons.collections4.set, |
|
46 |
org.apache.commons.collections4.splitmap, |
|
47 |
org.apache.commons.collections4.trie, |
|
48 |
org.apache.commons.collections4.trie.analyzer, |
|
49 |
org.apache.commons.compress, |
|
50 |
org.apache.commons.compress.archivers, |
|
51 |
org.apache.commons.compress.archivers.ar, |
|
52 |
org.apache.commons.compress.archivers.arj, |
|
53 |
org.apache.commons.compress.archivers.cpio, |
|
54 |
org.apache.commons.compress.archivers.dump, |
|
55 |
org.apache.commons.compress.archivers.examples, |
|
56 |
org.apache.commons.compress.archivers.jar, |
|
57 |
org.apache.commons.compress.archivers.sevenz, |
|
58 |
org.apache.commons.compress.archivers.tar, |
|
59 |
org.apache.commons.compress.archivers.zip, |
|
60 |
org.apache.commons.compress.changes, |
|
61 |
org.apache.commons.compress.compressors, |
|
62 |
org.apache.commons.compress.compressors.brotli, |
|
63 |
org.apache.commons.compress.compressors.bzip2, |
|
64 |
org.apache.commons.compress.compressors.deflate, |
|
65 |
org.apache.commons.compress.compressors.deflate64, |
|
66 |
org.apache.commons.compress.compressors.gzip, |
|
67 |
org.apache.commons.compress.compressors.lz4, |
|
68 |
org.apache.commons.compress.compressors.lz77support, |
|
69 |
org.apache.commons.compress.compressors.lzma, |
|
70 |
org.apache.commons.compress.compressors.lzw, |
|
71 |
org.apache.commons.compress.compressors.pack200, |
|
72 |
org.apache.commons.compress.compressors.snappy, |
|
73 |
org.apache.commons.compress.compressors.xz, |
|
74 |
org.apache.commons.compress.compressors.z, |
|
75 |
org.apache.commons.compress.compressors.zstandard, |
|
76 |
org.apache.commons.compress.parallel, |
|
77 |
org.apache.commons.compress.utils, |
|
78 |
org.apache.commons.math3, |
|
79 |
org.apache.commons.math3.analysis, |
|
80 |
org.apache.commons.math3.analysis.differentiation, |
|
81 |
org.apache.commons.math3.analysis.function, |
|
82 |
org.apache.commons.math3.analysis.integration, |
|
83 |
org.apache.commons.math3.analysis.integration.gauss, |
|
84 |
org.apache.commons.math3.analysis.interpolation, |
|
85 |
org.apache.commons.math3.analysis.polynomials, |
|
86 |
org.apache.commons.math3.analysis.solvers, |
|
87 |
org.apache.commons.math3.complex, |
|
88 |
org.apache.commons.math3.dfp, |
|
89 |
org.apache.commons.math3.distribution, |
|
90 |
org.apache.commons.math3.distribution.fitting, |
|
91 |
org.apache.commons.math3.exception, |
|
92 |
org.apache.commons.math3.exception.util, |
|
93 |
org.apache.commons.math3.filter, |
|
94 |
org.apache.commons.math3.fitting, |
|
95 |
org.apache.commons.math3.fitting.leastsquares, |
|
96 |
org.apache.commons.math3.fraction, |
|
97 |
org.apache.commons.math3.genetics, |
|
98 |
org.apache.commons.math3.geometry, |
|
99 |
org.apache.commons.math3.geometry.enclosing, |
|
100 |
org.apache.commons.math3.geometry.euclidean.oned, |
|
101 |
org.apache.commons.math3.geometry.euclidean.threed, |
|
102 |
org.apache.commons.math3.geometry.euclidean.twod, |
|
103 |
org.apache.commons.math3.geometry.euclidean.twod.hull, |
|
104 |
org.apache.commons.math3.geometry.hull, |
|
105 |
org.apache.commons.math3.geometry.partitioning, |
|
106 |
org.apache.commons.math3.geometry.partitioning.utilities, |
|
107 |
org.apache.commons.math3.geometry.spherical.oned, |
|
108 |
org.apache.commons.math3.geometry.spherical.twod, |
|
109 |
org.apache.commons.math3.linear, |
|
110 |
org.apache.commons.math3.ml.clustering, |
|
111 |
org.apache.commons.math3.ml.clustering.evaluation, |
|
112 |
org.apache.commons.math3.ml.distance, |
|
113 |
org.apache.commons.math3.ml.neuralnet, |
|
114 |
org.apache.commons.math3.ml.neuralnet.oned, |
|
115 |
org.apache.commons.math3.ml.neuralnet.sofm, |
|
116 |
org.apache.commons.math3.ml.neuralnet.sofm.util, |
|
117 |
org.apache.commons.math3.ml.neuralnet.twod, |
|
118 |
org.apache.commons.math3.ml.neuralnet.twod.util, |
|
119 |
org.apache.commons.math3.ode, |
|
120 |
org.txm.libs.msoffice |
|
5 | 121 |
Bundle-Name: org.txm.libs.msoffice |
6 |
Bundle-Version: 1.0.0.qualifier |
|
7 |
Bundle-ClassPath: lib/commons-codec-1.10.jar,lib/commons-collections4- |
|
8 |
4.1.jar,lib/commons-logging-1.2.jar,lib/curvesapi-1.04.jar,lib/junit- |
|
9 |
4.12.jar,lib/log4j-1.2.17.jar,lib/ooxml-schemas-1.3.jar,lib/poi-3.17. |
|
10 |
jar,lib/poi-excelant-3.17.jar,lib/poi-ooxml-3.17.jar,lib/poi-ooxml-sc |
|
11 |
hemas-3.17.jar,lib/poi-scratchpad-3.17.jar,lib/xmlbeans-2.6.0.jar,. |
|
122 |
Bundle-Version: 5.0.0.qualifier |
|
123 |
Bundle-ClassPath: ., |
|
124 |
lib/commons-codec-1.15.jar, |
|
125 |
lib/commons-collections4-4.4.jar, |
|
126 |
lib/commons-math3-3.6.1.jar, |
|
127 |
lib/SparseBitSet-1.2.jar, |
|
128 |
lib/poi-5.0.0.jar, |
|
129 |
lib/poi-excelant-5.0.0.jar, |
|
130 |
lib/poi-integration-5.0.0.jar, |
|
131 |
lib/poi-ooxml-5.0.0.jar, |
|
132 |
lib/poi-ooxml-lite-5.0.0.jar, |
|
133 |
lib/poi-scratchpad-5.0.0.jar, |
|
134 |
lib/commons-compress-1.20.jar, |
|
135 |
lib/curvesapi-1.06.jar, |
|
136 |
lib/xmlbeans-4.0.0.jar |
|
12 | 137 |
Bundle-ManifestVersion: 2 |
13 | 138 |
Bundle-RequiredExecutionEnvironment: JavaSE-1.8 |
14 | 139 |
Bundle-Vendor: Textometrie.org |
Formats disponibles : Unified diff