4 |
4 |
import java.io.File;
|
5 |
5 |
import java.io.FileOutputStream;
|
6 |
6 |
import java.io.IOException;
|
|
7 |
import java.text.ParseException;
|
7 |
8 |
import java.text.SimpleDateFormat;
|
8 |
9 |
import java.util.ArrayList;
|
9 |
10 |
import java.util.Arrays;
|
|
11 |
import java.util.Date;
|
10 |
12 |
import java.util.HashMap;
|
11 |
13 |
import java.util.List;
|
12 |
14 |
|
... | ... | |
21 |
23 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
22 |
24 |
|
23 |
25 |
public class ReadExcel {
|
24 |
|
|
|
26 |
|
25 |
27 |
Workbook wb = null;
|
26 |
|
|
|
28 |
|
27 |
29 |
Sheet ws = null;
|
28 |
|
|
|
30 |
|
29 |
31 |
String[] header = null;
|
30 |
|
|
|
32 |
|
31 |
33 |
HashMap<String, String> record = null;
|
32 |
|
|
|
34 |
|
33 |
35 |
int iRow = 0;
|
34 |
|
|
|
36 |
|
35 |
37 |
int nRows;
|
36 |
|
|
|
38 |
|
37 |
39 |
private File tableFile;
|
38 |
|
|
|
40 |
|
39 |
41 |
public ReadExcel(File tableFile, String sheetName) throws EncryptedDocumentException, InvalidFormatException, IOException {
|
40 |
|
|
|
42 |
|
41 |
43 |
this.tableFile = tableFile;
|
42 |
|
|
|
44 |
|
43 |
45 |
if (tableFile.exists() && tableFile.length() > 0) {
|
44 |
46 |
wb = WorkbookFactory.create(tableFile);
|
45 |
47 |
if (sheetName == null || sheetName.length() == 0) {
|
... | ... | |
51 |
53 |
ws = wb.getSheetAt(0);
|
52 |
54 |
}
|
53 |
55 |
}
|
54 |
|
|
|
56 |
|
55 |
57 |
nRows = ws.getPhysicalNumberOfRows();
|
56 |
58 |
}
|
57 |
59 |
else {
|
... | ... | |
59 |
61 |
ws = wb.createSheet();
|
60 |
62 |
}
|
61 |
63 |
}
|
62 |
|
|
|
64 |
|
63 |
65 |
/**
|
64 |
66 |
* one rule (regex test) per column
|
65 |
67 |
* all lines are processed
|
... | ... | |
70 |
72 |
Row headers = ws.getRow(0);
|
71 |
73 |
if (headers == null) return false;
|
72 |
74 |
int colMax = headers.getLastCellNum();
|
73 |
|
|
|
75 |
|
74 |
76 |
ArrayList<Integer> columnIdxToTest = new ArrayList<>();
|
75 |
77 |
ArrayList<String> columnsTest = new ArrayList<>();
|
76 |
78 |
for (int colIndex = 0; colIndex < colMax; colIndex++) {
|
... | ... | |
83 |
85 |
}
|
84 |
86 |
}
|
85 |
87 |
}
|
86 |
|
|
|
88 |
|
87 |
89 |
if (columnIdxToTest.size() == 0) {
|
88 |
90 |
System.out.println("Error: no column found with name=" + rules.keySet());
|
89 |
91 |
return false;
|
90 |
92 |
}
|
91 |
|
|
|
93 |
|
92 |
94 |
ArrayList<Integer> removed = new ArrayList<>();
|
93 |
95 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) {
|
94 |
96 |
Row row = ws.getRow(rowIndex);
|
95 |
97 |
if (row == null) continue;
|
96 |
98 |
for (int i = 0; i < columnIdxToTest.size(); i++) {
|
97 |
99 |
int colIndex = columnIdxToTest.get(i);
|
98 |
|
|
|
100 |
|
99 |
101 |
Cell cell = row.getCell(colIndex);
|
100 |
102 |
if (cell != null) {
|
101 |
103 |
String value = cellToString(cell).trim();
|
... | ... | |
107 |
109 |
}
|
108 |
110 |
}
|
109 |
111 |
}
|
110 |
|
|
|
112 |
|
111 |
113 |
// TODO very slow when there is a lot of lines
|
112 |
114 |
for (int rowIndex : removed) {
|
113 |
115 |
ws.shiftRows(rowIndex + 1, nRows, -1);
|
114 |
116 |
nRows--;
|
115 |
117 |
}
|
116 |
|
|
|
118 |
|
117 |
119 |
return true;
|
118 |
120 |
}
|
119 |
|
|
|
121 |
|
120 |
122 |
/**
|
121 |
123 |
* search and replace lines utils
|
122 |
124 |
*
|
... | ... | |
133 |
135 |
if (!extractTo(ws2, lineRules, columnsSelection)) {
|
134 |
136 |
return false;
|
135 |
137 |
}
|
136 |
|
|
|
138 |
|
137 |
139 |
return true;
|
138 |
140 |
}
|
139 |
|
|
|
141 |
|
140 |
142 |
/**
|
141 |
143 |
* extract lines and columns following rules.
|
142 |
144 |
*
|
... | ... | |
153 |
155 |
public boolean copyColumns(HashMap<String, String[]> columnsToCopy) throws EncryptedDocumentException, InvalidFormatException, IOException {
|
154 |
156 |
Row headers = ws.getRow(0);
|
155 |
157 |
if (headers == null) return false;
|
156 |
|
|
|
158 |
|
157 |
159 |
int colMax = headers.getLastCellNum();
|
158 |
160 |
int nRows = ws.getPhysicalNumberOfRows();
|
159 |
|
|
|
161 |
|
160 |
162 |
HashMap<String, Integer> searchColumns = new HashMap<>();
|
161 |
163 |
for (int colIndex = 0; colIndex < colMax; colIndex++) {
|
162 |
164 |
Cell cell = headers.getCell(colIndex);
|
... | ... | |
167 |
169 |
}
|
168 |
170 |
}
|
169 |
171 |
}
|
170 |
|
|
|
172 |
|
171 |
173 |
if (searchColumns.keySet().size() != columnsToCopy.keySet().size()) {
|
172 |
174 |
System.out.println("Error: not all columns found=" + searchColumns.keySet() + " of " + columnsToCopy.keySet());
|
173 |
175 |
ArrayList<String> notFound = new ArrayList<>(columnsToCopy.keySet());
|
... | ... | |
175 |
177 |
System.out.println("NOT FOUND: " + notFound);
|
176 |
178 |
return false;
|
177 |
179 |
}
|
178 |
|
|
|
180 |
|
179 |
181 |
System.out.println("N Rows to update: " + nRows);
|
180 |
182 |
int nRowWritten = 0;
|
181 |
|
|
|
183 |
|
182 |
184 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) { // update all rows, starting from the second row (first row is the header)
|
183 |
185 |
Row row = ws.getRow(rowIndex);
|
184 |
186 |
if (row == null) continue;
|
185 |
|
|
|
187 |
|
186 |
188 |
int icol = 0;
|
187 |
189 |
for (String col : columnsToCopy.keySet()) {
|
188 |
190 |
for (String newcol : columnsToCopy.get(col)) {
|
189 |
191 |
int colIndex = searchColumns.get(col);
|
190 |
|
|
|
192 |
|
191 |
193 |
Cell cell = row.getCell(colIndex);
|
192 |
194 |
if (cell != null) {
|
193 |
|
|
|
195 |
|
194 |
196 |
Cell newCell = row.createCell(colMax + icol);
|
195 |
197 |
if (rowIndex == 0) {
|
196 |
198 |
newCell.setCellValue(newcol); // first column must be renamed
|
... | ... | |
202 |
204 |
icol++;
|
203 |
205 |
}
|
204 |
206 |
}
|
205 |
|
|
|
207 |
|
206 |
208 |
nRowWritten++;
|
207 |
209 |
}
|
208 |
|
|
|
210 |
|
209 |
211 |
System.out.println("" + nRowWritten + " rows updated.");
|
210 |
|
|
|
212 |
|
211 |
213 |
return nRowWritten > 0;
|
212 |
214 |
}
|
213 |
|
|
|
215 |
|
|
216 |
public boolean formatDateColumns(List<String> columns, String dateformat) {
|
|
217 |
try {
|
|
218 |
java.text.SimpleDateFormat parser = new java.text.SimpleDateFormat(dateformat);
|
|
219 |
|
|
220 |
Row headers = ws.getRow(0);
|
|
221 |
if (headers == null) return false;
|
|
222 |
int colMax = headers.getLastCellNum(); // current number of columns
|
|
223 |
int nRows = ws.getPhysicalNumberOfRows();
|
|
224 |
|
|
225 |
HashMap<String, Integer> searchColumns = new HashMap<>();
|
|
226 |
for (int colIndex = 0; colIndex < colMax; colIndex++) {
|
|
227 |
Cell cell = headers.getCell(colIndex);
|
|
228 |
if (cell != null) {
|
|
229 |
String value = cellToString(cell).trim();
|
|
230 |
if (columns.contains(value)) {
|
|
231 |
searchColumns.put(value, colIndex);
|
|
232 |
}
|
|
233 |
}
|
|
234 |
}
|
|
235 |
|
|
236 |
if (searchColumns.keySet().size() != columns.size()) {
|
|
237 |
System.out.println("Error: not all columns found=" + searchColumns.keySet() + " of " + columns);
|
|
238 |
ArrayList<String> notFound = new ArrayList<>(columns);
|
|
239 |
notFound.removeAll(searchColumns.keySet());
|
|
240 |
System.out.println("NOT FOUND: " + notFound);
|
|
241 |
return false;
|
|
242 |
}
|
|
243 |
|
|
244 |
for (String column : columns) {
|
|
245 |
|
|
246 |
int colIndex = searchColumns.get(column);
|
|
247 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) { // update all rows, starting from the second row (first row is the header)
|
|
248 |
Row row = ws.getRow(rowIndex);
|
|
249 |
if (row == null) continue;
|
|
250 |
|
|
251 |
Cell cell = row.getCell(colIndex);
|
|
252 |
int icol = 0; // number of columns written for this row
|
|
253 |
|
|
254 |
if (cell != null) {
|
|
255 |
|
|
256 |
Cell newCell = null;
|
|
257 |
if (rowIndex == 0) { // header line
|
|
258 |
newCell = row.createCell(colMax + icol++);
|
|
259 |
newCell.setCellValue(column+"-joursemaine"); // first row must be renamed
|
|
260 |
newCell = row.createCell(colMax + icol++);
|
|
261 |
newCell.setCellValue(column+"-jour"); // first row must be renamed
|
|
262 |
newCell = row.createCell(colMax + icol++);
|
|
263 |
newCell.setCellValue(column+"-moi"); // first row must be renamed
|
|
264 |
newCell = row.createCell(colMax + icol++);
|
|
265 |
newCell.setCellValue(column+"-annee"); // first row must be renamed
|
|
266 |
newCell = row.createCell(colMax + icol++);
|
|
267 |
newCell.setCellValue(column+"-tri"); // first row must be renamed
|
|
268 |
}
|
|
269 |
else {
|
|
270 |
|
|
271 |
String rawValue = cellToString(cell).trim();
|
|
272 |
Date date = parser.parse(rawValue);
|
|
273 |
String joursemaine = new java.text.SimpleDateFormat("EEEE").format(date);
|
|
274 |
String jour = new java.text.SimpleDateFormat("dd").format(date);
|
|
275 |
String moi = new java.text.SimpleDateFormat("MM").format(date);
|
|
276 |
String annee = new java.text.SimpleDateFormat("yyyy").format(date);
|
|
277 |
String tri = new java.text.SimpleDateFormat("yyyy-MM-dd").format(date);
|
|
278 |
|
|
279 |
newCell = row.createCell(colMax + icol++);
|
|
280 |
newCell.setCellValue(joursemaine);
|
|
281 |
newCell = row.createCell(colMax + icol++);
|
|
282 |
newCell.setCellValue(jour);
|
|
283 |
newCell = row.createCell(colMax + icol++);
|
|
284 |
newCell.setCellValue(moi);
|
|
285 |
newCell = row.createCell(colMax + icol++);
|
|
286 |
newCell.setCellValue(annee);
|
|
287 |
newCell = row.createCell(colMax + icol++);
|
|
288 |
newCell.setCellValue(tri);
|
|
289 |
}
|
|
290 |
}
|
|
291 |
}
|
|
292 |
|
|
293 |
colMax += 5; // 5 new columns written
|
|
294 |
}
|
|
295 |
} catch (ParseException e) {
|
|
296 |
System.out.println("Error: while formating date columns: "+e);
|
|
297 |
e.printStackTrace();
|
|
298 |
return false;
|
|
299 |
}
|
|
300 |
return true;
|
|
301 |
}
|
|
302 |
|
214 |
303 |
/**
|
215 |
304 |
* extract lines and columns following rules.
|
216 |
305 |
*
|
... | ... | |
227 |
316 |
public boolean searchAndReplaceInLines(HashMap<String, String[]> searchAndReplaceRules) throws EncryptedDocumentException, InvalidFormatException, IOException {
|
228 |
317 |
Row headers = ws.getRow(0);
|
229 |
318 |
if (headers == null) return false;
|
230 |
|
|
|
319 |
|
231 |
320 |
int colMax = headers.getLastCellNum();
|
232 |
321 |
int nRows = ws.getPhysicalNumberOfRows();
|
233 |
|
|
|
322 |
|
234 |
323 |
HashMap<String, Integer> searchColumns = new HashMap<>();
|
235 |
324 |
for (int colIndex = 0; colIndex < colMax; colIndex++) {
|
236 |
325 |
Cell cell = headers.getCell(colIndex);
|
... | ... | |
241 |
330 |
}
|
242 |
331 |
}
|
243 |
332 |
}
|
244 |
|
|
|
333 |
|
245 |
334 |
if (searchColumns.keySet().size() != searchAndReplaceRules.keySet().size()) {
|
246 |
335 |
System.out.println("Error: not all columns found=" + searchColumns.keySet() + " of " + searchAndReplaceRules.keySet());
|
247 |
336 |
ArrayList<String> notFound = new ArrayList<>(searchAndReplaceRules.keySet());
|
... | ... | |
249 |
338 |
System.out.println("NOT FOUND: " + notFound);
|
250 |
339 |
return false;
|
251 |
340 |
}
|
252 |
|
|
|
341 |
|
253 |
342 |
System.out.println("N Rows to update: " + nRows);
|
254 |
343 |
int nRowWritten = 0;
|
255 |
|
|
|
344 |
|
256 |
345 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) { // update all rows, starting from the second row (first row is the header)
|
257 |
346 |
Row row = ws.getRow(rowIndex);
|
258 |
347 |
if (row == null) continue;
|
259 |
|
|
|
348 |
|
260 |
349 |
boolean change = false;
|
261 |
350 |
for (String col : searchAndReplaceRules.keySet()) {
|
262 |
351 |
int colIndex = searchColumns.get(col);
|
263 |
|
|
|
352 |
|
264 |
353 |
Cell cell = row.getCell(colIndex);
|
265 |
354 |
if (cell != null) {
|
266 |
355 |
String value = cellToString(cell).trim();
|
... | ... | |
272 |
361 |
}
|
273 |
362 |
}
|
274 |
363 |
}
|
275 |
|
|
|
364 |
|
276 |
365 |
if (change) {
|
277 |
366 |
nRowWritten++;
|
278 |
367 |
}
|
279 |
368 |
}
|
280 |
|
|
|
369 |
|
281 |
370 |
System.out.println("" + nRowWritten + " rows updated.");
|
282 |
|
|
|
371 |
|
283 |
372 |
return true;
|
284 |
373 |
}
|
285 |
|
|
|
374 |
|
286 |
375 |
/**
|
287 |
376 |
* one rule (regex test) per column
|
288 |
377 |
* all lines are processed
|
... | ... | |
295 |
384 |
public boolean extractTo(Sheet ws2, HashMap<String, String> lineRules, List<String> columnsSelection) throws EncryptedDocumentException, InvalidFormatException, IOException {
|
296 |
385 |
Row headers = ws.getRow(0);
|
297 |
386 |
if (headers == null) return false;
|
298 |
|
|
|
387 |
|
299 |
388 |
int colMax = headers.getLastCellNum();
|
300 |
|
|
|
389 |
|
301 |
390 |
ArrayList<Integer> columnIdxToTest = new ArrayList<>();
|
302 |
391 |
ArrayList<Integer> columnIdxToWrite = new ArrayList<>();
|
303 |
392 |
ArrayList<String> columns = new ArrayList<>();
|
... | ... | |
311 |
400 |
columnIdxToTest.add(colIndex);
|
312 |
401 |
columnsTest.add(lineRules.get(value));
|
313 |
402 |
}
|
314 |
|
|
|
403 |
|
315 |
404 |
if (columnsSelection.contains(value)) {
|
316 |
405 |
columnIdxToWrite.add(colIndex);
|
317 |
406 |
}
|
318 |
407 |
}
|
319 |
408 |
}
|
320 |
|
|
|
409 |
|
321 |
410 |
if (columnIdxToTest.size() == 0) {
|
322 |
411 |
System.out.println("Error: no column found with name=" + lineRules.keySet());
|
323 |
412 |
return false;
|
324 |
413 |
}
|
325 |
|
|
|
414 |
|
326 |
415 |
if (columnIdxToWrite.size() != columnsSelection.size()) {
|
327 |
416 |
System.out.println("Error: not all columns found=" + columnIdxToWrite + " of " + columnsSelection);
|
328 |
417 |
ArrayList<String> notFound = new ArrayList<>(columnsSelection);
|
... | ... | |
330 |
419 |
System.out.println("NOT FOUND: " + notFound);
|
331 |
420 |
return false;
|
332 |
421 |
}
|
333 |
|
|
|
422 |
|
334 |
423 |
Row row2 = ws2.createRow(0);
|
335 |
424 |
int nCell = 0;
|
336 |
425 |
for (int iCol : columnIdxToWrite) {
|
... | ... | |
338 |
427 |
cell2.setCellValue(cellToString(headers.getCell(iCol)).trim());
|
339 |
428 |
nCell++;
|
340 |
429 |
}
|
341 |
|
|
|
430 |
|
342 |
431 |
int nRowWritten = 1;
|
343 |
432 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) {
|
344 |
433 |
Row row = ws.getRow(rowIndex);
|
345 |
434 |
if (row == null) continue;
|
346 |
435 |
for (int i = 0; i < columnIdxToTest.size(); i++) {
|
347 |
436 |
int colIndex = columnIdxToTest.get(i);
|
348 |
|
|
|
437 |
|
349 |
438 |
Cell cell = row.getCell(colIndex);
|
350 |
439 |
if (cell != null) {
|
351 |
440 |
String value = cellToString(cell).trim();
|
352 |
441 |
if (value.matches(columnsTest.get(i))) {
|
353 |
|
|
|
442 |
|
354 |
443 |
// write new line in Sheet ws2
|
355 |
444 |
row2 = ws2.createRow(nRowWritten);
|
356 |
445 |
nCell = 0;
|
... | ... | |
360 |
449 |
nCell++;
|
361 |
450 |
}
|
362 |
451 |
nRowWritten++;
|
363 |
|
|
|
452 |
|
364 |
453 |
break; // next row
|
365 |
454 |
}
|
366 |
455 |
}
|
367 |
456 |
}
|
368 |
457 |
}
|
369 |
|
|
|
458 |
|
370 |
459 |
return true;
|
371 |
460 |
}
|
372 |
|
|
|
461 |
|
373 |
462 |
/**
|
374 |
463 |
*
|
375 |
464 |
* @param rules String:old column name -> String:new column name
|
... | ... | |
377 |
466 |
private boolean renameColumns(HashMap<String, String> rules) {
|
378 |
467 |
Row headers = ws.getRow(0);
|
379 |
468 |
if (headers == null) return false;
|
380 |
|
|
|
469 |
|
381 |
470 |
int colMax = headers.getLastCellNum();
|
382 |
471 |
for (int colIndex = 0; colIndex < colMax; colIndex++) {
|
383 |
472 |
Cell cell = headers.getCell(colIndex);
|
... | ... | |
391 |
480 |
}
|
392 |
481 |
return true;
|
393 |
482 |
}
|
394 |
|
|
|
483 |
|
395 |
484 |
public boolean save() throws IOException {
|
396 |
485 |
return saveAs(tableFile);
|
397 |
486 |
}
|
398 |
|
|
|
487 |
|
399 |
488 |
public boolean saveAs(File newTableFile) throws IOException {
|
400 |
489 |
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newTableFile, false));
|
401 |
490 |
wb.write(writer);
|
402 |
491 |
writer.close();
|
403 |
492 |
return true;
|
404 |
493 |
}
|
405 |
|
|
|
494 |
|
406 |
495 |
public boolean readHeaders() {
|
407 |
|
|
|
496 |
|
408 |
497 |
if (nRows == 0) {
|
409 |
498 |
header = new String[0];
|
410 |
499 |
return header != null;
|
411 |
500 |
}
|
412 |
|
|
|
501 |
|
413 |
502 |
Row firstRow = ws.getRow(0);
|
414 |
503 |
int colMax = firstRow.getLastCellNum();
|
415 |
|
|
|
504 |
|
416 |
505 |
header = new String[colMax];
|
417 |
506 |
for (int it = 0; it < colMax; it++) {
|
418 |
507 |
header[it] = firstRow.getCell(it).getStringCellValue();
|
419 |
508 |
}
|
420 |
|
|
|
509 |
|
421 |
510 |
if (iRow == 0) iRow = 1; // skip first line
|
422 |
511 |
return true;
|
423 |
512 |
}
|
424 |
|
|
|
513 |
|
425 |
514 |
public boolean readRecord() {
|
426 |
515 |
if (iRow < nRows) {
|
427 |
516 |
_getRecord();
|
... | ... | |
433 |
522 |
return false;
|
434 |
523 |
}
|
435 |
524 |
}
|
436 |
|
|
|
525 |
|
437 |
526 |
public String[] getHeaders() {
|
438 |
527 |
return header;
|
439 |
528 |
}
|
440 |
|
|
|
529 |
|
441 |
530 |
public HashMap<String, String> getRecord() {
|
442 |
531 |
return record;
|
443 |
532 |
}
|
444 |
|
|
|
533 |
|
445 |
534 |
public String get(String h) {
|
446 |
535 |
if (record == null) return null;
|
447 |
|
|
|
536 |
|
448 |
537 |
return record.get(h);
|
449 |
538 |
}
|
450 |
|
|
|
539 |
|
451 |
540 |
protected void _getRecord() {
|
452 |
541 |
record = new HashMap<>();
|
453 |
542 |
Row row = ws.getRow(iRow);
|
454 |
|
|
|
543 |
|
455 |
544 |
for (int colIndex = 0; colIndex < header.length; colIndex++) {
|
456 |
545 |
String col = header[colIndex];
|
457 |
546 |
Cell cell = row.getCell(colIndex);
|
... | ... | |
468 |
557 |
record.put(col, "");
|
469 |
558 |
}
|
470 |
559 |
}
|
471 |
|
|
|
560 |
|
472 |
561 |
for (String k : record.keySet())
|
473 |
562 |
if (record.get(k) == null) System.out.println("ERROR null value with " + k);
|
474 |
563 |
}
|
475 |
|
|
|
564 |
|
476 |
565 |
public void close() throws IOException {
|
477 |
566 |
if (wb != null) wb.close();
|
478 |
567 |
}
|
479 |
|
|
|
568 |
|
480 |
569 |
/**
|
481 |
570 |
*
|
482 |
571 |
* @param inputFile
|
... | ... | |
484 |
573 |
* @return list of lines (line = list of cells) with values converted to String - the file is completely read
|
485 |
574 |
*/
|
486 |
575 |
public static ArrayList<ArrayList<String>> toTable(File inputFile, String sheetName) {
|
487 |
|
|
|
576 |
|
488 |
577 |
ArrayList<ArrayList<String>> data = new ArrayList<>();
|
489 |
|
|
|
578 |
|
490 |
579 |
if (!inputFile.canRead()) {
|
491 |
580 |
System.out.println("** Excel2XML: '" + inputFile.getName() + "' file not readable. Aborting.");
|
492 |
581 |
return data;
|
493 |
582 |
}
|
494 |
|
|
|
583 |
|
495 |
584 |
try {
|
496 |
585 |
Workbook wb = WorkbookFactory.create(inputFile);
|
497 |
586 |
Sheet ws;
|
... | ... | |
504 |
593 |
ws = wb.getSheetAt(0);
|
505 |
594 |
}
|
506 |
595 |
}
|
507 |
|
|
|
596 |
|
508 |
597 |
if (ws == null) {
|
509 |
598 |
System.out.println("** Excel2XML: no sheet found. Aborting.");
|
510 |
599 |
return data;
|
511 |
600 |
}
|
512 |
|
|
|
601 |
|
513 |
602 |
int nRows = ws.getPhysicalNumberOfRows();
|
514 |
603 |
if (nRows == 0) return null;
|
515 |
|
|
|
604 |
|
516 |
605 |
Row firstRow = ws.getRow(0);
|
517 |
606 |
int colMax = firstRow.getLastCellNum();
|
518 |
|
|
|
607 |
|
519 |
608 |
ArrayList<String> headers = new ArrayList<>();
|
520 |
609 |
for (int it = 0; it < colMax; it++) {
|
521 |
610 |
headers.add(firstRow.getCell(it).getStringCellValue());
|
522 |
611 |
}
|
523 |
|
|
|
612 |
|
524 |
613 |
for (int rowIndex = 0; rowIndex < nRows; rowIndex++) {
|
525 |
614 |
Row row = ws.getRow(rowIndex);
|
526 |
615 |
ArrayList<String> dataLine = new ArrayList<>();
|
... | ... | |
543 |
632 |
e.printStackTrace();
|
544 |
633 |
return null;
|
545 |
634 |
}
|
546 |
|
|
|
635 |
|
547 |
636 |
return data;
|
548 |
637 |
}
|
549 |
|
|
|
638 |
|
550 |
639 |
/**
|
551 |
640 |
*
|
552 |
641 |
* @param cell. If null returns ""
|
... | ... | |
554 |
643 |
*/
|
555 |
644 |
public static String cellToString(Cell cell) {
|
556 |
645 |
if (cell == null) return "";
|
557 |
|
|
|
646 |
|
558 |
647 |
switch (cell.getCellTypeEnum()) {
|
559 |
|
case FORMULA:
|
560 |
|
return "#formulla";
|
561 |
|
case NUMERIC:
|
562 |
|
if (DateUtil.isCellDateFormatted(cell)) {
|
563 |
|
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
|
564 |
|
return dateFormat.format(cell.getDateCellValue());
|
565 |
|
}
|
566 |
|
else {
|
567 |
|
Double value = cell.getNumericCellValue();
|
568 |
|
return value.toString();
|
569 |
|
}
|
570 |
|
case STRING:
|
571 |
|
return cell.getStringCellValue();
|
572 |
|
case BLANK:
|
573 |
|
return "";
|
574 |
|
case BOOLEAN:
|
575 |
|
return "" + cell.getBooleanCellValue();
|
576 |
|
case ERROR:
|
577 |
|
return "#error";
|
578 |
|
default:
|
579 |
|
return "#typeerror";
|
|
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";
|
580 |
669 |
}
|
581 |
670 |
}
|
582 |
|
|
583 |
|
|
|
671 |
|
|
672 |
|
584 |
673 |
public static void main(String[] args) throws Exception {
|
585 |
674 |
File tableFile = new File("/home/mdecorde/Téléchargements/Requêtes_SVO-SOV.xlsx");
|
586 |
675 |
// ReadExcel excel = new ReadExcel(tableFile, null);
|
... | ... | |
589 |
678 |
System.out.println(line);
|
590 |
679 |
}
|
591 |
680 |
}
|
592 |
|
|
|
681 |
|
593 |
682 |
public static void mainAF(String[] args) throws Exception {
|
594 |
683 |
// ArrayList<ArrayList<String>> data = toTable(new File("/home/mdecorde/xml/ruscorpora1m-test/metadata.xlsx"), null);
|
595 |
684 |
// if (data.size() == 0) {
|
... | ... | |
597 |
686 |
// } else {
|
598 |
687 |
// System.out.println(data);
|
599 |
688 |
// }
|
600 |
|
|
|
689 |
|
601 |
690 |
File tableFile = new File("/home/mdecorde/TEMP/ANTRACT/AF/all.xlsx");
|
602 |
691 |
File table2File = null;
|
603 |
|
|
|
692 |
|
604 |
693 |
System.out.println("open...");
|
605 |
694 |
ReadExcel excel = new ReadExcel(tableFile, null);
|
606 |
|
|
|
695 |
|
607 |
696 |
HashMap<String, String> lineRules = new HashMap<>(); // line tests to select line to keep
|
608 |
697 |
List<String> columnsSelection; // list of columns to keep
|
609 |
698 |
HashMap<String, String> columnsToCopy = new HashMap<>();
|
... | ... | |
621 |
710 |
// columnsToCopy.put("Titre propre", "title"); // not working yet
|
622 |
711 |
// columnsToCopy.put("Date de diffusion", "textorder"); // not working yet
|
623 |
712 |
// searchAndReplaceRules.put("textorder", new String[] {"../../....", "$3$2$1"}); // not working yet
|
624 |
|
|
|
713 |
|
625 |
714 |
// sujets
|
626 |
715 |
table2File = new File("/home/mdecorde/TEMP/ANTRACT/AF/sujets.xlsx");
|
627 |
716 |
columnsSelection = Arrays.asList(
|
... | ... | |
630 |
719 |
"Nom fichier segmenté (info)", "antract_video", "antract_debut", "antract_fin", "antract_duree", "antract_tc_type", "antract_tc_date",
|
631 |
720 |
"Résumé", "Séquences", "Descripteurs (Aff. Lig.)", "Générique (Aff. Lig.)");
|
632 |
721 |
lineRules.put("Type de notice", "Notice sujet");
|
633 |
|
|
|
722 |
|
634 |
723 |
table2File.delete();
|
635 |
724 |
ReadExcel excel2 = new ReadExcel(table2File, null);
|
636 |
725 |
if (!excel.extractTo(excel2, lineRules, columnsSelection)) {
|
637 |
726 |
System.out.println("FAIL");
|
638 |
727 |
return;
|
639 |
728 |
}
|
640 |
|
|
|
729 |
|
641 |
730 |
System.out.println("copying column: " + columnsToCopy.size());
|
642 |
731 |
// excel2.copyColumns(columnsToCopy);
|
643 |
|
|
|
732 |
|
644 |
733 |
System.out.println("search&replace column: " + searchAndReplaceRules.size());
|
645 |
734 |
// excel2.searchAndReplaceInLines(searchAndReplaceRules);
|
646 |
|
|
|
735 |
|
647 |
736 |
System.out.println("renaming column: " + columnsToRenameRules.size());
|
648 |
737 |
excel2.renameColumns(columnsToRenameRules);
|
649 |
|
|
|
738 |
|
650 |
739 |
System.out.println("saving&closing...");
|
651 |
740 |
excel2.save();
|
652 |
741 |
excel2.close();
|