1+ package com .baeldung .excel ;
2+
3+ import org .apache .poi .ss .usermodel .Cell ;
4+ import org .apache .poi .ss .usermodel .CellType ;
5+ import org .apache .poi .ss .usermodel .CellStyle ;
6+ import org .apache .poi .ss .usermodel .IndexedColors ;
7+ import org .apache .poi .ss .usermodel .Row ;
8+ import org .apache .poi .ss .usermodel .Sheet ;
9+ import org .apache .poi .ss .usermodel .Workbook ;
10+ import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
11+ import org .apache .poi .xssf .usermodel .XSSFFont ;
12+ import org .apache .poi .ss .usermodel .DateUtil ;
13+ import org .apache .poi .ss .usermodel .FillPatternType ;
14+ import java .io .File ;
15+ import java .io .FileOutputStream ;
16+ import java .io .FileInputStream ;
17+ import java .io .IOException ;
18+ import java .io .InputStream ;
19+ import java .util .Map ;
20+ import java .util .HashMap ;
21+ import java .util .ArrayList ;
22+ import org .springframework .stereotype .Service ;
23+
24+ @ Service
25+ public class ExcelPOIHelper {
26+
27+ public Map <Integer , ArrayList <String >> readExcel (String fileLocation ) throws IOException {
28+
29+ Map <Integer , ArrayList <String >> data = new HashMap <Integer , ArrayList <String >>();
30+ FileInputStream file = new FileInputStream (new File (fileLocation ));
31+ Workbook workbook = new XSSFWorkbook (file );
32+ Sheet sheet = workbook .getSheetAt (0 );
33+ int i = 0 ;
34+ for (Row row : sheet ) {
35+ data .put (i , new ArrayList <String >());
36+ for (Cell cell : row ) {
37+ switch (cell .getCellTypeEnum ()) {
38+ case STRING :
39+ data .get (i )
40+ .add (cell .getRichStringCellValue ()
41+ .getString ());
42+ break ;
43+ case NUMERIC :
44+ if (DateUtil .isCellDateFormatted (cell )) {
45+ data .get (i )
46+ .add (cell .getDateCellValue () + "" );
47+ } else {
48+ data .get (i )
49+ .add (cell .getNumericCellValue () + "" );
50+ }
51+ break ;
52+ case BOOLEAN :
53+ data .get (i )
54+ .add (cell .getBooleanCellValue () + "" );
55+ break ;
56+ case FORMULA :
57+ data .get (i )
58+ .add (cell .getCellFormula () + "" );
59+ break ;
60+ default :
61+ data .get (i )
62+ .add (" " );
63+ }
64+ }
65+ i ++;
66+ }
67+ if (workbook != null ){
68+ workbook .close ();
69+ }
70+ return data ;
71+ }
72+
73+ public void writeExcel () throws IOException {
74+ Workbook workbook = new XSSFWorkbook ();
75+
76+ try {
77+ Sheet sheet = workbook .createSheet ("Persons" );
78+ sheet .setColumnWidth (0 , 6000 );
79+ sheet .setColumnWidth (1 , 4000 );
80+
81+ Row header = sheet .createRow (0 );
82+
83+ CellStyle headerStyle = workbook .createCellStyle ();
84+
85+ headerStyle .setFillForegroundColor (IndexedColors .LIGHT_BLUE .getIndex ());
86+ headerStyle .setFillPattern (FillPatternType .SOLID_FOREGROUND );
87+
88+ XSSFFont font = ((XSSFWorkbook ) workbook ).createFont ();
89+ font .setFontName ("Arial" );
90+ font .setFontHeightInPoints ((short ) 16 );
91+ font .setBold (true );
92+ headerStyle .setFont (font );
93+
94+ Cell headerCell = header .createCell (0 );
95+ headerCell .setCellValue ("Name" );
96+ headerCell .setCellStyle (headerStyle );
97+
98+ headerCell = header .createCell (1 );
99+ headerCell .setCellValue ("Age" );
100+ headerCell .setCellStyle (headerStyle );
101+
102+ CellStyle style = workbook .createCellStyle ();
103+ style .setWrapText (true );
104+
105+ Row row = sheet .createRow (2 );
106+ Cell cell = row .createCell (0 );
107+ cell .setCellValue ("John Smith" );
108+ cell .setCellStyle (style );
109+
110+ cell = row .createCell (1 );
111+ cell .setCellValue (20 );
112+ cell .setCellStyle (style );
113+
114+ row = sheet .createRow (3 );
115+ cell = row .createCell (0 );
116+ cell .setCellValue ("Ana Johnson" );
117+ cell .setCellStyle (style );
118+
119+ cell = row .createCell (1 );
120+ cell .setCellValue (30 );
121+ cell .setCellStyle (style );
122+
123+ File currDir = new File ("." );
124+ String path = currDir .getAbsolutePath ();
125+ String fileLocation = path .substring (0 , path .length () - 1 ) + "temp.xlsx" ;
126+
127+ FileOutputStream outputStream = new FileOutputStream (fileLocation );
128+ workbook .write (outputStream );
129+ } finally {
130+ if (workbook != null ) {
131+ try {
132+ workbook .close ();
133+ } catch (IOException e ) {
134+ e .printStackTrace ();
135+ }
136+ }
137+ }
138+ }
139+
140+ }
0 commit comments