Skip to content

Commit a44ea95

Browse files
lor6KevinGilmore
authored andcommitted
excel processing using jexcel and poi (eugenp#1058)
* excel processing using jexcel and poi * update examples * small fix * fix poi version, fix imports
1 parent a1312e9 commit a44ea95

6 files changed

Lines changed: 419 additions & 0 deletions

File tree

‎spring-mvc-java/pom.xml‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,28 @@
160160
<scope>test</scope>
161161
</dependency>
162162

163+
<!-- excel -->
164+
<dependency>
165+
<groupId>org.apache.poi</groupId>
166+
<artifactId>poi</artifactId>
167+
<version>${poi.version}</version>
168+
</dependency>
169+
<dependency>
170+
<groupId>org.apache.poi</groupId>
171+
<artifactId>poi-ooxml</artifactId>
172+
<version>${poi.version}</version>
173+
</dependency>
174+
<dependency>
175+
<groupId>org.apache.poi</groupId>
176+
<artifactId>poi-ooxml-schemas</artifactId>
177+
<version>${poi.version}</version>
178+
</dependency>
179+
<dependency>
180+
<groupId>org.jxls</groupId>
181+
<artifactId>jxls-jexcel</artifactId>
182+
<version>${jexcel.version}</version>
183+
</dependency>
184+
163185
</dependencies>
164186

165187
<build>
@@ -370,6 +392,10 @@
370392

371393
<!-- AspectJ -->
372394
<aspectj.version>1.8.9</aspectj.version>
395+
396+
<!-- excel -->
397+
<poi.version>3.16-beta1</poi.version>
398+
<jexcel.version>1.0.6</jexcel.version>
373399
</properties>
374400

375401
</project>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.excel;
2+
3+
import jxl.*;
4+
import java.util.Map;
5+
import java.util.HashMap;
6+
import java.util.ArrayList;
7+
import jxl.read.biff.BiffException;
8+
import java.io.File;
9+
import java.io.IOException;
10+
import org.springframework.stereotype.Service;
11+
import jxl.write.*;
12+
import jxl.write.Number;
13+
import jxl.format.Colour;
14+
15+
@Service
16+
public class JExcelHelper {
17+
18+
public Map<Integer, ArrayList<String>> readJExcel(String fileLocation) throws IOException, BiffException {
19+
Map<Integer, ArrayList<String>> data = new HashMap<Integer, ArrayList<String>>();
20+
21+
Workbook workbook = Workbook.getWorkbook(new File(fileLocation));
22+
Sheet sheet = workbook.getSheet(0);
23+
int rows = sheet.getRows();
24+
int columns = sheet.getColumns();
25+
26+
for (int i = 0; i < rows; i++) {
27+
data.put(i, new ArrayList<String>());
28+
for (int j = 0; j < columns; j++) {
29+
data.get(i).add(sheet.getCell(j, i).getContents());
30+
}
31+
}
32+
return data;
33+
}
34+
35+
public void writeJExcel() throws IOException, WriteException {
36+
WritableWorkbook workbook = null;
37+
try {
38+
File currDir = new File(".");
39+
String path = currDir.getAbsolutePath();
40+
String fileLocation = path.substring(0, path.length() - 1) + "temp.xls";
41+
42+
workbook = Workbook.createWorkbook(new File(fileLocation));
43+
44+
WritableSheet sheet = workbook.createSheet("Sheet 1", 0);
45+
46+
WritableCellFormat headerFormat = new WritableCellFormat();
47+
WritableFont font = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD);
48+
headerFormat.setFont(font);
49+
headerFormat.setBackground(Colour.LIGHT_BLUE);
50+
headerFormat.setWrap(true);
51+
Label headerLabel = new Label(0, 0, "Name", headerFormat);
52+
sheet.setColumnView(0, 60);
53+
sheet.addCell(headerLabel);
54+
55+
headerLabel = new Label(1, 0, "Age", headerFormat);
56+
sheet.setColumnView(0, 40);
57+
sheet.addCell(headerLabel);
58+
59+
WritableCellFormat cellFormat = new WritableCellFormat();
60+
cellFormat.setWrap(true);
61+
62+
Label cellLabel = new Label(0, 2, "John Smith", cellFormat);
63+
sheet.addCell(cellLabel);
64+
Number cellNumber = new Number(1, 2, 20, cellFormat);
65+
sheet.addCell(cellNumber);
66+
67+
cellLabel = new Label(0, 3, "Ana Johnson", cellFormat);
68+
sheet.addCell(cellLabel);
69+
cellNumber = new Number(1, 3, 30, cellFormat);
70+
sheet.addCell(cellNumber);
71+
72+
workbook.write();
73+
} finally {
74+
if (workbook != null) {
75+
workbook.close();
76+
}
77+
}
78+
79+
}
80+
}

‎spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
2222
import org.springframework.web.servlet.view.XmlViewResolver;
2323
import org.springframework.web.util.UrlPathHelper;
24+
import com.baeldung.excel.*;
2425

2526
@Configuration
2627
@EnableWebMvc
@@ -106,4 +107,15 @@ public void configurePathMatch(final PathMatchConfigurer configurer) {
106107

107108
configurer.setUrlPathHelper(urlPathHelper);
108109
}
110+
111+
@Bean
112+
public JExcelHelper jExcelHelper() {
113+
return new JExcelHelper();
114+
}
115+
116+
@Bean
117+
public ExcelPOIHelper excelPOIHelper() {
118+
return new ExcelPOIHelper();
119+
}
120+
109121
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baeldung.web.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.multipart.MultipartFile;
8+
import java.io.File;
9+
import java.io.FileOutputStream;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import com.baeldung.excel.*;
13+
import jxl.read.biff.BiffException;
14+
import java.util.Map;
15+
import java.util.ArrayList;
16+
import javax.annotation.Resource;
17+
import jxl.write.WriteException;
18+
19+
@Controller
20+
public class ExcelController {
21+
22+
private String fileLocation;
23+
24+
@Resource(name = "jExcelHelper")
25+
private JExcelHelper jExcelHelper;
26+
27+
@Resource(name = "excelPOIHelper")
28+
private ExcelPOIHelper excelPOIHelper;
29+
30+
@RequestMapping(method = RequestMethod.GET, value = "/excelProcessing")
31+
public String getExcelProcessingPage() {
32+
return "excel";
33+
}
34+
35+
@RequestMapping(method = RequestMethod.POST, value = "/uploadExcelFile")
36+
public String uploadFile(Model model, MultipartFile file) throws IOException {
37+
InputStream in = file.getInputStream();
38+
File currDir = new File(".");
39+
String path = currDir.getAbsolutePath();
40+
fileLocation = path.substring(0, path.length() - 1) + file.getOriginalFilename();
41+
FileOutputStream f = new FileOutputStream(fileLocation);
42+
int ch = 0;
43+
while ((ch = in.read()) != -1) {
44+
f.write(ch);
45+
}
46+
f.flush();
47+
f.close();
48+
System.out.println(fileLocation);
49+
model.addAttribute("message", "File: " + file.getOriginalFilename() + " has been uploaded successfully!");
50+
return "excel";
51+
}
52+
53+
@RequestMapping(method = RequestMethod.GET, value = "/readJExcel")
54+
public String readJExcel(Model model) throws IOException, BiffException {
55+
56+
if (fileLocation != null) {
57+
if (fileLocation.endsWith(".xls")) {
58+
Map<Integer, ArrayList<String>> data = jExcelHelper.readJExcel(fileLocation);
59+
model.addAttribute("data", data);
60+
} else {
61+
model.addAttribute("message", "Not a valid .xls file!");
62+
}
63+
} else {
64+
model.addAttribute("message", "File missing! Please upload an excel file.");
65+
}
66+
return "excel";
67+
}
68+
69+
@RequestMapping(method = RequestMethod.GET, value = "/readPOI")
70+
public String readPOI(Model model) throws IOException {
71+
72+
if (fileLocation != null) {
73+
if (fileLocation.endsWith(".xlsx")) {
74+
Map<Integer, ArrayList<String>> data = excelPOIHelper.readExcel(fileLocation);
75+
model.addAttribute("data", data);
76+
} else {
77+
model.addAttribute("message", "Not a valid .xlsx file!");
78+
}
79+
} else {
80+
model.addAttribute("message", "File missing! Please upload an excel file.");
81+
}
82+
return "excel";
83+
}
84+
85+
@RequestMapping(method = RequestMethod.POST, value = "/writeJExcel")
86+
public String writeJExcel(Model model) throws IOException, BiffException, WriteException {
87+
88+
jExcelHelper.writeJExcel();
89+
90+
model.addAttribute("message", "Write successful!");
91+
92+
return "excel";
93+
}
94+
95+
@RequestMapping(method = RequestMethod.POST, value = "/writePOI")
96+
public String writePOI(Model model) throws IOException {
97+
98+
excelPOIHelper.writeExcel();
99+
100+
model.addAttribute("message", "Write successful!");
101+
102+
return "excel";
103+
}
104+
105+
}

0 commit comments

Comments
 (0)