-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTikaXLSXRowsToDocs.js
88 lines (73 loc) · 2.88 KB
/
TikaXLSXRowsToDocs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
var tikaRowsToDocs = function (doc) {
if (doc !== null && doc.getId() !== null) {
var File = java.io.File;
var FileInputStream = java.io.FileInputStream;
var ArrayList = java.util.ArrayList;
var HashMap = java.util.HashMap;
var Iterator = java.util.Iterator;
var Sheet = org.apache.poi.ss.usermodel.Sheet;
var Cell = org.apache.poi.ss.usermodel.Cell;
var Row = org.apache.poi.ss.usermodel.Row;
var Workbook = org.apache.poi.ss.usermodel.Workbook;
var XSSFWorkbook = org.apache.poi.xssf.usermodel.XSSFWorkbook;
var e = java.lang.Exception;
var docs = new ArrayList();
try {
var excelFilePath = doc.getId();//"/projects/lucidworks_support/sharepoint_data-sample.xlsx";
var inputStream = new FileInputStream(new File(excelFilePath));
var workbook = new XSSFWorkbook(inputStream);
var firstSheet = workbook.getSheetAt(0);
var iterator = firstSheet.iterator();
var headers = new ArrayList();
var map = new HashMap();
var count = 0;
var column = 0;
var value = "";
var header = "";
while (iterator.hasNext()) {
var nextRow = iterator.next();
var cellIterator = nextRow.cellIterator();
column = 0;
while (cellIterator.hasNext()) {
var cell = cellIterator.next();
if (count > 0) {
header = headers.get(column);
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
value = "" + cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
value = "" + cell.getNumericCellValue();
break;
}
// System.out.print(" - ");
column++;
if (count == 0) {
headers.add(value);
}
if (count > 0) {
logger.debug("Header: " + header + " :: " + value);
}
}
// System.out.println();
count++;
}
workbook.close();
inputStream.close();
} catch (e) {
logger.error(e);
}
return docs;
} else {
return doc;
}
}