-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadExcel.pde
55 lines (53 loc) · 1.24 KB
/
ReadExcel.pde
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
// This code allows to read XSLX files it was copied from an example
SXSSFWorkbook swb=null;
Sheet sh=null;
InputStream inp=null;
Workbook wb=null;
String[][] importExcel(String filepath) throws Exception {
String[][] temp;
try {
inp = new FileInputStream(filepath);
}
catch(Exception e) {
e.printStackTrace();
throw e;
}
try {
wb = WorkbookFactory.create(inp);
}
catch(Exception e) {
e.printStackTrace();
throw e;
}
Sheet sheet = wb.getSheetAt(0);
int sizeX = sheet.getLastRowNum();
int sizeY = 100;
for (int i=0;i<sizeX;++i) {
Row row = sheet.getRow(i);
for (int j=0;j<sizeY;++j) {
try {
Cell cell = row.getCell(j);
}
catch(Exception e) {
if (j>sizeY) {
sizeY = j;
}
}
}
}
temp = new String[sizeX][sizeY];
for (int i=0;i<sizeX;++i) {
for (int j=0;j<sizeY;++j) {
Row row = sheet.getRow(i);
try {
Cell cell = row.getCell(j);
if (cell.getCellType()==0 || cell.getCellType()==2 || cell.getCellType()==3)cell.setCellType(1);
temp[i][j] = cell.getStringCellValue();
}
catch(Exception e) {
}
}
}
println("Excel file imported: " + filepath + " successfully!");
return temp;
}