-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcel_Methods.java
More file actions
318 lines (295 loc) · 11.2 KB
/
Excel_Methods.java
File metadata and controls
318 lines (295 loc) · 11.2 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel_Methods {
public static void CreateBlankExcel(String File_Path) {
try {
XSSFWorkbook workbook = new XSSFWorkbook();
FileOutputStream out;
out = new FileOutputStream(
new File(File_Path));
workbook.write(out);
out.close();
workbook.close();
System.out.println(File_Path+" is written successfully");
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(CreateBlankExcel)");
e.printStackTrace();
}
}
public static void CreateBlankExcel_L(String File_Path, ArrayList<String> Sheets) {
try {
XSSFWorkbook workbook = new XSSFWorkbook();
for (int counter = 0 ; counter < Sheets.size() ; counter++)
{
workbook.createSheet(Sheets.get(counter));
}
FileOutputStream out;
out = new FileOutputStream(
new File(File_Path));
workbook.write(out);
out.close();
workbook.close();
System.out.println(File_Path+" is written successfully");
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(CreateBlankExcel_L)");
e.printStackTrace();
}
}
public static int getSheetNum(String File_Path) {
File file= new File(File_Path);
int x = 0;
if (file.isFile() && file.exists()) {
XSSFWorkbook workbook;
try {
workbook = new XSSFWorkbook(file);
x = workbook.getNumberOfSheets() ;
workbook.close();
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(getSheetNum)");
e.printStackTrace();
}
}else {
System.out.println(File_Path+ " is not Found.");
}
return x;
}
public static ArrayList<String> getSheetNames(String File_Path) {
ArrayList<String> Sheets;
File file= new File(File_Path);
if (file.isFile() && file.exists()) {
XSSFWorkbook workbook;
Sheets = new ArrayList<String>() ;
try {
workbook = new XSSFWorkbook(file);
for (int counter = 0 ; counter < workbook.getNumberOfSheets() ; counter++){
Sheets.add(workbook.getSheetName(counter));
}
workbook.close();
return Sheets;
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(getSheetNum)");
e.printStackTrace();
}
}else {
System.out.println(File_Path+ " is not Found.");
}
return null;
}
public static void CopyExcel(String File_Path, String File_Path_Copy) {
File file= new File(File_Path);
if (file.isFile() && file.exists()) {
XSSFWorkbook workbook;
try {
workbook = new XSSFWorkbook(file);
FileOutputStream out = new FileOutputStream(
new File(File_Path_Copy));
workbook.write(out);
out.close();
workbook.close();
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(CopyExcel)");
e.printStackTrace();
}
System.out.println(File_Path+" is backuped successfully");
}else {
System.out.println(File_Path+ " is not Found.");
}
}
public static void AddExcelSheet(String File_Path, ArrayList<String> Sheets) {
CopyExcel(File_Path,File_Path+"_"+datetimenow());
File file= new File(File_Path);
XSSFWorkbook workbook;
if (file.isFile() && file.exists()) {
try {
ArrayList<String> Sheets_names = getSheetNames(File_Path);
workbook = new XSSFWorkbook(new FileInputStream(file));
for (int counter = 0 ; counter < Sheets.size() ; counter++)
{
if(!Sheets_names.contains(Sheets.get(counter))) {
workbook.createSheet(Sheets.get(counter));
}
}
FileOutputStream out = new FileOutputStream(
new File(File_Path));
workbook.write(out);
out.close();
System.out.println(File_Path+" is written successfully");
workbook.close();
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(AddExcelSheet)");
e.printStackTrace();
}
}else {
System.out.println(File_Path+ " is not Found.");
}
}
public static void RemoveExcelSheet(String File_Path, ArrayList<String> Sheets) {
CopyExcel(File_Path,File_Path+"_"+datetimenow());
File file= new File(File_Path);
XSSFWorkbook workbook;
try {
workbook = new XSSFWorkbook(new FileInputStream(file));
if (file.isFile() && file.exists()) {
for (int counter = 0 ; counter < Sheets.size() ; counter++){
workbook.removeSheetAt(workbook.getSheetIndex(Sheets.get(counter)));
}
FileOutputStream out = new FileOutputStream(
new File(File_Path));
workbook.write(out);
out.close();
workbook.close();
System.out.println(File_Path+" is written successfully");
}else {
System.out.println(File_Path+ " is not Found.");
}
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(RemoveExcelSheet)");
e.printStackTrace();
}
}
public static void Addrecord(String File_Path,String Sheet_Name ,Map < Integer, Object[] > records) {
CopyExcel(File_Path,File_Path+"_"+datetimenow());
File file= new File(File_Path);
XSSFWorkbook workbook;
try {
workbook = new XSSFWorkbook(new FileInputStream(file));
if (file.isFile() && file.exists()) {
XSSFSheet Sheet = workbook.getSheet(Sheet_Name);
XSSFRow row;
Set < Integer > keyid = records.keySet();
int rowid = Sheet.getLastRowNum()+1;
for (Integer key : keyid) {
row = Sheet.createRow(rowid++);
Object [] objectArr = records.get(key);
int cellid = 0;
for (Object obj : objectArr) {
Cell cell = row.createCell(cellid++);
cell.setCellValue((String)obj);
}
}
FileOutputStream out = new FileOutputStream(
new File(File_Path));
workbook.write(out);
out.close();
System.out.println(File_Path+" is written successfully");
}else {
System.out.println(File_Path+ " is not Found.");
}
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(Addrecord)");
e.printStackTrace();
}
}
public static void Removerecord(String File_Path,String Sheet_Name,Integer row_num) {
CopyExcel(File_Path,File_Path+"_"+datetimenow());
File file= new File(File_Path);
XSSFWorkbook workbook;
try {
workbook = new XSSFWorkbook(new FileInputStream(file));
if (file.isFile() && file.exists()) {
XSSFSheet Sheet = workbook.getSheet(Sheet_Name);
XSSFRow row;
int rowid = row_num-1;
row = Sheet.createRow(rowid);
Sheet.removeRow(row);
FileOutputStream out = new FileOutputStream(
new File(File_Path));
workbook.write(out);
out.close();
System.out.println(File_Path+" is written successfully");
}else {
System.out.println(File_Path+ " is not Found.");
}
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(Removerecord)");
e.printStackTrace();
}
}
public static int rows_num(String File_Path,String Sheet_Name) {
CopyExcel(File_Path,File_Path+"_"+datetimenow());
File file= new File(File_Path);
XSSFWorkbook workbook;
try {
workbook = new XSSFWorkbook(new FileInputStream(file));
if (file.isFile() && file.exists()) {
XSSFSheet Sheet = workbook.getSheet(Sheet_Name);
return Sheet.getLastRowNum()+1;
}else {
System.out.println(File_Path+ " is not Found.");
}
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(rows_num)");
e.printStackTrace();
}
return 0;
}
public static int columns_num(String File_Path,String Sheet_Name) {
CopyExcel(File_Path,File_Path+"_"+datetimenow());
File file= new File(File_Path);
XSSFWorkbook workbook;
try {
workbook = new XSSFWorkbook(new FileInputStream(file));
if (file.isFile() && file.exists()) {
XSSFSheet Sheet = workbook.getSheet(Sheet_Name);
XSSFRow row = Sheet.getRow(0);
return row.getLastCellNum();
}else {
System.out.println(File_Path+ " is not Found.");
}
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(columns_num)");
e.printStackTrace();
}
return 0;
}
public static void editcell(String File_Path,String Sheet_Name ,Integer row_num,Integer column_num,Object cell_content) {
CopyExcel(File_Path,File_Path+"_"+datetimenow());
File file= new File(File_Path);
XSSFWorkbook workbook;
try {
workbook = new XSSFWorkbook(new FileInputStream(file));
if (file.isFile() && file.exists()) {
XSSFSheet Sheet = workbook.getSheet(Sheet_Name);
XSSFRow row;
int rowid = row_num-1;
row = Sheet.getRow(rowid);
int cellid = column_num-1;
Cell cell = row.createCell(cellid);
if(cell_content instanceof String || cell_content instanceof Integer)
if (cell_content instanceof Integer)
cell.setCellValue(Integer.valueOf(String.valueOf(cell_content)));
else
cell.setCellValue(String.valueOf(cell_content));
else if(cell_content instanceof Date)
cell.setCellValue((Date)cell_content);
else if (cell_content instanceof Number)
cell.setCellValue(Double.valueOf(String.valueOf(cell_content)));
FileOutputStream out = new FileOutputStream(
new File(File_Path));
workbook.write(out);
out.close();
System.out.println(File_Path+" is written successfully");
}else {
System.out.println(File_Path+ " is not Found.");
}
} catch (Exception e) {
System.out.println("\nE : Excel_Methods(editcell)");
e.printStackTrace();
}
}
public static String datetimenow() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
return dtf.format(LocalDateTime.now());
}
}