diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 7d0cc24d41..9513c81064 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -160,6 +160,28 @@ test + + + org.apache.poi + poi + ${poi.version} + + + org.apache.poi + poi-ooxml + ${poi.version} + + + org.apache.poi + poi-ooxml-schemas + ${poi.version} + + + org.jxls + jxls-jexcel + ${jexcel.version} + + @@ -370,6 +392,10 @@ 1.8.9 + + + 3.16-beta1 + 1.0.6 diff --git a/spring-mvc-java/src/main/java/com/baeldung/excel/ExcelPOIHelper.java b/spring-mvc-java/src/main/java/com/baeldung/excel/ExcelPOIHelper.java new file mode 100644 index 0000000000..5cf74aff63 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/excel/ExcelPOIHelper.java @@ -0,0 +1,140 @@ +package com.baeldung.excel; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFFont; +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.FillPatternType; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; +import org.springframework.stereotype.Service; + +@Service +public class ExcelPOIHelper { + + public Map> readExcel(String fileLocation) throws IOException { + + Map> data = new HashMap>(); + FileInputStream file = new FileInputStream(new File(fileLocation)); + Workbook workbook = new XSSFWorkbook(file); + Sheet sheet = workbook.getSheetAt(0); + int i = 0; + for (Row row : sheet) { + data.put(i, new ArrayList()); + for (Cell cell : row) { + switch (cell.getCellTypeEnum()) { + case STRING: + data.get(i) + .add(cell.getRichStringCellValue() + .getString()); + break; + case NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + data.get(i) + .add(cell.getDateCellValue() + ""); + } else { + data.get(i) + .add(cell.getNumericCellValue() + ""); + } + break; + case BOOLEAN: + data.get(i) + .add(cell.getBooleanCellValue() + ""); + break; + case FORMULA: + data.get(i) + .add(cell.getCellFormula() + ""); + break; + default: + data.get(i) + .add(" "); + } + } + i++; + } + if (workbook != null){ + workbook.close(); + } + return data; + } + + public void writeExcel() throws IOException { + Workbook workbook = new XSSFWorkbook(); + + try { + Sheet sheet = workbook.createSheet("Persons"); + sheet.setColumnWidth(0, 6000); + sheet.setColumnWidth(1, 4000); + + Row header = sheet.createRow(0); + + CellStyle headerStyle = workbook.createCellStyle(); + + headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); + headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + + XSSFFont font = ((XSSFWorkbook) workbook).createFont(); + font.setFontName("Arial"); + font.setFontHeightInPoints((short) 16); + font.setBold(true); + headerStyle.setFont(font); + + Cell headerCell = header.createCell(0); + headerCell.setCellValue("Name"); + headerCell.setCellStyle(headerStyle); + + headerCell = header.createCell(1); + headerCell.setCellValue("Age"); + headerCell.setCellStyle(headerStyle); + + CellStyle style = workbook.createCellStyle(); + style.setWrapText(true); + + Row row = sheet.createRow(2); + Cell cell = row.createCell(0); + cell.setCellValue("John Smith"); + cell.setCellStyle(style); + + cell = row.createCell(1); + cell.setCellValue(20); + cell.setCellStyle(style); + + row = sheet.createRow(3); + cell = row.createCell(0); + cell.setCellValue("Ana Johnson"); + cell.setCellStyle(style); + + cell = row.createCell(1); + cell.setCellValue(30); + cell.setCellStyle(style); + + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + String fileLocation = path.substring(0, path.length() - 1) + "temp.xlsx"; + + FileOutputStream outputStream = new FileOutputStream(fileLocation); + workbook.write(outputStream); + } finally { + if (workbook != null) { + try { + workbook.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/excel/JExcelHelper.java b/spring-mvc-java/src/main/java/com/baeldung/excel/JExcelHelper.java new file mode 100644 index 0000000000..eb65987e8b --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/excel/JExcelHelper.java @@ -0,0 +1,80 @@ +package com.baeldung.excel; + +import jxl.*; +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; +import jxl.read.biff.BiffException; +import java.io.File; +import java.io.IOException; +import org.springframework.stereotype.Service; +import jxl.write.*; +import jxl.write.Number; +import jxl.format.Colour; + +@Service +public class JExcelHelper { + + public Map> readJExcel(String fileLocation) throws IOException, BiffException { + Map> data = new HashMap>(); + + Workbook workbook = Workbook.getWorkbook(new File(fileLocation)); + Sheet sheet = workbook.getSheet(0); + int rows = sheet.getRows(); + int columns = sheet.getColumns(); + + for (int i = 0; i < rows; i++) { + data.put(i, new ArrayList()); + for (int j = 0; j < columns; j++) { + data.get(i).add(sheet.getCell(j, i).getContents()); + } + } + return data; + } + + public void writeJExcel() throws IOException, WriteException { + WritableWorkbook workbook = null; + try { + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + String fileLocation = path.substring(0, path.length() - 1) + "temp.xls"; + + workbook = Workbook.createWorkbook(new File(fileLocation)); + + WritableSheet sheet = workbook.createSheet("Sheet 1", 0); + + WritableCellFormat headerFormat = new WritableCellFormat(); + WritableFont font = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD); + headerFormat.setFont(font); + headerFormat.setBackground(Colour.LIGHT_BLUE); + headerFormat.setWrap(true); + Label headerLabel = new Label(0, 0, "Name", headerFormat); + sheet.setColumnView(0, 60); + sheet.addCell(headerLabel); + + headerLabel = new Label(1, 0, "Age", headerFormat); + sheet.setColumnView(0, 40); + sheet.addCell(headerLabel); + + WritableCellFormat cellFormat = new WritableCellFormat(); + cellFormat.setWrap(true); + + Label cellLabel = new Label(0, 2, "John Smith", cellFormat); + sheet.addCell(cellLabel); + Number cellNumber = new Number(1, 2, 20, cellFormat); + sheet.addCell(cellNumber); + + cellLabel = new Label(0, 3, "Ana Johnson", cellFormat); + sheet.addCell(cellLabel); + cellNumber = new Number(1, 3, 30, cellFormat); + sheet.addCell(cellNumber); + + workbook.write(); + } finally { + if (workbook != null) { + workbook.close(); + } + } + + } +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java index 663b9cc4d2..9578303554 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -21,6 +21,7 @@ import org.springframework.web.servlet.view.JstlView; import org.springframework.web.servlet.view.ResourceBundleViewResolver; import org.springframework.web.servlet.view.XmlViewResolver; import org.springframework.web.util.UrlPathHelper; +import com.baeldung.excel.*; @Configuration @EnableWebMvc @@ -106,4 +107,15 @@ public class WebConfig extends WebMvcConfigurerAdapter { configurer.setUrlPathHelper(urlPathHelper); } + + @Bean + public JExcelHelper jExcelHelper() { + return new JExcelHelper(); + } + + @Bean + public ExcelPOIHelper excelPOIHelper() { + return new ExcelPOIHelper(); + } + } diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/ExcelController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/ExcelController.java new file mode 100644 index 0000000000..810282dd65 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/ExcelController.java @@ -0,0 +1,105 @@ +package com.baeldung.web.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.multipart.MultipartFile; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import com.baeldung.excel.*; +import jxl.read.biff.BiffException; +import java.util.Map; +import java.util.ArrayList; +import javax.annotation.Resource; +import jxl.write.WriteException; + +@Controller +public class ExcelController { + + private String fileLocation; + + @Resource(name = "jExcelHelper") + private JExcelHelper jExcelHelper; + + @Resource(name = "excelPOIHelper") + private ExcelPOIHelper excelPOIHelper; + + @RequestMapping(method = RequestMethod.GET, value = "/excelProcessing") + public String getExcelProcessingPage() { + return "excel"; + } + + @RequestMapping(method = RequestMethod.POST, value = "/uploadExcelFile") + public String uploadFile(Model model, MultipartFile file) throws IOException { + InputStream in = file.getInputStream(); + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + fileLocation = path.substring(0, path.length() - 1) + file.getOriginalFilename(); + FileOutputStream f = new FileOutputStream(fileLocation); + int ch = 0; + while ((ch = in.read()) != -1) { + f.write(ch); + } + f.flush(); + f.close(); + System.out.println(fileLocation); + model.addAttribute("message", "File: " + file.getOriginalFilename() + " has been uploaded successfully!"); + return "excel"; + } + + @RequestMapping(method = RequestMethod.GET, value = "/readJExcel") + public String readJExcel(Model model) throws IOException, BiffException { + + if (fileLocation != null) { + if (fileLocation.endsWith(".xls")) { + Map> data = jExcelHelper.readJExcel(fileLocation); + model.addAttribute("data", data); + } else { + model.addAttribute("message", "Not a valid .xls file!"); + } + } else { + model.addAttribute("message", "File missing! Please upload an excel file."); + } + return "excel"; + } + + @RequestMapping(method = RequestMethod.GET, value = "/readPOI") + public String readPOI(Model model) throws IOException { + + if (fileLocation != null) { + if (fileLocation.endsWith(".xlsx")) { + Map> data = excelPOIHelper.readExcel(fileLocation); + model.addAttribute("data", data); + } else { + model.addAttribute("message", "Not a valid .xlsx file!"); + } + } else { + model.addAttribute("message", "File missing! Please upload an excel file."); + } + return "excel"; + } + + @RequestMapping(method = RequestMethod.POST, value = "/writeJExcel") + public String writeJExcel(Model model) throws IOException, BiffException, WriteException { + + jExcelHelper.writeJExcel(); + + model.addAttribute("message", "Write successful!"); + + return "excel"; + } + + @RequestMapping(method = RequestMethod.POST, value = "/writePOI") + public String writePOI(Model model) throws IOException { + + excelPOIHelper.writeExcel(); + + model.addAttribute("message", "Write successful!"); + + return "excel"; + } + +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/excel.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/excel.jsp new file mode 100644 index 0000000000..b50687df15 --- /dev/null +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/excel.jsp @@ -0,0 +1,56 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + + + + +Excel Processing + + + + + + + + + +
+ + +
+
+${message } +

+
+ +
+

+Read file using JExcel     +Read file using Apache POI +

+ +File content: + + + + + + + + + +
${cell}
+
+

+
+ +
+
+
+ +
+ + + + \ No newline at end of file