1
1
package cn .netbuffer .jfinal_bootstrap_table .util ;
2
2
3
+ import lombok .extern .slf4j .Slf4j ;
3
4
import org .apache .poi .hssf .usermodel .HSSFWorkbook ;
4
5
import org .apache .poi .ss .usermodel .*;
5
6
7
+ import java .io .File ;
8
+ import java .io .FileInputStream ;
6
9
import java .io .FileOutputStream ;
7
10
import java .io .IOException ;
8
- import java .util .Iterator ;
9
- import java .util .List ;
10
- import java .util .Map ;
11
- import java .util .Set ;
11
+ import java .util .*;
12
12
13
+ @ Slf4j
13
14
public class POIExcelUtil {
14
15
15
- public static void export (List <String > titles , List <Map <String , Object >> datas , String path ) {
16
+ public static List <List <String >> read (String path ) {
17
+ Workbook workbook = null ;
18
+ try {
19
+ workbook = new HSSFWorkbook (new FileInputStream (new File (path )));
20
+ } catch (IOException e ) {
21
+ log .error (e .getMessage ());
22
+ }
23
+ if (workbook == null ) {
24
+ return null ;
25
+ }
26
+ // for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
27
+ // }
28
+ Sheet sheet = workbook .getSheetAt (0 );
29
+ int firstRowNum = sheet .getFirstRowNum ();
30
+ Row firstRow = sheet .getRow (firstRowNum );
31
+ if (null == firstRow ) {
32
+ log .warn ("解析Excel失败,在第一行没有读取到任何数据!" );
33
+ }
34
+ // 解析每一行的数据,构造数据对象
35
+ int rowStart = firstRowNum + 1 ;
36
+ int rowEnd = sheet .getPhysicalNumberOfRows ();
37
+ List <List <String >> data = new ArrayList <>();
38
+ for (int rowNum = rowStart ; rowNum < rowEnd ; rowNum ++) {
39
+ Row row = sheet .getRow (rowNum );
40
+ if (null == row ) {
41
+ continue ;
42
+ }
43
+ int cells = row .getPhysicalNumberOfCells ();
44
+ List <String > arr = new ArrayList <>(cells );
45
+ for (int i = 0 ; i < cells ; i ++) {
46
+ arr .add (row .getCell (i ).getStringCellValue ());
47
+ }
48
+ data .add (arr );
49
+ }
50
+ return data ;
51
+ }
52
+
53
+ public static void write (List <String > titles , List <Map <String , Object >> datas , String path ) {
16
54
Workbook wb = new HSSFWorkbook ();
17
55
Sheet sheet = wb .createSheet (titles .get (0 ));
18
56
// 冻结该行,使其无法移动
@@ -30,17 +68,17 @@ public static void export(List<String> titles, List<Map<String, Object>> datas,
30
68
titleFont .setBold (true );
31
69
titleFont .setColor (IndexedColors .AQUA .getIndex ());
32
70
CellStyle cellStyle = wb .createCellStyle ();
33
- cellStyle .setAlignment (CellStyle . ALIGN_CENTER );
34
- cellStyle .setVerticalAlignment (CellStyle . VERTICAL_CENTER );
71
+ cellStyle .setAlignment (HorizontalAlignment . CENTER );
72
+ cellStyle .setVerticalAlignment (VerticalAlignment . CENTER );
35
73
cellStyle .setFont (titleFont );
36
74
for (int i = 0 ; i < titleCount ; i ++) {
37
75
Cell cell = row .createCell (i );
38
76
cell .setCellValue (titles .get (i ));
39
77
cell .setCellStyle (cellStyle );
40
78
}
41
79
CellStyle contentStyle = wb .createCellStyle ();
42
- cellStyle .setAlignment (CellStyle . ALIGN_CENTER );
43
- cellStyle .setVerticalAlignment (CellStyle . VERTICAL_CENTER );
80
+ cellStyle .setAlignment (HorizontalAlignment . CENTER );
81
+ cellStyle .setVerticalAlignment (VerticalAlignment . CENTER );
44
82
int dataCount = datas .size ();
45
83
for (int i = 1 ; i < dataCount + 1 ; i ++) {
46
84
Row rowIndex = sheet .createRow ((short ) i );
0 commit comments