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