1
1
package io .github .millij .poi .ss .writer ;
2
2
3
+ import java .io .File ;
3
4
import java .io .IOException ;
4
5
import java .util .List ;
5
6
import java .util .Map ;
6
7
8
+ import org .apache .logging .log4j .util .Strings ;
9
+
7
10
8
11
/**
9
12
* Representation of a Spreadsheet Writer. To write data to files, create a new Instance of {@link SpreadsheetWriter},
13
16
*/
14
17
public interface SpreadsheetWriter {
15
18
19
+ //
20
+ // Add Sheet :: Beans
21
+
16
22
/**
17
- * This method will attempt to add a new sheet and add the beans a rows of data. To write the data to a file, call
18
- * {@link #write(String)} method once the data addition is completed.
23
+ * Add a new sheet and add the beans a rows of data. To write the data to a file, call {@link #write(String)} method
24
+ * once the data addition is completed.
19
25
*
20
26
* <br/>
21
27
* <b>Sheet Name</b> : default sheet name will be used
@@ -31,8 +37,8 @@ default <T> void addSheet(Class<T> beanClz, List<T> beans) {
31
37
}
32
38
33
39
/**
34
- * This method will attempt to add a new sheet and add the beans a rows of data. To write the data to a file, call
35
- * {@link #write(String)} method once the data addition is completed.
40
+ * Add a new sheet and add the beans a rows of data. To write the data to a file, call {@link #write(String)} method
41
+ * once the data addition is completed.
36
42
*
37
43
* <br/>
38
44
* <b>Headers</b> : all possible properties as defined by the Type will be used as headers
@@ -46,8 +52,8 @@ default <T> void addSheet(Class<T> beanClz, List<T> beans, String sheetName) {
46
52
}
47
53
48
54
/**
49
- * This method will attempt to add a new sheet and add the beans a rows of data. To write the data to a file, call
50
- * {@link #write(String)} method once the data addition is completed.
55
+ * Add a new sheet and add the beans a rows of data. To write the data to a file, call {@link #write(String)} method
56
+ * once the data addition is completed.
51
57
*
52
58
* <br/>
53
59
* <b>Sheet Name</b> : default sheet name will be used
@@ -61,9 +67,10 @@ default <T> void addSheet(Class<T> beanClz, List<T> beans, List<String> headers)
61
67
this .addSheet (beanClz , beans , null , headers );
62
68
}
63
69
70
+
64
71
/**
65
- * This method will attempt to add a new sheet and add the beans a rows of data. To write the data to a file, call
66
- * {@link #write(String)} method once the data addition is completed.
72
+ * Add a new sheet and add the beans a rows of data. To write the data to a file, call {@link #write(String)} method
73
+ * once the data addition is completed.
67
74
*
68
75
* @param beanClz The Class type to serialize the rows data
69
76
* @param beans List of Data beans of the parameterized type
@@ -74,27 +81,58 @@ default <T> void addSheet(Class<T> beanClz, List<T> beans, List<String> headers)
74
81
<T > void addSheet (Class <T > beanClz , List <T > beans , String sheetName , List <String > headers );
75
82
76
83
84
+ //
85
+ // Add Sheet :: Map<String, Object>
86
+
87
+ /**
88
+ * Add a new sheet and add the rows of data defined by a {@link Map}. The <code>null</code> entry in the rows data
89
+ * list will be skipped and no rows will be added to the sheet.
90
+ *
91
+ * @param rowsData List of Rows defined by a {@link Map}s where the map key is header and value is the row value
92
+ * @param headers a {@link List} of Header names to write in the file
93
+ */
94
+ default void addSheet (List <Map <String , Object >> rowsData , List <String > headers ) {
95
+ this .addSheet (rowsData , null , headers );
96
+ }
97
+
77
98
/**
78
- * This method will attempt to add a new sheet and add the rows of data from the rows data. The
79
- * <code>null</code> entry in the rows data list will be skipped and no rows will be added to the
80
- * sheet.
99
+ * Add a new sheet and add the rows of data defined by a {@link Map}. The <code>null</code> entry in the rows data
100
+ * list will be skipped and no rows will be added to the sheet.
81
101
*
102
+ * @param rowsData List of Rows defined by a {@link Map}s where the map key is header and value is the row value
82
103
* @param sheetName Name of the Sheet. (set it to <code>null</code> for default name)
83
- * @param headers a {@link List} of Header names to write in the file. <code>null</code> or empty
84
- * list will default to all writable properties.
85
- * @param rowsData List of Map, representing the sheet data. Each Map represents the row data where
86
- * the map elements stores a value for a header as key.
104
+ * @param headers a {@link List} of Header names to write in the file
87
105
*/
88
- void addSheet (String sheetName , List < String > headers , List <Map < String , String >> rowsData );
106
+ void addSheet (List < Map < String , Object >> rowsData , String sheetName , List <String > headers );
89
107
90
108
109
+ //
110
+ // Write
111
+
91
112
/**
92
113
* Writes the current Spreadsheet workbook to a file in the specified path.
93
114
*
94
115
* @param filepath output filepath (including filename).
95
116
*
96
117
* @throws IOException if the filepath is not writable.
97
118
*/
98
- void write (String filepath ) throws IOException ;
119
+ default void write (final String filepath ) throws IOException {
120
+ // Sanity checks
121
+ if (Strings .isBlank (filepath )) {
122
+ throw new IllegalArgumentException ("#write :: Input File Path is BLANK" );
123
+ }
124
+
125
+ this .write (new File (filepath ));
126
+ }
127
+
128
+ /**
129
+ * Writes the current Spreadsheet workbook to a file
130
+ *
131
+ * @param fiel output file
132
+ *
133
+ * @throws IOException if the file is not writable.
134
+ */
135
+ void write (File file ) throws IOException ;
136
+
99
137
100
138
}
0 commit comments