-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileProcesser.java
More file actions
159 lines (139 loc) · 6.61 KB
/
FileProcesser.java
File metadata and controls
159 lines (139 loc) · 6.61 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.company;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class FileProcesser {
/**
* @param dir
* @param wb
* @throws IOException
*/
public static Workbook wb;
ArrayList<Course> courseList = new ArrayList<>();
public static void save(String dir, Object[][] p) throws IOException {
// TODO Auto-generated method stub
try {
InputStream inp = new FileInputStream(dir);
wb = WorkbookFactory.create(inp);
org.apache.poi.ss.usermodel.Sheet sheet1 = wb.getSheetAt(0);
int rowStart = sheet1.getFirstRowNum();
int rowEnd = sheet1.getLastRowNum();
Row tr = sheet1.getRow(0);
int ColumnEnd = tr.getLastCellNum();
String s[] = new String[ColumnEnd];
for (int i = 0; i < ColumnEnd; i++) {
Cell c = tr.getCell(i, Row.RETURN_BLANK_AS_NULL);
String temps = c.getRichStringCellValue().getString();
s[i] = temps;
}
for (int rowNum = rowStart + 1; rowNum <= rowEnd; rowNum++) {
Row r = sheet1.getRow(rowNum);
int lastColumn = r.getLastCellNum();
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
switch (c.getCellType()) {
case Cell.CELL_TYPE_STRING:
// System.out.println(c.getRichStringCellValue().getString());
p[rowNum - 1][cn] = (Object) c.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(c)) {
// System.out.println(c.getDateCellValue());
c.setCellValue(p[rowNum - 1][cn].toString());
} else {
//System.out.println(c.getNumericCellValue());
c.setCellValue(p[rowNum - 1][cn].toString());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
// System.out.println(c.getBooleanCellValue());
c.setCellValue(p[rowNum - 1][cn].toString());
break;
case Cell.CELL_TYPE_FORMULA:
// System.out.println(c.getCellFormula());
c.setCellValue(p[rowNum - 1][cn].toString());
break;
default:
System.out.println();
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
FileOutputStream out = null;
out = new FileOutputStream(dir);
wb.write(out);
}
public void read(final String Dir) {
try {
InputStream inp = new FileInputStream(Dir);
Workbook wb = WorkbookFactory.create(inp);
org.apache.poi.ss.usermodel.Sheet sheet1 = wb.getSheetAt(0);
int rowStart = sheet1.getFirstRowNum();
int rowEnd = sheet1.getLastRowNum();
Row tr = sheet1.getRow(0);
int ColumnEnd = tr.getLastCellNum();
String s[] = new String[ColumnEnd];
for (int i = 0; i < ColumnEnd; i++) {
Cell c = tr.getCell(i, Row.RETURN_BLANK_AS_NULL);
String temps = c.getRichStringCellValue().getString();
s[i] = temps;
}
Object[][] cellData=new Object[rowEnd-rowStart][10];
for (int rowNum = rowStart + 1; rowNum <= rowEnd; rowNum++) {
Row r = sheet1.getRow(rowNum);
int lastColumn = r.getLastCellNum();
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
switch (c.getCellType()) {
case Cell.CELL_TYPE_STRING:
//System.out.println(c.getRichStringCellValue().getString());
cellData[rowNum - 1][cn] = c.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(c)) {
// System.out.println(c.getDateCellValue());
cellData[rowNum - 1][cn] = c.getDateCellValue();
} else {
// System.out.println(c.getNumericCellValue());
cellData[rowNum - 1][cn] = (int) c.getNumericCellValue();
}
break;
case Cell.CELL_TYPE_BOOLEAN:
// System.out.println(c.getBooleanCellValue());
cellData[rowNum - 1][cn] = c.getBooleanCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
// System.out.println(c.getCellFormula());
cellData[rowNum - 1][cn] = c.getCellFormula();
break;
default:
System.out.println();
}
}
}
getCourseObjectFromCell(cellData, rowEnd - rowStart);
} catch (Exception e1) {
e1.printStackTrace();
}
}
public ArrayList<Course> getCourseList() {
return courseList;
}
private void getCourseObjectFromCell(Object[][] cellData, int rowNum) {
for (int i = 0; i < rowNum; i++) {
Course course = new Course(cellData[i][0].toString(), cellData[i][1].toString(), cellData[i][2].toString(),
cellData[i][3].toString(), (cellData[i][4].toString()), (cellData[i][5].toString()), Integer.valueOf(cellData[i][6].toString()),
cellData[i][7].toString());
courseList.add(course);
}
}
}