77
88import anhtester .com .exceptions .InvalidPathForExcelException ;
99import anhtester .com .utils .Log ;
10- import org .apache .poi .hssf .usermodel .HSSFWorkbook ;
1110import org .apache .poi .ss .usermodel .*;
1211import org .apache .poi .xssf .usermodel .XSSFCellStyle ;
1312import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
@@ -91,111 +90,52 @@ public Row getRowData(int rowNum) {
9190 row = sheet .getRow (rowNum );
9291 return row ;
9392 }
93+
9494
95- public Object [][] getDataArray (String excelPath , String sheetName , int startCol , int totalCols ) {
96-
95+ public Object [][] getExcelData (String fileName , String sheetName ) {
9796 Object [][] data = null ;
97+ Workbook workbook = null ;
9898 try {
99+ // load the file
100+ FileInputStream fis = new FileInputStream (fileName );
101+ String fileExtensionName = fileName .substring (fileName .indexOf ("." ));
99102
100- File f = new File (excelPath );
101-
102- if (!f .exists ()) {
103- try {
104- Log .info ("File Excel path not found." );
105- throw new InvalidPathForExcelException ("File Excel path not found." );
106- } catch (Exception e ) {
107- e .printStackTrace ();
108- }
109- }
110- if (sheetName .isEmpty ()) {
111- try {
112- Log .info ("The Sheet Name is empty." );
113- throw new InvalidPathForExcelException ("The Sheet Name is empty." );
114- } catch (Exception e ) {
115- e .printStackTrace ();
116- }
117- }
118-
119- fis = new FileInputStream (excelPath );
120-
121- String fileExtensionName = excelPath .substring (excelPath .indexOf ("." ));
122103 // load the workbook
123- if (fileExtensionName .equals (".xlsx" )) workbook = new XSSFWorkbook (fis );
124- else if (fileExtensionName .equals (".xls" )) {
125- workbook = new HSSFWorkbook (fis );
126- }
104+ workbook = new XSSFWorkbook (fis );
127105
128- sheet = workbook .getSheet (sheetName );
106+ // if (fileExtensionName.equals(".xlsx")) workbook = new XSSFWorkbook(fis);
107+ // else if (fileExtensionName.equals(".xls")) {
108+ // workbook = new HSSFWorkbook(fis);
109+ // }
129110
130- if (sheet == null ) {
131- try {
132- Log .info ("Sheet name not found." );
133- throw new InvalidPathForExcelException ("Sheet name not found." );
134- } catch (Exception e ) {
135- e .printStackTrace ();
136- }
137- }
138-
139- int noOfRows = sheet .getPhysicalNumberOfRows ();
140- //int noOfCols = row.getLastCellNum();
141- int noOfCols = totalCols + 1 ;
142-
143- System .out .println ("Số Dòng: " + (noOfRows - 1 ));
144- System .out .println ("Số Cột: " + (noOfCols - startCol ));
145-
146- data = new String [noOfRows - 1 ][noOfCols - startCol ];
147- for (int i = 1 ; i < noOfRows ; i ++) {
148- for (int j = 0 ; j < noOfCols - startCol ; j ++) {
149- data [i - 1 ][j ] = getCellData (i , j + startCol );
150- System .out .println (data [i - 1 ][j ]);
151- }
152- }
153- } catch (Exception e ) {
154- e .getMessage ();
155- Log .error (e .getMessage ());
156- }
157- return data ;
158- }
159-
160- public Object [][] getExcelDataAll (String filePath , String sheetName ) throws Exception {
161-
162- Object [][] data = null ;
163- Workbook wb = null ;
164- try {
165- // load the file
166- FileInputStream fis = new FileInputStream (filePath );
167- String fileExtensionName = filePath .substring (filePath .indexOf ("." ));
111+ // load the sheet
112+ Sheet sheet = workbook .getSheet (sheetName );
168113
169- // load the workbook
170- if (fileExtensionName .equals (".xlsx" )) wb = new XSSFWorkbook (fis );
171- else if (fileExtensionName .equals (".xls" )) {
172- wb = new HSSFWorkbook (fis );
173- }
114+ // load the row
115+ Row row = sheet .getRow (0 );
174116
175- // load the sheet
176- Sheet sh = wb .getSheet (sheetName );
177- Row row = sh .getRow (0 );
178117
179- int noOfRows = sh .getPhysicalNumberOfRows ();
118+ int noOfRows = sheet .getPhysicalNumberOfRows ();
180119 int noOfCols = row .getLastCellNum ();
181120
182121 System .out .println (noOfRows + " - " + noOfCols );
183122
184123 Cell cell ;
185124 data = new Object [noOfRows - 1 ][noOfCols ];
186125
187- //
126+ //Vòng lặp FOR chạy từ 1 để bỏ dòng tiêu đề (dòng tiêu đề là 0)
188127 for (int i = 1 ; i < noOfRows ; i ++) {
189128 for (int j = 0 ; j < noOfCols ; j ++) {
190- row = sh .getRow (i );
129+ row = sheet .getRow (i );
191130 cell = row .getCell (j );
192131
132+ //Này dùng để xác định kiểu dữ liệu từ ô trong excel rồi chuển về String luôn cho tiện đọc
193133 switch (cell .getCellType ()) {
194134 case STRING :
195135 data [i - 1 ][j ] = cell .getStringCellValue ();
196136 break ;
197137 case NUMERIC :
198- data [i - 1 ][j ] = ( int ) cell .getNumericCellValue ();
138+ data [i - 1 ][j ] = String . valueOf ( cell .getNumericCellValue () );
199139 break ;
200140 case BLANK :
201141 data [i - 1 ][j ] = "" ;
@@ -206,9 +146,9 @@ else if (fileExtensionName.equals(".xls")) {
206146 }
207147 }
208148 }
209- System .out .println ("Data in Excel: " + data );
210149 } catch (Exception e ) {
211- System .out .println ("The exception is:" + e .getMessage ());
150+ e .getMessage ();
151+ throw new RuntimeException (e );
212152 }
213153 return data ;
214154 }
@@ -234,11 +174,12 @@ public Object[][] getDataHashTable(String excelPath, String sheetName, int start
234174
235175 String fileExtensionName = excelPath .substring (excelPath .indexOf ("." ));
236176 // load the workbook
237- if (fileExtensionName .equals (".xlsx" )) workbook = new XSSFWorkbook (fis );
238- else if (fileExtensionName .equals (".xls" )) {
239- workbook = new HSSFWorkbook (fis );
240- }
177+ // if (fileExtensionName.equals(".xlsx")) workbook = new XSSFWorkbook(fis);
178+ // else if (fileExtensionName.equals(".xls")) {
179+ // workbook = new HSSFWorkbook(fis);
180+ // }
241181
182+ workbook = new XSSFWorkbook (fis );
242183 sheet = workbook .getSheet (sheetName );
243184
244185 int rows = getRows ();
0 commit comments