[JAVA-28185] (#15367)
* [JAVA-28185] Moved code for article(Introduction to HtmlUnit) to spring-mvc-java-2 * [JAVA-28185] Moved code for article(Upload and Display Excel Files with Spring MVC) to spring-mvc-java-2 * [JAVA-28185] Clean up * [JAVA-28185]
This commit is contained in:
-185
@@ -1,185 +0,0 @@
|
||||
package com.baeldung.excel;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.xssf.usermodel.XSSFCell;
|
||||
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
||||
import org.apache.poi.xssf.usermodel.XSSFColor;
|
||||
import org.apache.poi.xssf.usermodel.XSSFFont;
|
||||
import org.apache.poi.xssf.usermodel.XSSFRow;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.apache.poi.hssf.usermodel.HSSFCell;
|
||||
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
||||
import org.apache.poi.hssf.usermodel.HSSFFont;
|
||||
import org.apache.poi.hssf.usermodel.HSSFRow;
|
||||
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.hssf.util.HSSFColor;
|
||||
import org.apache.poi.ss.usermodel.DateUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class ExcelPOIHelper {
|
||||
|
||||
public Map<Integer, List<MyCell>> readExcel(String fileLocation) throws IOException {
|
||||
|
||||
Map<Integer, List<MyCell>> data = new HashMap<>();
|
||||
FileInputStream fis = new FileInputStream(new File(fileLocation));
|
||||
|
||||
if (fileLocation.endsWith(".xls")) {
|
||||
data = readHSSFWorkbook(fis);
|
||||
} else if (fileLocation.endsWith(".xlsx")) {
|
||||
data = readXSSFWorkbook(fis);
|
||||
}
|
||||
|
||||
int maxNrCols = data.values().stream()
|
||||
.mapToInt(List::size)
|
||||
.max()
|
||||
.orElse(0);
|
||||
|
||||
data.values().stream()
|
||||
.filter(ls -> ls.size() < maxNrCols)
|
||||
.forEach(ls -> {
|
||||
IntStream.range(ls.size(), maxNrCols)
|
||||
.forEach(i -> ls.add(new MyCell("")));
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
private String readCellContent(Cell cell) {
|
||||
String content;
|
||||
switch (cell.getCellTypeEnum()) {
|
||||
case STRING:
|
||||
content = cell.getStringCellValue();
|
||||
break;
|
||||
case NUMERIC:
|
||||
if (DateUtil.isCellDateFormatted(cell)) {
|
||||
content = cell.getDateCellValue() + "";
|
||||
} else {
|
||||
content = cell.getNumericCellValue() + "";
|
||||
}
|
||||
break;
|
||||
case BOOLEAN:
|
||||
content = cell.getBooleanCellValue() + "";
|
||||
break;
|
||||
case FORMULA:
|
||||
content = cell.getCellFormula() + "";
|
||||
break;
|
||||
default:
|
||||
content = "";
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
private Map<Integer, List<MyCell>> readHSSFWorkbook(FileInputStream fis) throws IOException {
|
||||
Map<Integer, List<MyCell>> data = new HashMap<>();
|
||||
HSSFWorkbook workbook = null;
|
||||
try {
|
||||
workbook = new HSSFWorkbook(fis);
|
||||
|
||||
HSSFSheet sheet = workbook.getSheetAt(0);
|
||||
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
|
||||
HSSFRow row = sheet.getRow(i);
|
||||
data.put(i, new ArrayList<>());
|
||||
if (row != null) {
|
||||
for (int j = 0; j < row.getLastCellNum(); j++) {
|
||||
HSSFCell cell = row.getCell(j);
|
||||
if (cell != null) {
|
||||
HSSFCellStyle cellStyle = cell.getCellStyle();
|
||||
|
||||
MyCell myCell = new MyCell();
|
||||
|
||||
HSSFColor bgColor = cellStyle.getFillForegroundColorColor();
|
||||
if (bgColor != null) {
|
||||
short[] rgbColor = bgColor.getTriplet();
|
||||
myCell.setBgColor("rgb(" + rgbColor[0] + "," + rgbColor[1] + "," + rgbColor[2] + ")");
|
||||
}
|
||||
HSSFFont font = cell.getCellStyle()
|
||||
.getFont(workbook);
|
||||
myCell.setTextSize(font.getFontHeightInPoints() + "");
|
||||
if (font.getBold()) {
|
||||
myCell.setTextWeight("bold");
|
||||
}
|
||||
HSSFColor textColor = font.getHSSFColor(workbook);
|
||||
if (textColor != null) {
|
||||
short[] rgbColor = textColor.getTriplet();
|
||||
myCell.setTextColor("rgb(" + rgbColor[0] + "," + rgbColor[1] + "," + rgbColor[2] + ")");
|
||||
}
|
||||
myCell.setContent(readCellContent(cell));
|
||||
data.get(i)
|
||||
.add(myCell);
|
||||
} else {
|
||||
data.get(i)
|
||||
.add(new MyCell(""));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (workbook != null) {
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private Map<Integer, List<MyCell>> readXSSFWorkbook(FileInputStream fis) throws IOException {
|
||||
XSSFWorkbook workbook = null;
|
||||
Map<Integer, List<MyCell>> data = new HashMap<>();
|
||||
try {
|
||||
|
||||
workbook = new XSSFWorkbook(fis);
|
||||
XSSFSheet sheet = workbook.getSheetAt(0);
|
||||
|
||||
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
|
||||
XSSFRow row = sheet.getRow(i);
|
||||
data.put(i, new ArrayList<>());
|
||||
if (row != null) {
|
||||
for (int j = 0; j < row.getLastCellNum(); j++) {
|
||||
XSSFCell cell = row.getCell(j);
|
||||
if (cell != null) {
|
||||
XSSFCellStyle cellStyle = cell.getCellStyle();
|
||||
|
||||
MyCell myCell = new MyCell();
|
||||
XSSFColor bgColor = cellStyle.getFillForegroundColorColor();
|
||||
if (bgColor != null) {
|
||||
byte[] rgbColor = bgColor.getRGB();
|
||||
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]) + ")");
|
||||
}
|
||||
XSSFFont font = cellStyle.getFont();
|
||||
myCell.setTextSize(font.getFontHeightInPoints() + "");
|
||||
if (font.getBold()) {
|
||||
myCell.setTextWeight("bold");
|
||||
}
|
||||
XSSFColor textColor = font.getXSSFColor();
|
||||
if (textColor != null) {
|
||||
byte[] rgbColor = textColor.getRGB();
|
||||
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]) + ")");
|
||||
}
|
||||
myCell.setContent(readCellContent(cell));
|
||||
data.get(i)
|
||||
.add(myCell);
|
||||
} else {
|
||||
data.get(i)
|
||||
.add(new MyCell(""));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (workbook != null) {
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.baeldung.excel;
|
||||
|
||||
public class MyCell {
|
||||
private String content;
|
||||
private String textColor;
|
||||
private String bgColor;
|
||||
private String textSize;
|
||||
private String textWeight;
|
||||
|
||||
public MyCell() {
|
||||
}
|
||||
|
||||
public MyCell(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getTextColor() {
|
||||
return textColor;
|
||||
}
|
||||
|
||||
public void setTextColor(String textColor) {
|
||||
this.textColor = textColor;
|
||||
}
|
||||
|
||||
public String getBgColor() {
|
||||
return bgColor;
|
||||
}
|
||||
|
||||
public void setBgColor(String bgColor) {
|
||||
this.bgColor = bgColor;
|
||||
}
|
||||
|
||||
public String getTextSize() {
|
||||
return textSize;
|
||||
}
|
||||
|
||||
public void setTextSize(String textSize) {
|
||||
this.textSize = textSize;
|
||||
}
|
||||
|
||||
public String getTextWeight() {
|
||||
return textWeight;
|
||||
}
|
||||
|
||||
public void setTextWeight(String textWeight) {
|
||||
this.textWeight = textWeight;
|
||||
}
|
||||
|
||||
}
|
||||
-6
@@ -27,7 +27,6 @@ import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.templateresolver.ITemplateResolver;
|
||||
|
||||
import com.baeldung.excel.ExcelPOIHelper;
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
@@ -118,11 +117,6 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
configurer.setUrlPathHelper(urlPathHelper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ExcelPOIHelper excelPOIHelper() {
|
||||
return new ExcelPOIHelper();
|
||||
}
|
||||
|
||||
@Bean(name = "multipartResolver")
|
||||
public CommonsMultipartResolver multipartResolver() {
|
||||
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
|
||||
|
||||
-63
@@ -1,63 +0,0 @@
|
||||
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 java.util.Map;
|
||||
import java.util.List;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Controller
|
||||
public class ExcelController {
|
||||
|
||||
private String fileLocation;
|
||||
|
||||
@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();
|
||||
model.addAttribute("message", "File: " + file.getOriginalFilename() + " has been uploaded successfully!");
|
||||
return "excel";
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/readPOI")
|
||||
public String readPOI(Model model) throws IOException {
|
||||
|
||||
if (fileLocation != null) {
|
||||
if (fileLocation.endsWith(".xlsx") || fileLocation.endsWith(".xls")) {
|
||||
Map<Integer, List<MyCell>> data = excelPOIHelper.readExcel(fileLocation);
|
||||
model.addAttribute("data", data);
|
||||
} else {
|
||||
model.addAttribute("message", "Not a valid excel file!");
|
||||
}
|
||||
} else {
|
||||
model.addAttribute("message", "File missing! Please upload an excel file.");
|
||||
}
|
||||
return "excel";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user