11package com .baeldung .excel ;
22
33import 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 ;
4+ import org .apache .poi .xssf .usermodel .XSSFCell ;
5+ import org .apache .poi .xssf .usermodel .XSSFCellStyle ;
6+ import org .apache .poi .xssf .usermodel .XSSFColor ;
117import org .apache .poi .xssf .usermodel .XSSFFont ;
8+ import org .apache .poi .xssf .usermodel .XSSFRow ;
9+ import org .apache .poi .xssf .usermodel .XSSFSheet ;
10+ import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
11+ import org .apache .poi .hssf .usermodel .HSSFCell ;
12+ import org .apache .poi .hssf .usermodel .HSSFCellStyle ;
13+ import org .apache .poi .hssf .usermodel .HSSFFont ;
14+ import org .apache .poi .hssf .usermodel .HSSFRow ;
15+ import org .apache .poi .hssf .usermodel .HSSFSheet ;
16+ import org .apache .poi .hssf .usermodel .HSSFWorkbook ;
17+ import org .apache .poi .hssf .util .HSSFColor ;
1218import org .apache .poi .ss .usermodel .DateUtil ;
13- import org . apache . poi . ss . usermodel . FillPatternType ;
19+
1420import java .io .File ;
15- import java .io .FileOutputStream ;
1621import java .io .FileInputStream ;
1722import java .io .IOException ;
18- import java .io .InputStream ;
1923import java .util .Map ;
2024import java .util .HashMap ;
2125import java .util .ArrayList ;
22- import org . springframework . stereotype . Service ;
26+ import java . util . List ;
2327
24- @ Service
2528public class ExcelPOIHelper {
2629
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 ();
30+ public Map <Integer , List <MyCell >> readExcel (String fileLocation ) throws IOException {
7531
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 ();
32+ Map <Integer , List <MyCell >> data = new HashMap <>();
33+ FileInputStream fis = new FileInputStream (new File (fileLocation ));
8434
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 );
35+ if (fileLocation .endsWith (".xls" )) {
36+ data = readHSSFWorkbook (fis );
37+ } else if (fileLocation .endsWith (".xlsx" )) {
38+ data = readXSSFWorkbook (fis );
39+ }
9740
98- headerCell = header .createCell (1 );
99- headerCell .setCellValue ("Age" );
100- headerCell .setCellStyle (headerStyle );
41+ int maxNrCols = 0 ;
10142
102- CellStyle style = workbook .createCellStyle ();
103- style .setWrapText (true );
43+ for (List <MyCell > ls : data .values ()) {
44+ if (ls .size () > maxNrCols ) {
45+ maxNrCols = ls .size ();
46+ }
47+ }
10448
105- Row row = sheet .createRow (2 );
106- Cell cell = row .createCell (0 );
107- cell .setCellValue ("John Smith" );
108- cell .setCellStyle (style );
49+ for (List <MyCell > ls : data .values ()) {
50+ if (ls .size () < maxNrCols ) {
51+ for (int i = ls .size (); i < maxNrCols ; i ++) {
52+ ls .add (new MyCell ("" ));
53+ }
54+ }
55+ }
10956
110- cell = row .createCell (1 );
111- cell .setCellValue (20 );
112- cell .setCellStyle (style );
57+ return data ;
58+ }
11359
114- row = sheet .createRow (3 );
115- cell = row .createCell (0 );
116- cell .setCellValue ("Ana Johnson" );
117- cell .setCellStyle (style );
60+ private String readCellContent (Cell cell ) {
61+ String content = "" ;
62+ switch (cell .getCellTypeEnum ()) {
63+ case STRING :
64+ content = cell .getStringCellValue ();
65+ break ;
66+ case NUMERIC :
67+ if (DateUtil .isCellDateFormatted (cell )) {
68+ content = cell .getDateCellValue () + "" ;
69+ } else {
70+ content = cell .getNumericCellValue () + "" ;
71+ }
72+ break ;
73+ case BOOLEAN :
74+ content = cell .getBooleanCellValue () + "" ;
75+ break ;
76+ case FORMULA :
77+ content = cell .getCellFormula () + "" ;
78+ break ;
79+ default :
80+ content = "" ;
81+ }
82+ return content ;
83+ }
11884
119- cell = row .createCell (1 );
120- cell .setCellValue (30 );
121- cell .setCellStyle (style );
85+ private Map <Integer , List <MyCell >> readHSSFWorkbook (FileInputStream fis ) throws IOException {
86+ Map <Integer , List <MyCell >> data = new HashMap <>();
87+ HSSFWorkbook workbook = null ;
88+ try {
89+ workbook = new HSSFWorkbook (fis );
90+
91+ HSSFSheet sheet = workbook .getSheetAt (0 );
92+ for (int i = sheet .getFirstRowNum (); i <= sheet .getLastRowNum (); i ++) {
93+ HSSFRow row = sheet .getRow (i );
94+ data .put (i , new ArrayList <MyCell >());
95+ if (row != null ) {
96+ for (int j = 0 ; j < row .getLastCellNum (); j ++) {
97+ HSSFCell cell = row .getCell (j );
98+ if (cell != null ) {
99+ HSSFCellStyle cellStyle = cell .getCellStyle ();
100+
101+ MyCell myCell = new MyCell ();
102+
103+ HSSFColor bgColor = cellStyle .getFillForegroundColorColor ();
104+ if (bgColor != null ) {
105+ short [] rgbColor = bgColor .getTriplet ();
106+ myCell .setBgColor ("rgb(" + rgbColor [0 ] + "," + rgbColor [1 ] + "," + rgbColor [2 ] + ")" );
107+ }
108+ HSSFFont font = cell .getCellStyle ()
109+ .getFont (workbook );
110+ myCell .setTextSize (font .getFontHeightInPoints () + "" );
111+ if (font .getBold ()) {
112+ myCell .setTextWeight ("bold" );
113+ }
114+ HSSFColor textColor = font .getHSSFColor (workbook );
115+ if (textColor != null ) {
116+ short [] rgbColor = textColor .getTriplet ();
117+ myCell .setTextColor ("rgb(" + rgbColor [0 ] + "," + rgbColor [1 ] + "," + rgbColor [2 ] + ")" );
118+ }
119+ myCell .setContent (readCellContent (cell ));
120+ data .get (i )
121+ .add (myCell );
122+ } else {
123+ data .get (i )
124+ .add (new MyCell ("" ));
125+ }
126+ }
127+ }
128+ }
129+ } finally {
130+ if (workbook != null ) {
131+ workbook .close ();
132+ }
133+ }
134+ return data ;
135+ }
122136
123- File currDir = new File ("." );
124- String path = currDir .getAbsolutePath ();
125- String fileLocation = path .substring (0 , path .length () - 1 ) + "temp.xlsx" ;
137+ private Map <Integer , List <MyCell >> readXSSFWorkbook (FileInputStream fis ) throws IOException {
138+ XSSFWorkbook workbook = null ;
139+ Map <Integer , List <MyCell >> data = new HashMap <>();
140+ try {
126141
127- FileOutputStream outputStream = new FileOutputStream (fileLocation );
128- workbook .write (outputStream );
142+ workbook = new XSSFWorkbook (fis );
143+ XSSFSheet sheet = workbook .getSheetAt (0 );
144+
145+ for (int i = sheet .getFirstRowNum (); i <= sheet .getLastRowNum (); i ++) {
146+ XSSFRow row = sheet .getRow (i );
147+ data .put (i , new ArrayList <MyCell >());
148+ if (row != null ) {
149+ for (int j = 0 ; j < row .getLastCellNum (); j ++) {
150+ XSSFCell cell = row .getCell (j );
151+ if (cell != null ) {
152+ XSSFCellStyle cellStyle = cell .getCellStyle ();
153+
154+ MyCell myCell = new MyCell ();
155+ XSSFColor bgColor = cellStyle .getFillForegroundColorColor ();
156+ if (bgColor != null ) {
157+ byte [] rgbColor = bgColor .getRGB ();
158+ myCell .setBgColor ("rgb(" + (rgbColor [0 ] < 0 ? (rgbColor [0 ] + 0xff ) : rgbColor [0 ]) + "," + (rgbColor [1 ] < 0 ? (rgbColor [1 ] + 0xff ) : rgbColor [1 ]) + "," + (rgbColor [2 ] < 0 ? (rgbColor [2 ] + 0xff ) : rgbColor [2 ]) + ")" );
159+ }
160+ XSSFFont font = cellStyle .getFont ();
161+ myCell .setTextSize (font .getFontHeightInPoints () + "" );
162+ if (font .getBold ()) {
163+ myCell .setTextWeight ("bold" );
164+ }
165+ XSSFColor textColor = font .getXSSFColor ();
166+ if (textColor != null ) {
167+ byte [] rgbColor = textColor .getRGB ();
168+ myCell .setTextColor ("rgb(" + (rgbColor [0 ] < 0 ? (rgbColor [0 ] + 0xff ) : rgbColor [0 ]) + "," + (rgbColor [1 ] < 0 ? (rgbColor [1 ] + 0xff ) : rgbColor [1 ]) + "," + (rgbColor [2 ] < 0 ? (rgbColor [2 ] + 0xff ) : rgbColor [2 ]) + ")" );
169+ }
170+ myCell .setContent (readCellContent (cell ));
171+ data .get (i )
172+ .add (myCell );
173+ } else {
174+ data .get (i )
175+ .add (new MyCell ("" ));
176+ }
177+ }
178+ }
179+ }
129180 } finally {
130181 if (workbook != null ) {
131- try {
132- workbook .close ();
133- } catch (IOException e ) {
134- e .printStackTrace ();
135- }
182+ workbook .close ();
136183 }
137184 }
185+ return data ;
138186 }
139187
140188}
0 commit comments